如何更改GGPLOT2传奇中的项目?

发布于 2025-02-12 10:49:37 字数 2129 浏览 1 评论 0原文

我正在尝试更改此图的传奇项目。

我的代码是:

library(ggplot2)
library(HDInterval)
library(ggridges)

df <- data.frame(
  density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
  source = rep(c("source_1", "source_2", "source_3"),
               each = 400))

ggplot(df, aes(x = density, color = source, linetype = source,
                                fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, size=1.2, 
                               quantile_fun = hdi, # vline_linetype = 0, scale = 1) +
  labs(y = "Density", x = "Contribution") + 
  
 scale_linetype_cyclical(name = "Source", values = c("solid", "dotted", "longdash"),
                          labels = c("source1", "source2", "source3"),
                          guide = "legend") +
  
  scale_fill_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                      labels = c("source1", "source2", "source3"),
                      guide = "none", na.value = "transparent") +
  
  scale_color_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                       labels = c("source1", "source2", "source3"),
                       guide = "none") +

  ylim(0, 8) + xlim(0, 1) +
  theme(#legend.position=c(.85,.75),
    legend.text = element_text(size=16), # item legend text font size
    legend.title=element_text(size=18), # title font size
    legend.key.height= unit(1, 'cm'),# box height
    legend.key.width= unit(1, 'cm')) +  # box width
  guides(color = guide_legend(override.aes = list(fill = "white")))+
  theme(axis.text.y  = element_text(size = 12, vjust = 0.5),
        axis.text.x  = element_text(size = 12, vjust = 0.5),  
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14))

我想在传说中展示台词,而不是盒子。

我将感谢您的帮助。
我试图深入了解ggridges,但是在某些方面有些不同或严格。
提前致谢。

I am trying to change the legend items of this plot.

My code is:

library(ggplot2)
library(HDInterval)
library(ggridges)

df <- data.frame(
  density = c(rgamma(400, 2, 10), rgamma(400, 2.25, 9), rgamma(400, 5, 7)),
  source = rep(c("source_1", "source_2", "source_3"),
               each = 400))

ggplot(df, aes(x = density, color = source, linetype = source,
                                fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), quantile_lines = TRUE, size=1.2, 
                               quantile_fun = hdi, # vline_linetype = 0, scale = 1) +
  labs(y = "Density", x = "Contribution") + 
  
 scale_linetype_cyclical(name = "Source", values = c("solid", "dotted", "longdash"),
                          labels = c("source1", "source2", "source3"),
                          guide = "legend") +
  
  scale_fill_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                      labels = c("source1", "source2", "source3"),
                      guide = "none", na.value = "transparent") +
  
  scale_color_cyclical(name = "Source", values = c("#31a354", "#2c7fb8", "#d95f0e"),
                       labels = c("source1", "source2", "source3"),
                       guide = "none") +

  ylim(0, 8) + xlim(0, 1) +
  theme(#legend.position=c(.85,.75),
    legend.text = element_text(size=16), # item legend text font size
    legend.title=element_text(size=18), # title font size
    legend.key.height= unit(1, 'cm'),# box height
    legend.key.width= unit(1, 'cm')) +  # box width
  guides(color = guide_legend(override.aes = list(fill = "white")))+
  theme(axis.text.y  = element_text(size = 12, vjust = 0.5),
        axis.text.x  = element_text(size = 12, vjust = 0.5),  
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14))

I would like to show lines in the legend, instead of the boxes.

enter image description here

I would appreciate your help.
I have tried to understand ggridges deeply, however it is a bit different or rigid for some things.
Thanks in advance.

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

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

发布评论

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

评论(1

怎会甘心 2025-02-19 10:49:37
ggplot(df, aes(x = density, color = source, linetype = source,
               fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), size=1.2, 
                               quantile_lines = TRUE, quantile_fun = hdi,
                               key_glyph = "path") +
  ...

https://ggplot2.tidyverse.org/reference.orperion/reference/draw_key.hekey.html

=“ https://i.sstatic.net/0x3yi.png” rel =“ nofollow noreferrer”> “

ggplot(df, aes(x = density, color = source, linetype = source,
               fill = after_stat(ifelse(quantile == 2, NA, color)))) +
  geom_density_ridges_gradient(aes(y = 0), size=1.2, 
                               quantile_lines = TRUE, quantile_fun = hdi,
                               key_glyph = "path") +
  ...

https://ggplot2.tidyverse.org/reference/draw_key.html

enter image description here

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