I am generating density plots like so:
# Density Plot ZOOM with no legend title and mean lines
ggplot(comb, aes(x=values, fill = type)) +
geom_density(alpha = 0.3) +
scale_x_continuous(limits=c(9, 15), expand = c(0, 0)) +
xlab("Index Value") +
ylab("Density") +
ggtitle(expression('TI versus TI'[u])) +
theme(text = element_text(size=30),axis.text.x=element_text(angle=0,hjust=1,vjust=.9,size=20),
axis.text.y=element_text(angle=0,hjust=1,vjust=.8,size=20),
plot.title = element_text(size=40, face="bold", vjust=2),
axis.title.x=element_text(size=25,face="bold",vjust=-0.5,hjust=0.5),
axis.title.y=element_text(size=25,face="bold",vjust=1.5,hjust=0.5),
legend.position = c(.87,.90),
legend.text.align = 0) +
scale_fill_manual(values=c('red','blue'),
labels=c(expression('TI '),expression(TI[u]))) +
guides(fill=guide_legend(title=NULL)) +
geom_vline(data=mean, aes(xintercept=values.mean, colour=type),
linetype="dashed", size=1)
And that generates almost the exact plot I want:
But there are still a couple of things i wish to do:
- Have legend items for the mean lines
- Alternate the order of the legend items so the lines are grouped with the appropriate density legend item (Ti, mean TI, TIu, mean TIu)
When I try to just add the lines to the legend using show_guide=T:
geom_vline(data=mean, aes(xintercept=values.mean, colour=type), linetype="dashed", size=1, show_guide=T)
I get this:
How can I go about doing this?
EDIT: Using Peter's answer, I was able to get this:
# Density Plot ZOOM with no legend title and mean lines - double legend
ggplot(comb, aes(x=values, fill = type)) +
geom_vline(data=mean, aes(xintercept=values.mean, colour=type),
linetype="dashed", size=1, show_guide=T) +
geom_density(alpha = 0.3) +
scale_x_continuous(limits=c(9, 15), expand = c(0, 0)) +
xlab("Index Value") +
ylab("Density") +
ggtitle(expression('TI versus TI'[u])) +
theme(text = element_text(size=30),axis.text.x=element_text(angle=0,hjust=1,vjust=.9,size=20),
axis.text.y=element_text(angle=0,hjust=1,vjust=.8,size=20),
plot.title = element_text(size=20,face="bold", vjust=1),
axis.title.x=element_text(size=15,face="bold",vjust=-0.5,hjust=0.5),
axis.title.y=element_text(size=15,face="bold",vjust=1.5,hjust=0.5),
legend.position = c(0.15, 0.90),
legend.text.align = 0,
legend.box = 'horizontal',
legend.margin = unit(45.0, 'line'),
legend.text=element_text(size=12,vjust=0,hjust=0)) +
scale_color_hue(labels=c(expression(bar(TI)),
expression(bar(TI[u])))) +
guides(color = guide_legend(title=NULL)) +
scale_fill_manual(values=c('red','blue'),
labels=c(expression(TI),
expression(TI[u]))) +
guides(fill = guide_legend(title=NULL, override.aes = list(linetype = 0)))
Only additional items I wish I could figure out:
- Get the buffer around each legend to be enough to connect the two and make them appear as one