0
votes

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:

enter image description here

But there are still a couple of things i wish to do:

  1. Have legend items for the mean lines
  2. 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:

enter image description here

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)))

Graph: enter image description here

Only additional items I wish I could figure out:

  1. Get the buffer around each legend to be enough to connect the two and make them appear as one
1

1 Answers

1
votes

I think you will be able to get the result you are looking for by adjusting the legend.box

library(ggplot2)
data('diamonds')

plot_data <- subset(diamonds, cut %in% c("Ideal", "Premium"))
means     <- aggregate( price ~ cut, plot_data, mean)

ggplot(plot_data, aes(x = price, fill = cut)) + 
  geom_density(alpha = 0.3) + 
  scale_x_continuous() + 
  geom_vline(data = means, aes(xintercept = price, color = cut), linetype = 2, show_guide = TRUE) + 
  scale_fill_manual(name = "", 
                    values=c('red','blue'), 
                    labels = c("Premium", "Ideal")) +
  scale_color_hue(name = " ",  
                  labels = c("Mean", "Mean")) +
  theme(legend.box = 'horizontal', 
        legend.position = c(0.75, 0.75)) + 
  guides(fill = guide_legend(override.aes = list(linetype = 0)))

enter image description here