首页 文章

使用ggplot2的两个不同geom 'coloured'和'filled'的混合图例

提问于
浏览
4

我想在一个单独的图和垂直线中为三个类别绘制核密度图,以指示整个分布的平均值和中值 . 使用 ggplot2 ,情节相当容易,但我非常努力 .

set.seed(1234)
data <- data.frame(value = rgamma(n = 10000, shape = 3, scale = 1),
               type = sample(letters[1:3], size = 10000, replace = TRUE))
data$value[data$type == "b"] <- data$value[data$type == "b"] +
                          rnorm(sum(data$type == "b"), mean = 2)
data$value[data$type == "c"] <- data$value[data$type == "c"] +
                          rnorm(sum(data$type == "c"), mean = 4)

# Let's produce a 'coloured' AND 'filled' density plot
# ('cause I want both the area under the curve and the line to be coloured)
library(ggplot2)
gp <- ggplot(data=data, aes_string(x="value"))
gp <- gp + geom_density(aes_string(fill="type", colour="type"), alpha=0.3)
gp

这给了我情节和传奇没有问题 .

plot1

现在,我添加垂直线条,情节很好,但传说真的很难看 .

# Now let's add vertical lines to the plot, indicating the mean 
# and median for the whole distribution
vlines <- data.frame(mean_median = c(mean(data$value), median(data$value)),
                 labels = c("Mean", "Median"))

gp <- gp + geom_vline(data=vlines,
                  aes(xintercept=mean_median, colour=labels),
                  size=1.05, linetype="dashed", show_guide=TRUE)
gp

plot2

图例全部与'coloured' vlines 以及'coloured'和'filled'密度混淆 . 我确实想要两个传说,其中一个是vlines的传说(两个条目),另一个想要密度图的颜色的图例 .

一种解决方法是使 geom_density 变为彩色或填充,但不是两者 . 它效果更好,但它不是我想要的(因为我想要两者,密度图的面积和线条都有颜色) . 它是这样的 . 而不是这样做:

gp <- gp + geom_density(aes_string(fill="type", colour="type"), alpha=0.3)

我这样做:

gp <- gp + geom_density(aes_string(fill="type"), alpha=0.3)

plot3

结果几乎是我想要的,但我真的想要曲线下面积和密度图线都要着色 .

我一直试图在整个网络上找到解决方案,似乎没有任何效果 . 这些问题有时会使用geom的 show_guide 来处理,因为你可以覆盖是否为各个geoms绘制图例 . 我玩过它并且它在我的情况下不起作用,因为我需要两个geoms(密度和vlines)的传说,问题是在其中一个geoms中加上填充和颜色,加上另一个中的颜色 .

Stack Overflow(bar and line plot in one chart with a legend under ggplot2)中发布了一个非常类似的问题,但在那里使用的解决方案(使用子集)不适用于我的情况 .

我真的很感激任何想法 . 我一直在努力解决这个问题很长一段时间,我找不到解决方案 .

1 回答

  • 4

    这可能是一种更简单的方法,但你可以破解情节对象,作为上述评论的替代方案 . 您可以根据自己的喜好对情节进行两个绘图,也可以根据需要对图例进行两个绘图并交换图例 .

    # Plot with density area and line coloured but legend not right
    p1 <- ggplot(data=data, aes(x=value)) + 
                geom_density(aes(fill=type, colour=type), alpha=0.3 ) + 
                geom_vline(data=vlines, aes(xintercept=mean_median, colour=labels), 
                             linetype="dashed", size=1.5, show_guide=TRUE ) 
    
    g1 <- ggplotGrob(p1)
    
    # Plot with density line not coloured but legend is ok
    p2 <- ggplot(data=data, aes(x=value)) + 
                geom_density(aes(fill=type), alpha=0.3 ) + 
                geom_vline(data=vlines, aes(xintercept=mean_median, colour=labels), 
                             linetype="dashed", size=1.5, show_guide=TRUE )  +
                guides(fill = guide_legend(override.aes = list(linetype = 0 ))) 
    
    g2 <- ggplotGrob(p2)
    
    
    # Add legend of second plot to first plot    
    g1$grobs[which(g1$layout$name=="guide-box")] <- 
                                  g2$grobs[which(g2$layout$name=="guide-box")]  
    
    grid::grid.newpage()    
    grid::grid.draw(g1)
    

    enter image description here

相关问题