I'm trying to add boxes to replace the Y axis labels, such as in this figure.
I'm using ggplot but any other R package would work too.
input <- data.frame(
Pre.Subtype=c("Basal sq.", "Basal Sq.", "Basal sq.", "Neuronal", "Luminal", "Luminal", "Luminal"),
Post.Subtype=c("Basal sq.", "No tumor", "Luminal inf.", "Basal sq.", "Luminal", "Luminal pap.", "No tumor"),
Number.of.Patients=c(2,3,4,5,2,4,4), Percentage=c(34,23,23,53,20,34,56))
ggplot(data=input, aes(x=Pre.Subtype, y=Percentage, fill=Post.Subtype)) +
geom_bar(position = 'dodge', stat='identity') +
theme_classic() +
theme( axis.text.y = element_text(face="bold", size=12, colour="black"), axis.title=element_text(size=16,face="bold")) +
theme(legend.text=element_text(size=12), legend.title = element_text(size=14, face="bold")) +
labs(x= "Pre", y="Percentage", title="Post") +
geom_text(aes(label=Number.of.Patients), position=position_dodge(width=0.9), vjust=0.5, hjust=-0.3) +
scale_fill_manual(values=c("#fed976", "#7fcdbb", "#4d004b", "grey"), name="") +
theme(text = element_text(size=20), axis.text.x = element_text(size=13, face="bold", colour="black")) +
theme(plot.title = element_text(hjust = 0.5, face="bold", size=16), axis.ticks.y = element_blank()) +
coord_flip()
You can set clip = "off" in coord_flip as well as ensuring the y limit doesn't drop below zero:
... + coord_flip(ylim = c(0,50), clip = "off") + ...
Now you can draw a geom_rect outside of the plot area:
... + geom_rect(aes(xmin = 0.55, xmax = 0.85, ymin = -10, ymax = -7),
fill = "blue", size = 2, colour = "darkblue")
Which looks something like this:
Or use a geom_label:
geom_label(aes(label = "2", x = 0.55, y = -7), fill = "blue", size = 5, colour = "white")
Like this:
Of course, you'll need to add multiple geoms to get this effect for each category.
For a more automated way to do this, I suggest generating two separate plots, and combining them using ggarrange as shown below.
I have also fixed an issue where different bars were showing up in different widths in your original code.
library(tidyverse)
library(ggpubr)
input <- data.frame(
Pre.Subtype=c("Basal sq.", "Basal Sq.", "Basal sq.", "Neuronal", "Luminal", "Luminal", "Luminal"),
Post.Subtype=c("Basal sq.", "No tumor", "Luminal inf.", "Basal sq.", "Luminal", "Luminal pap.", "No tumor"),
Number.of.Patients=c(2,3,4,5,2,4,4), Percentage=c(34,23,23,53,20,34,56))
p1 <- ggplot(data=input, aes(x=Pre.Subtype, y=Percentage, fill=Post.Subtype)) +
#geom_bar(position = 'dodge', stat='identity') +
geom_col(position = position_dodge2(width = 0.9, preserve = "single")) +
theme_classic() +
theme( axis.text.y = element_blank(), axis.title.y = element_blank(), axis.title.x=element_text(size=16,face="bold")) +
theme(legend.text=element_text(size=12), legend.title = element_text(size=14, face="bold")) +
labs(x= "Pre", y="Percentage", title="Post") +
geom_text(aes(label=Number.of.Patients), position=position_dodge(width=0.9), vjust=0.5, hjust=-0.3) +
scale_fill_manual(values=c("#fed976", "#7fcdbb", "#4d004b", "grey", "red"), name="") +
theme(text = element_text(size=20), axis.text.x = element_text(size=13, face="bold", colour="black")) +
theme(plot.title = element_text(hjust = 0.5, face="bold", size=16), axis.ticks.y = element_blank()) +
scale_y_continuous(expand = c(0,0)) +
coord_flip()
p2 <- ggplot(data=input, aes(x=Pre.Subtype, y=Percentage)) +
theme_void() +
scale_y_continuous(expand = c(0,0)) +
geom_label(data = (input %>% group_by(Pre.Subtype) %>%
summarise_at(c('Number.of.Patients'),sum)), aes(label=Number.of.Patients, y = -0.1), fill = 'blue', color = 'white') +
coord_flip() + labs(title = 'Pre') + theme(plot.title = element_text(hjust = 0.5)) + theme(plot.margin = unit(c(0,-1,0,0), "cm"))
ggarrange(p2,p1, widths = c(0.1,0.9), align = "h")
This will give you the picture below.
Related
I have a plot like this:
library(ggplot2)
library(reshape2)
library(ggh4x)
data <- data.frame(col1=c("Sample1","Sample2","Sample3","Sample4","Sample5","Sample6"),
col2=c(0.5,0.1,0.4,0.05,0.05,0.9),
col3=c(0.6,0.1,0.3,0.1,0.1,0.8),
col4=c(0.5,0.3,0.2,0.05,0.15,0.8),
col5=c("a","a","a","b","b","b"),
col6=c("c","c","c","c","c","c"))
data2 <- melt(data)
ggplot(data=data2, aes(x = variable, y = value, fill=col1))+
geom_bar(position="stack", stat="identity")+
scale_fill_manual(values=c("#e6194B","#ffe119","#f58231","#911eb4","#42d4f4","#bfef45")) +
scale_y_continuous(expand = c(0, 0),labels = scales::percent) +
facet_nested(~col6 + ~col5, scales = "free_x",space = "free_x",switch = "x") +
ggtitle("Title") +
theme_classic() +
theme(strip.text.y = element_text(angle=0),legend.position = "right",
legend.key.size = unit(0.4, 'cm'),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
strip.placement = "outside",
strip.background = element_rect(color = "white", fill = "white"),
axis.title = element_blank()) +
guides(fill=guide_legend(title=NULL, ncol = 1)) +
xlab("X axis") +
ylab("Y axis")
Which creates a barplot like this:
Please take a look
My question is simple, how can I set y-axis starting value to 10% instead of 0% (without changing the code too much). All answers are greatly appreciated! (Similar questions are checked, without success...)
While in general not recommended for bar charts one option to "set" the starting point would be to set the limits via coord_cartesian:
library(ggplot2)
library(ggh4x)
ggplot(data = data2, aes(x = variable, y = value, fill = col1)) +
geom_bar(position = "stack", stat = "identity") +
scale_fill_manual(values = c("#e6194B", "#ffe119", "#f58231", "#911eb4", "#42d4f4", "#bfef45")) +
scale_y_continuous(expand = c(0, 0), labels = scales::percent) +
facet_nested(~ col6 + ~col5, scales = "free_x", space = "free_x", switch = "x") +
ggtitle("Title") +
theme_classic() +
theme(
strip.text.y = element_text(angle = 0), legend.position = "right",
legend.key.size = unit(0.4, "cm"),
axis.line = element_line(colour = "black"),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
strip.placement = "outside",
strip.background = element_rect(color = "white", fill = "white"),
axis.title = element_blank()
) +
guides(fill = guide_legend(title = NULL, ncol = 1)) +
xlab("X axis") +
ylab("Y axis") +
coord_cartesian(ylim = c(.1, NA))
I used ggarrange to make a figure with two panels. I want the two panels to have the same y-axis range and increments. Even with using scale_y_continuous, I can't seem to get them to line up correctly. Plot
Here's the code I've been working with:
ei<- ggplot(data=ingestion, aes(x=Species, y=Numberofparticlesingested, colour=factor(Condition), shape=factor(Condition)))
ei1<- ei + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"),labels=c("Healthy","Bleached"))+
stat_summary(fun.y=mean, geom="point",size=2, position=position_dodge(width =0.90)) +
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom=
"errorbar", position=position_dodge(width=0.90), width=0.2) +scale_y_continuous(limits = c(0, 60))+
labs(x="Species", y="Average number of particles") +
theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))
ei2<- ei1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(strip.background = element_rect(fill="white"))+ theme(
axis.text.x = element_text(size = 14, vjust = -0.2, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans"))
e<- ggplot(data=egestioncumulativedata, aes(x=Time, y=Numberofparticles, colour=factor(Condition), shape=factor(Condition)))
e1<- e + scale_shape_manual(values = c(16,1),name="Condtion",breaks=c("Healthy","Bleached"))+ scale_color_manual(values=c("black", "grey"),name="Condtion",breaks=c("Healthy","Bleached"))+
facet_grid(~Species) +
stat_summary(fun.y=mean, geom="point", size = 2, position=position_dodge(width =0.90)) + stat_summary(fun.y=mean, geom="line", size = 0.8, position=position_dodge(width =0.90))+
stat_summary(fun.data=mean_sdl, fun.args=list(mult=1), geom=
"errorbar", position=position_dodge(width=0.90), width=0.52) + scale_x_discrete(limits=c(6, 12, 18, 24, 30, 36, 42, 48)) + scale_y_continuous(limits = c(0, 60)) +
labs(x=expression(Time~ (Hours)), y=expression(Average ~number ~of ~particles)) +
theme_bw()+ theme(legend.justification = "top")+ theme(legend.key = element_rect(fill = "white", colour = "black"))+ theme(text = element_text(size = 14, family = "sans"))
e2<- e1 + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(strip.background = element_rect(fill="white"))+ scale_fill_discrete(breaks=c("AI","PV"))+ theme(
axis.text.x = element_text(size = 14, vjust = -0.25, color = "black", family = "sans")) + theme(axis.text.y=element_text(size=14, hjust=1, color="black", family = "sans"))
library(ggpubr)
figure6<-ggarrange(ei2,
e2+ theme(
axis.title.y = element_blank() ),
labels= c("a", "b"),
nrow = 1,ncol = 2,
align = "hv",
common.legend = TRUE, legend = "right")
Using the breaks and labels arguments in addition to the limits argument in scale_y_continuous should give you better control of the axes
I have tried any method online but the legend is not automatically showing up. Some previous issue says the usage of dataframe in ggplot2 is not preferable, but I have tried together() method and it doesn't work either. How to fix the bug?
Here is the data:
library(ggplot2)
library(gtable)
R = 0.01*c(7.000, 6.800, 6.620, 6.460, 6.330, 6.250, 6.200, 6.160, 6.125, 6.100)
Maturity = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
c = seq(from=0, to=0, length.out = length(R))
for (i in 1:length(R)){
for (j in 1:i){
c[i] = c[i] + 1/(1+R[j])^Maturity[j]
}
c[i] = (1-1/(1+R[i])^Maturity[i])/c[i]
}
Forward = seq(from=0, to=0, length.out = length(R))
for (i in 1:length(R)){
Forward[i] = (1+R[i+1])^2/(1+R[i])-1
}
Here is the plot code in RMarkDown:
```{R fig.width=2.7559, fig.height=2.0669291}
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
p = ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield), shape=21, fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5, color="black") +
geom_point(aes(y=R), shape=22, fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5, color="black") +
geom_point(aes(y=ForwardRate), shape=23, fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5, color="black") +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20),fill=c('r','r','r'))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))
p
Check this sketch and make the necessary adjustments as I am not clear on how colors must be set. Pay attention to the suggestion from #stefan and modify next code:
library(ggplot2)
#Code
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield,colour='ParYield'), shape=21, fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5,show.legend = T) + #Black
geom_point(aes(y=R,colour='R'), shape=22,
fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5) + #Black
geom_point(aes(y=ForwardRate,colour='ForwardRate'), shape=23,
fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5) +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))+
labs(color='Variable')
Output:
Other option would be:
#Code 2
df = data.frame(Maturity=Maturity, ParYield=c, ForwardRate=Forward, R=R)
ggplot(data=df, aes(x=Maturity, y=ParYield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Par Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(aes(y=ParYield), size=1) +
geom_line(aes(y=R), size=1) +
geom_line(aes(y=ForwardRate), size=1) +
geom_point(aes(y=ParYield,colour='ParYield'), shape=21,
fill=rgb(69/255, 117/255, 180/255),
size=3, stroke=1.5,show.legend = T) +
geom_point(aes(y=R,colour='R'), shape=22,
fill=rgb(145/255, 191/255, 219/255),
size=3, stroke=1.5) +
geom_point(aes(y=ForwardRate,colour='ForwardRate'), shape=23,
fill=rgb(224/255, 243/255, 248/255),
size=3, stroke=1.5) +
scale_color_manual(values = c('black','black','black'))+
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))+
labs(color='Variable')
Output:
You need to map some additional aesthetics to variables in your data. I accomplished this by pivoting your data to a longer format.
library(tidyr)
df2 <- df %>% pivot_longer(-Maturity, names_to = "Yield", values_to = "Rate")
Then, I only used one geom_line and one geom_point, but with the new Yield variable mapped to group, fill, and shape. Now you can use scale_shape_manual and scale_fill_manual to set the appearances the way you want.
p = ggplot(data=df2, aes(x = Maturity, y = Rate, group = Yield,
fill = Yield, shape = Yield)) +
ggtitle("Curve of Zero-coupon Yield, Par Yield, Forward Yield")+
labs(x = "Maturity (year)", y = "Yield") +
scale_y_continuous(breaks=seq(100*(min(na.omit(Forward), c)-0.01),
100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,
sec.axis = dup_axis(),
labels = scales::number_format(accuracy = 0.005)) +
scale_x_continuous("Maturity", labels=as.character(Maturity), breaks=Maturity,
sec.axis = dup_axis()) +
theme_classic() +
geom_line(size=1) +
geom_point(size=3, stroke=1.5, color="black") +
scale_shape_manual(values = c("ParYield" = 21, "R" = 22, "ForwardRate" = 23),
labels = c("Par Yield", "Zero-coupon Yield", "Forward Yield")) +
scale_fill_manual(values = c("ParYield" = rgb(69/255, 117/255, 180/255),
"R" = rgb(145/255, 191/255, 219/255),
"ForwardRate" = rgb(224/255, 243/255, 248/255)),
labels = c("Par Yield", "Zero-coupon Yield", "Forward Yield")) +
theme(plot.title = element_text(size=14, hjust=0.5),
text = element_text(size=15, colour = "black", family = "Calibri"),
axis.ticks.length = unit(-0.25, 'cm'),
axis.line = element_line(size=1),
axis.ticks = element_line(size=1),
axis.text.x = element_text(margin = margin(t=15)),
axis.text.x.top = element_text(margin = margin(b=15)),
axis.text.y.right = element_text(margin = margin(l=15, r=5)),
axis.text.y = element_text(margin = margin(l=5, r=15)),
axis.title.x.top = element_blank(),
axis.title.y.right = element_blank())+
guides(colour=guide_legend(override.aes = list(pch=c(16,21,20),fill=c('r','r','r'))))+
theme(legend.position = c(0.8,0.8), legend.justification = c("right", "top"))
I would recommend rethinking your y axis labels.'
I'm trying to plot some data on top of an imported image using ggplot2. However I can not get a visual grid in the plot..
Here is a simplified example to illustrate my issue:
library(ggplot2)
library(scales)
library(jpeg)
library(scales)
library(grid)
#picture from internet
myurl <- "http://upload.wikimedia.org/wikipedia/commons/9/95/Apollonian_spheres.jpg"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readJPEG(z)
file.remove(z) # cleanup
x <- sample(1:10, replace=T, 10)
y <- c("a","b","c","d","e","f","g","h","i", "j")
df <- data.frame(y,x)
p <-ggplot(df, aes(y, x, fill=y)) +
annotation_custom(rasterGrob(pic, width=unit(1,"npc"), height=unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_bar(stat = "identity", fill="darkorange",width=0.8, alpha=0.75 )+
#geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
coord_flip() + ggtitle("Something")+ theme_classic() +
labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
theme(panel.grid.major = element_line(colour = "white", linetype = "dotted"))
p
#without picture works fine
p <-ggplot(df, aes(y, x, fill=y)) +
geom_bar(stat = "identity", fill="darkorange",width=0.8, alpha=0.75 )+
#geom_text(aes(label=data2$Attributes), vjust=1.5,colour="black")
coord_flip() + ggtitle("Something")+ theme_classic() +
labs(y = "yyy", x = "xxx") + guides(fill = guide_legend(reverse=TRUE))+
theme(axis.text.y = element_blank()) + theme(plot.title = element_text(size=20))+
theme(axis.title.x = element_text(size = 16))+ theme(axis.title.y = element_text(size = 16))+
theme(legend.text = element_text( size = 14)) + theme(legend.title = element_text( size = 16))+
theme(panel.grid.major = element_line(colour = "black", linetype = "dotted"))
p
Any ideas?
I have a plot with three ECDFs and three vertical lines, and cannot get the stat_ecdf legend line color to match the actual line color.
Here is the code for just the ECDF plot:
set.seed(124)
allDTI <- data.frame(values=rnorm(1000,500,200),type=sample(LETTERS[1:3],1000,T))
meanALLDTI <- ddply(allDTI, "type", summarise, values.mean=mean(values, na.rm=TRUE), n)
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1, show_guide=T) +
xlab(expression('Index Value')) +
ylab("Cumulative Density") +
ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=28,vjust=0,hjust=0),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white")) +
scale_color_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(TI[d]),expression(TI[l]),
expression(TI[w]))) +
guides(color = guide_legend(title=NULL))
Here is the plot:
But I can't figure out how to add the vertical lines I want to add in the manner I want to add them:
Code:
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1, show_guide=T) +
xlab(expression('Index Value')) +
ylab("Cumulative Density") +
ggtitle(expression(TI[d]~'and'~TI[l]~'and'~TI[w])) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x=element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y=element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=28,vjust=0,hjust=0),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white")) +
scale_color_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(TI[d]),expression(TI[l]),
expression(TI[w]))) +
guides(color = guide_legend(title=NULL)) +
geom_vline(data=meanALLDTI, aes(xintercept=values.mean, colour=type),
linetype="dashed", size=1, show_guide=T) +
scale_linetype_manual(values=c('grey10','grey30','grey50'),
labels=c(expression(bar(TI[d])),expression(bar(TI[l])),
expression(bar(TI[w])))) +
guides(linetype= guide_legend(title=NULL))
Plot:
As an example of what I want, here is how it came out for me with density plots:
After some trial and error, I think it's better to use geom_segment in order to get a nice legend. With:
ggplot(allDTI, aes(x=values, color=type)) +
stat_ecdf(size=1) +
geom_segment(data=meanALLDTI, aes(x=values.mean, xend=values.mean, y=0, yend=1, color=type, linetype = type), size=1) +
labs(title=expression(TI[d]~'and'~TI[l]~'and'~TI[w]), x="Index Value", y="Cumulative Density") +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
scale_color_manual(values=c("grey10", "grey30", "grey50"),
labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
scale_linetype_manual(values=c(3,2,5),
labels=c(expression(TI[d]),expression(TI[l]),expression(TI[w]))) +
guides(color = guide_legend(title="color",
override.aes = list(color = c("grey10", "grey30", "grey50"),
size = 2)),
linetype = guide_legend(title="lty", override.aes = list(linetype = c(3,2,5),
size = 0.7))) +
theme(text = element_text(size=20),
plot.title = element_text(size=30,face="bold",vjust=1),
axis.title.x = element_text(size=20,face="bold",vjust=0,hjust=0.5),
axis.title.y = element_text(size=20,face="bold",vjust=1.0,hjust=0.5),
legend.position = c(0.85, 0.25),
legend.box = 'horizontal',
legend.title = element_blank(),
legend.text.align = 0,
legend.text = element_text(size=20,vjust=0,hjust=0),
legend.key = element_rect(fill=NA),
legend.key.height = unit(1.5, 'line'),
legend.key.width = unit(1.5, 'line'),
panel.background = element_rect(fill = "white"))
you get: