如何在ggplot2中获取堆叠的geom_bar和geom_point的通用图例?

索兰

因此,我正在使用以下信息构建图:

  • 以堆叠的geom_bar图表显示并按国家/地区划分单个家庭的收入组成部分(工资,社会转移,税收)
  • 纯收入每户型国家的税收和转移支付后,一个geom_point图表中显示。

这被安排在以下数据框中:

example <- data.frame(country = c("Arcadia", "Asgard", "Atlantis", "Avalon"),
            wage = c(80, 70, 55, 40),
            transfers = c(15, 15, 5, 20),
            tax = c(-10, -5, -5, 0),
            net = c(85, 80, 65, 60)) %>% gather(icom, euro, wage:net)

然后将其绘制成图:

income_graph <- ggplot(filter(example, icom != "net"), aes(x = country, y = euro, fill = factor(icom, levels = c("transfers", "wage", "tax")))) +
  geom_bar(stat = "identity", colour = "black") +
  geom_point(data = filter(example, icom == "net"), colour = "black", shape = 5) +
  labs(fill = "Income components") +
  scale_fill_brewer(palette = "Greys", labels=(c("Social transfers", "Wage", "Tax"))) 
income_graph 

结果如下图所示。

在此处输入图片说明

这是我需要帮助的地方:图形完成了我需要做的事情,但是我一生都无法弄清楚如何让geom_bar和geom_point图形使用组合图例(即圆形“净”指标)在相同的“收入成分”标题下)。从我发现阅读其他条目的发现来看,一种解决方案可能是将geom_bar和geom_point都映射到“填充”,但这似乎最终是将“净”菱形与其他icom条目重叠。[*]

在此先感谢您,希望问题不会重复出现-我在StackOverflow上找不到适用的解决方案,但是如果答案很明显,它将很高兴地重定向到一个解决方案。

LS

[*] =(单独地并且以较小的比例,是否有可能使“净”指标用白色填充并用黑色轮廓填充?这可能是合乎逻辑的解决方案,但我发现自己对此有些挠头。 )

Senzeybek

geom_point(shape=21) + scale_color_manual 在这种情况下可以挽救生命

income_graph <- ggplot(filter(example, icom != "net"), aes(x = country,
 y = euro, fill = factor(icom, levels = c("transfers", "wage", "tax",'net')))) +
  geom_bar(stat = "identity", colour = "black") +
  geom_point(data = filter(example, icom == "net"),aes(color=icom), 
  shape = 21, size = 1.5, stroke = 1,fill='white') +
  labs(fill = "Income components") +
  scale_fill_brewer(palette = "Greys", labels=(c("Social transfers", "Wage", "Tax"))) +
  scale_colour_manual("", values='black', label='Net')
income_graph 

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ggplot2:合并geom_line,geom_point和geom_bar的图例

使用geom_bar和geom_point控制ggplot中的图例

ggplot2中带有geom_line和geom_point的图例

带有ggplot2和geom_point的图例

从图例中删除ggplot2的geom_point图标

填充地图上geom_point的ggplot2图例

ggplot2 geom_point alpha在图例中与图例不同

ggplot2。如何使geom_bar堆叠图的y范围为0-100%?

如何在geom_bar ggplot2中为许多列着色相同的颜色

如何在geom_bar ggplot2中更改颜色,否则无法正常工作

调整ggplot2中堆叠的geom_bar的y轴原点

在ggplot2中更改geom_bar的图例键的形状

如何在Shiny-R中的ggplot2中的geom_line和geom_bar可视化之间切换

geom_bar和geom_point条件颜色

ggplot2:将geom_line和geom_point组合在一起的图形中的坐标图例

如何更改 ggplot geom_bar 中的条形名称和图例标题?

ggplot2 | geom_bar和position =“ identity”

geom_bar的宽度和间隙(ggplot2)

将geom_point覆盖到geom_bar。如何对齐点和条?

ggplot2 2.0.0中的geom_point边界

ggplot2 中的振幅线 + geom_point

geom_point {ggplot2}中的alpha值

ggplot2 geom_bar的问题

无法在 r 中使用 ggplot2 将组合 geom_point 和 _line 图的颜色和线型图例分开

ggplot2:如何在图例键中分隔geom_polygon和geom_line?

在ggplot2中的geom_bar上重叠文本

ggplot2-结合了geom_point和geom_line的图例

使用geom_point()和geom_line()进行多个系列的ggplot中的图例错误

为ggplot2中的负值和正值在geom_point中设置不同的颜色