r - ggplot2:合并 geom_line、geom_point 和 geom_bar 的图例

标签 r ggplot2

ggplot2图中,我将geom_linegeom_pointgeom_bar组合在一起,并且在合并时遇到问题将图例放入一个盒子中。

基本图的代码如下。使用的数据更进一步。

# Packages
library(ggplot2)
library(scales)

# Basic Plot
ggplot(data = df1, aes(x = Year, y = value, group = variable, 
    colour = variable, shape = variable)) + 
geom_line() + 
geom_point(size = 3) + 
geom_bar(data = df2, aes(x = Year, y = value, fill = variable), 
    stat = "identity", alpha = 0.8) + 
ylab("Current Account Transactions (Billion $)") + 
xlab(NULL) +  
theme_bw(14) + 
scale_x_discrete(breaks = seq(1999, 2013, by = 2)) +
scale_y_continuous(labels = dollar, limits = c(-1, 4), 
    breaks = seq(-1, 4, by = .5)) + 
geom_hline(yintercept = 0) + 
theme(legend.key = element_blank(), 
    legend.background = element_rect(colour = 'black', fill = 'white'), 
    legend.position = "top", legend.title = element_blank()) + 
guides(col = guide_legend(ncol = 1), fill = NULL, colour = NULL)

enter image description here

我的目标是将传奇融合在一起。由于某种原因,“经常账户余额”出现在顶部图例中(我不明白为什么),而“导出”和“进口”图例则因黑色背景和缺失形状而困惑。

如果我将fill放在aes之外,我可以获得“导入”和“导出”的图例,以正确的形状和颜色显示,并且没有黑色背景,但随后我丢失了“当前帐户余额”的 fill 图例。

我之前使用过并取得了一些成功的技巧,即使用 scale_colour_manualscale_shape_manualscale_fill_manual(也许还有 scale_alpha )在这里似乎不起作用。让它发挥作用就好了。但请注意,据我所知,使用此技巧时,必须手动指定颜色、形状和填充,但我实际上不想这样做,因为我对默认的颜色/形状/填充非常满意。

我通常会做这样的事情,但它不起作用:

library(RColorBrewer)
cols <- colorRampPalette(brewer.pal(9, "Set1"))(3)
last_plot() + scale_colour_manual(name = "legend", values = cols) + 
    scale_shape_manual(name = "legend", values = c(0,2,1)) + 
    scale_fill_manual(name = "legend", values = "darkred") 

在上面我没有指定标签,因为在我的问题中我将处理大量数据,手动指定标签是不切实际的。我希望 ggplot2 使用默认标签。出于同样的原因,我想使用默认的颜色/形状/填充。

其他地方也报告了类似的困难,例如这里 Construct a manual legend for a complicated plot ,但我还没有设法应用解决方案来解决我的问题。

有什么想法吗?

# Data

df1 <- structure(list(Year = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L), .Label = c("1999", 
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", 
"2008", "2009", "2010", "2011", "2012", "2013"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Exports of goods and services", 
    "Imports of goods and services"), class = "factor"), value = c(1.304557, 
    1.471532, 1.345165, 1.31879, 1.409053, 1.642291, 1.895983, 
    2.222124, 2.569492, 2.751949, 2.285922, 2.630799, 2.987571, 
    3.08526, 3.178744, 1.600087, 1.882288, 1.740493, 1.776877, 
    1.930395, 2.276059, 2.641418, 3.028851, 3.288135, 3.43859, 
    2.666714, 3.074729, 3.446914, 3.546009, 3.578998)), .Names = c("Year", 
"variable", "value"), row.names = c(NA, -30L), class = "data.frame")

df2 <- structure(list(Year = structure(1:15, .Label = c("1999", "2000 ", 
"2001", "2002 ", "2003", "2004 ", "2005", "2006 ", "2007", "2008 ", 
"2009", "2010 ", "2011", "2012 ", "2013"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = "Balance on current account", class = "factor"), 
    value = c(-0.29553, -0.410756, -0.395328, -0.458087, -0.521342, 
    -0.633768, -0.745434, -0.806726, -0.718643, -0.686641, -0.380792, 
    -0.44393, -0.459344, -0.460749, -0.400254)), .Names = c("Year", 
"variable", "value"), row.names = c(NA, -15L), class = "data.frame")

编辑

在发布我的问题并阅读斯科特的回答后,我尝试了另一种方法。它在某些方面更接近期望的结果,但在其他方面更接近。这个想法是将数据帧合并为单个数据帧,并将颜色/形状/填充传递给第一个 ggplot 调用内的 aes 。这样做的问题是我在图例上得到了不想要的“斜线”。我无法在不删除所有颜色的情况下删除斜线。我立即提到的这种方法的另一个问题是,我需要手动指定一堆东西,而我希望尽可能保留默认值。

df <- rbind(df1, df2)
ggplot(data = df, aes(x = Year, y = value, group = variable, colour = variable, 
    shape = variable, fill = variable)) + 
  geom_line(data = subset(df, variable %in% c("Exports of goods and services", "Imports of goods and services"))) + 
  geom_point(data = subset(df, variable %in% c("Exports of goods and services", "Imports of goods and services")), size = 3) + 
  geom_bar(data = subset(df, variable %in% c("Balance on current account")), aes(x = Year, y = value, fill = variable), 
    stat = "identity", alpha = 0.8) 
cols <- c(NA, "darkgreen", "darkblue")
last_plot() + scale_colour_manual(name = "legend", values = cols) + 
  scale_shape_manual(name = "legend", values = c(32, 15, 17)) + 
  scale_fill_manual(name = "legend", values = c("orange", NA, NA)) + 
  ylab("Current Account Transactions (Billion $)") + 
  xlab(NULL) +  
  theme_bw(14) + scale_x_discrete(breaks = seq(1999, 2013, by = 2)) + 
  scale_y_continuous(labels = dollar, limits = c(-1, 4), breaks = seq(-1, 4, by = .5)) + 
  geom_hline(yintercept = 0) + 
  theme(legend.key = element_blank(), legend.background = element_rect(colour = 'black', fill = 'white'), legend.position = "top", legend.title = element_blank()) + 
  guides(col = guide_legend(ncol = 1)) 

添加 +guides(fill = Guide_legend(override.aes = list(colour = NULL))) 会删除斜杠,但也会删除深绿色/深蓝色(它会保留橙色填充)。

enter image description here

最佳答案

要消除顶部图例中出现的“当前帐户余额”,您可以将颜色形状美学移出父级 ggplot() 调用并适本地调用 geom_line()geom_point() 。这可以具体控制哪些美学适用于共享变量名称的两个数据集。 enter image description here

ggplot(data = df1, aes(x = Year, y = value)) + 
  geom_line(aes(group = variable, colour = variable)) + 
  geom_point(aes(shape = variable, colour = variable), size = 3) + 
  geom_bar(data = df2, aes(x = Year, y = value, fill = variable), 
           stat = "identity", position = 'identity', alpha = 0.8, guide = 'none') + 
  ylab("Current Account Transactions (Billion $)") + 
  xlab(NULL) +  
  theme_bw(14) + 
  scale_x_discrete(breaks = seq(1999, 2013, by = 2)) +
  scale_y_continuous(labels = dollar, limits = c(-1, 4), 
                     breaks = seq(-1, 4, by = .5)) + 
  geom_hline(yintercept = 0) + 
  guides(col = guide_legend(ncol = 1)) + 
  theme(legend.key = element_blank(), 
        legend.background = element_rect(colour = 'black', fill = 'white'), 
        legend.position = "top", legend.title = element_blank(),
        legend.box.just = "left")

这个答案有一些缺点。举几个例子:1)保留了两个独立的图例,如果您决定不将它们装箱(例如,不设置 legend.background ),它们可能会被伪装。 2) 从顶部图例中删除 df2 变量意味着它不会消耗第一个默认颜色(与之前一样,纯属巧合),因此现在“平衡...”和“导出...”都显示为粉红色,因为填充图例回收默认色阶。

关于r - ggplot2:合并 geom_line、geom_point 和 geom_bar 的图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28684612/

相关文章:

r - 如何将第二个变量添加到直方图 ggplot 并在顶部当前直方图上绘图,从第二个变量添加密度曲线

r - ggvis 是否可以交互地更改 x 轴和 y 轴的变量?

r - 如何在 RMarkdown 中将 SVG 设置为默认渲染?

分类树模型中的根节点错误

r - R 中 apply.monthly 的每月操作时间序列

r - 列宽与使用 sendmailr 从 R 发送的 pander 表中的表数据不对齐

r - ggplot2:突出显示图表区域

r - grid.Call 错误

r - ggplot2 : How to reduce the width AND the space between bars with geom_bar

r - 如何在R中对 'paired'向量进行排序