向ggplot添加多个图例 [英] Adding multiple legends to ggplot

查看:109
本文介绍了向ggplot添加多个图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下多层情节:

df <- data.frame(number = 1:10, 
                 values = rnorm(10), 
                 decision = factor(rbinom(10,1,.5), levels = c(0,1),
                                   labels=c("No","Yes")),
                 expValues = rnorm(10),
                 bandwidth = runif(10, 0,1))

ggplot(df,aes(x = number, y = values, color=decision)) + aes(group=NA) +
  geom_line(size = 1) + 
  geom_point(size = 3,shape = 16) + 
  geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth), 
              stat = "identity") +
  geom_point(data=df,
             aes(x=number,y=expValues),shape = "x", size = 5, color = "blue") +
  geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean  = 12"), 
            aes(label=label, x =  x, y = y) ,
            hjust = 1, vjust = -0.1, color = "brown", size = 10) +
  geom_hline(yintercept=mean(df$values) ,color="blue", linetype = "dashed") +
  theme(text=element_text(size=20))

我想添加形状为"x"的geom_hlinegeom_point的图例,分别表示第一个是剪切值",第二个是期望值".

I want to add legends for geom_hline and geom_point with shape "x", denoting the for the first it is "Cut Value", for the second "Expected Value".

我该怎么做?

注意:我检查了帖子,并

Note: I checked THIS post and THIS post for possible solutions but cannot figure out how can I do it for especially geom_hline.

推荐答案

要添加图例,首先要做的是将某些东西实际映射到所需的美感.例如,现在您已将点形状设置为x,但没有在aes映射它,因此没有图例.您可以映射到常量和变量以强制使用图例.

To add legends, the first thing to do is to actually map something to the desired aesthetic. For example, right now you've set the point shape as x, but you didn't map it inside of aes so you don't get a legend. You can map to constants as well as to variables to force a legend.

对于您的geom_point层,您只需将shape移到aes内,即可映射到所需的任何常量.使用一些指示其含义的值将使图例编辑更加容易.

For your geom_point layer, you can just move shape inside aes, mapped to whatever constant you like. Using some value that indicates what it is will make the legend editing easier.

geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"), size = 5, color = "blue")

对于geom_hline,您需要特定于图层的数据集以进行映射.根据您走的路线,您可能还需要在此层中将show_guide设置为TRUE.

For the geom_hline, you'll need a dataset specific to the layer for mapping purposes. Depending on the route you go, you may also need to set show_guide to TRUE in this layer.

geom_hline(data = data.frame(yint = mean(df$values)), aes(...), show_guide = TRUE)

您可以制作两个单独的图例.您还可以在此处中将线条和形状组合为单个图例.这两个选项都涉及在适当的scale_xxx_manual中设置值并在guide_legend中使用override.aes.

You could make two separate legends. You could also combine the line and shapes into a single legend as in this answer here. Both options will involve setting values in the appropriate scale_xxx_manual and using override.aes in guide_legend.

在这里您可以制作一个新的图例.注意,我必须在geom_line之前添加geom_hline,以使decision图例看起来正确.

Here is how you could make a single new legend. Notice I had to add geom_hline before geom_line to make the decision legend look right.

ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
    geom_hline(data = data.frame(yint = mean(df$values)),
        aes(yintercept = yint, shape = "Cut value"), 
             color="blue", linetype = 2, show_guide = TRUE) +
    geom_line(size = 1) + 
    geom_point(size = 3,shape = 16) + 
    geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth), 
              stat = "identity") +
    geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"), 
             size = 5, color = "blue") +
    geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean  = 12"), 
        aes(label=label, x =  x, y = y),
        hjust = 1, vjust = -0.1, color = "brown", size = 10) +      
    theme(text=element_text(size=20)) +
    scale_shape_manual(name = "", values = c("x", "x")) +
    guides(shape = guide_legend(override.aes = list(linetype = c(2, 0), 
                                            shape = c(NA, "x"))))

编辑为错误带功能区添加图例

Edit Add a legend for the error band ribbon

我无法完全使用fill来添加基于错误带功能区的第三个图例.您可以将其作为三个单独的图例进行操作,尽管我认为间距不太好:

I couldn't quite get things working with fill to add a third legend based on the error band ribbon. You could do this as three separate legends, although I don't think the spacing is as nice:

ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
    geom_hline(data = data.frame(yint = mean(df$values)),
        aes(yintercept = yint, linetype = "Cut value"), 
             color="blue", show_guide = TRUE) +
    geom_line(size = 1) + 
    geom_point(size = 3,shape = 16) + 
    geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth, fill = "Error band"), 
              stat = "identity") +
    geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"), 
             size = 5, color = "blue") +
    geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean  = 12"), 
        aes(label=label, x =  x, y = y),
        hjust = 1, vjust = -0.1, color = "brown", size = 10) +  
    theme(text=element_text(size=20)) +
    scale_shape_manual(name = "", values = "x") +
    scale_linetype_manual(name = "", values = 2) +
    scale_fill_manual(name = "", values = "grey") +
    guides(shape = guide_legend(override.aes = list(linetype = 0)),
          fill = guide_legend(override.aes = list(linetype = 0)),
          color = guide_legend(override.aes = list(fill = NA)))

或者,在override.aes内部进行一些工作,可以结合使用coloursize以及linetypeshape.

Alternatively, with some work inside override.aes, this could be done with the combination of colour and size along with linetype and shape.

ggplot(df, aes(x = number, y = values, color=decision, group = NA)) +
    geom_hline(data = data.frame(yint = mean(df$values)),
        aes(yintercept = yint, shape = "Cut value"), 
             color="blue", linetype = 2, show_guide = TRUE) +
    geom_line(size = 1) + 
    geom_point(size = 3,shape = 16) + 
    geom_smooth(data = df, aes(ymin = values-bandwidth , ymax = values+bandwidth, shape = "Error band"), 
              stat = "identity", show_guide = FALSE) +
    geom_point(data=df, aes(x=number, y=expValues, shape = "Expected value"), 
             size = 5, color = "blue") +
    geom_text(data = data.frame(x = Inf, y = max(df$values), label = "Mean  = 12"), 
        aes(label=label, x =  x, y = y),
        hjust = 1, vjust = -0.1, color = "brown", size = 10) +  
    theme(text=element_text(size=20)) +
    scale_shape_manual(name = "", values = rep("x", 3)) +
    guides(shape = guide_legend(override.aes = list(linetype = c(2, 1, 0), 
                                           size = c(.5, 10, 5),
                                           shape = c(NA, NA, "x"),
                                           colour = c("blue", "grey75", "blue"))))

这篇关于向ggplot添加多个图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆