如何在 R 中使用 ggplot2 更改图例类型?

发布于 2025-01-11 07:37:17 字数 193 浏览 1 评论 0原文

我按组使用 geom_area() 创建了一个图表。图例的形状是正方形。我希望它是一条线,所以没有任何关于我用于填充美学的比例的信息。我怎样才能做到这一点?

Ps:guides(override.aes()) 没有帮助,因为它只允许我更改我放入美学中的信息,而不是乘坐fill

I created a graph using geom_area() by group. The shape of the legend is a square. I would like it to be a line, so without any information regarding the scale I use for the fill aesthetic. How can I achieve that ?

Ps : guides(override.aes()) is not helpful since it only allows me to change the informations I put in my aesthetics, not to get ride of fill.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海的爱人是光 2025-01-18 07:37:17

目前尚不清楚为什么您想要一个与绘图无关的图例,但您可以使用 key_glyph 参数更改键字形。您可能想使用override.aes将该区域的线条颜色更改为黑色。

library(ggplot2)

set.seed(1)

df <- data.frame(x = rep(1:5, 2), 
                 y = sample(5, 10 , TRUE),
                 group = rep(c("A", "B"), each = 5))

ggplot(df, aes(x, y, fill = group)) + 
  geom_area(key_glyph = draw_key_path) +
  guides(fill = guide_legend(override.aes = list(color = "black")))

输入图片此处描述

It's not clear why you want a legend that doesn't relate to the plot, but you can change the key glyph using the key_glyph parameter. You probably want to use override.aes to change the line colour of the area to black.

library(ggplot2)

set.seed(1)

df <- data.frame(x = rep(1:5, 2), 
                 y = sample(5, 10 , TRUE),
                 group = rep(c("A", "B"), each = 5))

ggplot(df, aes(x, y, fill = group)) + 
  geom_area(key_glyph = draw_key_path) +
  guides(fill = guide_legend(override.aes = list(color = "black")))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文