ggplot 有两个图例,颜色图例中显示错误的形状

Posted

技术标签:

【中文标题】ggplot 有两个图例,颜色图例中显示错误的形状【英文标题】:ggplot has two legends and the wrong shape shows up in the color legend 【发布时间】:2016-12-29 15:07:51 【问题描述】:

我正在使用 R 中的 ggplot 创建图表,但图例未正确显示。首先,我得到了两个图例,一个用于颜色,一个用于线型。尽管我在 scale_color_manual 和 scale_linetype_manual 中放置了相同的项目,但这些项目都出现了,正如关于该主题的其他几篇文章中所建议的那样。此外,颜色图例为三个项目中的每一个都显示相同的形状(带点和 x 的线),而它们应该都不同(前两个应该是带点的线,而第三个应该是 x没有线)。

这是一个可重现的例子。

library(ggplot2)
library(dplyr)

#specify color palette
b.navHexRGB <- c(green=rgb(149,214,0, maxColorValue=255),
             red=rgb(229,60,46, maxColorValue=255),
             gray=rgb(85,87,89, maxColorValue=255),
             dark_green=rgb(100,140,26, maxColorValue=255),
             yellow=rgb(255,183,24, maxColorValue=255),
             purple=rgb(139,24,155, maxColorValue=255),
             blue=rgb(0,147,201, maxColorValue = 255))

#create plot
ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
        size = .9) +
geom_point(aes(group = treatment, colour=treatment),
         size = 1.5) +
geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
         aes(y=stat_sig, colour=treatment),
         size = 2.5,
         shape=4,
         na.rm=T) +
guides(colour=guide_legend(nrow=3)) +
scale_color_manual(name= "Variable",values=c(palette(b.navHexRGB)), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
scale_linetype_manual(name="Variable",values=c(1,2), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
ylab("Average Daily Consumption (kWh)") +
xlab("Year-Month") +
theme_bw() +
theme(legend.title = element_blank(),
    legend.justification = c(0,0), 
    legend.position = "bottom",
    legend.key = element_rect(fill = "white",colour = "white"),
    #legend.key.width = unit(1.1, "cm"),
    axis.text.x = element_text(angle=45, hjust=1, color="black"),
    axis.text.y = element_text(color="black"),
    axis.title.y = element_text(vjust=1)
)

数据

df <- structure(list(treatment = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L), .Label = c("Control", "Recipient"), class = "factor"), 
    bill_month = c(9, 9, 10, 10, 11, 11, 12, 12, 1, 1, 2, 2, 
    3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8), bill_year = c(2013, 
    2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 
    2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
    2014, 2014, 2014), bill_yrmo = c(201309, 201309, 201310, 
    201310, 201311, 201311, 201312, 201312, 201401, 201401, 201402, 
    201402, 201403, 201403, 201404, 201404, 201405, 201405, 201406, 
    201406, 201407, 201407, 201408, 201408), mean_kwh = c(34.1891698781763, 
    34.8263665605318, 22.998584869823, 23.6329516672246, 21.0428206185862, 
    21.7774153609304, 25.4992975653725, 25.8397296039854, 28.74368522348, 
    29.200670842288, 29.8474912589325, 30.373483172434, 26.7411627390396, 
    26.4600472396878, 21.628265542195, 21.3047667878863, 19.502019234349, 
    19.062337524723, 24.1381516068859, 24.3165665754673, 27.8915927136898, 
    28.3625761820341, 26.8570348685593, 27.1359185596385), p.value = c(9.36594553258583e-07, 
    9.36594553258583e-07, 1.76373182797948e-13, 1.76373182797948e-13, 
    2.12425701682086e-15, 2.12425701682086e-15, 0.00415203493379312, 
    0.00415203493379312, 0.00109178463449181, 0.00109178463449181, 
    0.00122110380638705, 0.00122110380638705, 0.0438138636035026, 
    0.0438138636035026, 0.00140538140516743, 0.00140538140516743, 
    5.74367939388898e-07, 5.74367939388898e-07, 0.100848768452669, 
    0.100848768452669, 0.000172505914392074, 0.000172505914392074, 
    0.145110211153141, 0.145110211153141), stat_sig = c(19, 19, 
    19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 
    19, NA, NA, 19, 19, NA, NA)), .Names = c("treatment", "bill_month", 
"bill_year", "bill_yrmo", "mean_kwh", "p.value", "stat_sig"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -24L))

【问题讨论】:

只是一个指定的调色板。 【参考方案1】:

我遇到了类似的问题。在 scale_color_manual()、scale_shape_manual() 和 scale_linetype_manual() 中指定 name="Variable" 解决了这个问题。 values 参数的命名向量并没有改变我的结果。

【讨论】:

【参考方案2】:

谢谢大家。我将上面的两个响应结合起来得到了我需要的东西。

ggplot(data = avgkwh_pre2, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
      geom_point(aes(group = treatment, colour = treatment, shape = treatment),
                 size = 2) +
      geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
                size = .9) +
      scale_shape_manual(values = c("Recipient" = 16, "Control" = 16, "Indicates the difference is statistically significant" = 4)) +
      scale_color_manual(values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]), 
                         breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
      scale_linetype_manual(values = c("Recipient" = 1,"Control" = 2, "Indicates the difference is statistically significant" = 0), 
                            breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
      ylab("Average Daily Consumption (kWh)") +
      xlab("Year-Month") +
      ggtitle(paste("Group Starting", rct_start)) +
      theme_bw() +    
      theme(legend.title = element_blank(),
            legend.justification = c(0,0), 
            legend.position = "bottom",
            legend.key = element_rect(fill = "white",colour = "white"),
            #legend.key.width = unit(1.1, "cm"),
            axis.text.x = element_text(angle=45, hjust=1, color="black"),
            axis.text.y = element_text(color="black"),
            axis.title.y = element_text(vjust=1)
      )

【讨论】:

【参考方案3】:

我经常发现在调用ggplot 之前排列数据很有用。我rbind “统计显着”行与主数据框对齐,并将“统计显着”行的“y”美学对齐到与其他数据相同 (mean_kwh = stat_sig):

dd <- rbind(df, df %>% 
  mutate(treatment= 'Indicates the difference is statistically significant',
         mean_kwh = stat_sig))

然后拨打ggplot。注意“统计显着”也有linetype,只是它是0

#create plot
ggplot(data = dd, aes(x=as.character(bill_yrmo), y=mean_kwh)) +
  geom_point(aes(group = treatment, colour = treatment, shape = treatment),
             size = 1.5) +
  geom_line(aes(group = treatment, colour = treatment, linetype = treatment),
             size = .9) +
  scale_shape_manual(values = c(1, 2, 4)) +
  scale_color_manual(values = c(palette(b.navHexRGB)), 
                     breaks = c("Control", "Recipient", "Indicates the difference is statistically significant")) +
  scale_linetype_manual(values = c(1, 2, 0), 
                        breaks = c("Control","Recipient","Indicates the difference is statistically significant")) +
  labs(y = "Average Daily Consumption (kWh)",
       x = "Year-Month") +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.justification = c(0,0), 
        legend.position = "bottom",
        legend.key = element_rect(fill = "white",colour = "white"),
        axis.text.x = element_text(angle=45, hjust=1, color="black"),
        axis.text.y = element_text(color="black"),
        axis.title.y = element_text(vjust=1)
  )

输出:

【讨论】:

【参考方案4】:

如果情节的一般美学映射在主要的ggplot 调用中处理,则可以简化您对geom_linegeom_point 的调用。但主要问题是您在 scale 调用中指定颜色和线型的方式。如果您向values 参数提供命名向量,这些函数更不容易出错,这保证了对映射的控制。

这段代码:

ggplot(data = df, aes(x=as.character(bill_yrmo), y=mean_kwh, color = treatment, lty = treatment)) +
  geom_line(size = .9, aes(group = treatment)) +
  geom_point(size = 1.5) +
  geom_point(data = df %>% mutate(treatment= 'Indicates the difference is statistically significant'),
             aes(y=stat_sig, colour=treatment),
             size = 2.5,
             shape=4,
             na.rm=T) +
  scale_color_manual(name = "Variable", values = c("Recipient" = b.navHexRGB[["gray"]], "Control" = b.navHexRGB[["green"]], "Indicates the difference is statistically significant" = b.navHexRGB[["red"]]), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  scale_linetype_manual(name="Variable",values = c("Recipient" = 2, "Control" = 1, "Indicates the difference is statistically significant" = 0), breaks=c("Control","Recipient","Indicates the difference is statistically significant")) +
  labs(x = "Year-Month", y = "Average Daily Consumption (kWh)") +
  theme_bw() +
  theme(legend.title = element_blank(),
        legend.justification = c(0,0), 
        legend.position = "bottom",
        legend.key = element_rect(fill = "white",colour = "white"),
        legend.direction = "vertical",
        axis.text.x = element_text(angle=45, hjust=1, color="black"),
        axis.text.y = element_text(color="black"),
        axis.title.y = element_text(vjust=1)
  )

产生这个情节:

【讨论】:

以上是关于ggplot 有两个图例,颜色图例中显示错误的形状的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2密度图图例显示错误的组名和错误的颜色

难以获得混合线a和指向ggplot2中颜色和形状的点图例

R语言ggplot2可视化抑制(部分)图例(legend)输出实战:抑制颜色图例输出保留数据点形状图例输出

ggplot2 - 在大小图例中显示多个键(形状)

ggplot中只有一个图例的斜体标签

(R) 如何在 ggplot2 中根据颜色、线型和点形状获取图例?