将垂直线(平均值)的图例项添加到现有图例

时间:2015-10-24 21:47:32

标签: r ggplot2 legend density-plot

我正在生成这样的密度图:

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

这几乎产生了我想要的精确情节:

enter image description here

但我还有一些事情要做:

  1. 包含平均线的图例项
  2. 替换图例项目的顺序,以便将线条与适当的密度图例项目(Ti,平均TI,TIu,平均TIu)分组
  3. 当我尝试使用show_guide = T:

    将这些行添加到图例时

    geom_vline(data = mean,aes(xintercept = values.mean,color = type),              linetype ="虚线",尺寸= 1, show_guide = T

    我明白了:

    enter image description here

    我该怎么做呢?

    编辑:使用彼得的答案,我能够得到这个:

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

    图表: enter image description here

    我希望我能想出的其他项目:

    1. 获取每个图例周围的缓冲区,以便将两者连接起来并使它们显示为一个

1 个答案:

答案 0 :(得分:1)

我认为您可以通过调整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