【问题标题】:custom color for each group + category combination raincloud plot每组自定义颜色+类别组合雨云图
【发布时间】:2020-12-30 00:51:33
【问题描述】:

我有一个雨云图:

但我希望 TL group 和 yr 的每个组合都是不同的颜色,就像在基本 boxplot() 中所做的那样:

我尝试使用以下代码绘制雨云图:

Y_C_rain= ggplot(yct_rain, aes(y=d13C, x=lengthcat,fill = yr,color=yr)) +
   geom_flat_violin(position = position_nudge(x = .2, y =0), alpha = .8)+
   geom_point(aes(y = , color = yr), 
             position = position_jitter(width = .05), size = 2, alpha = .5) +
   geom_boxplot(width = .3, guides = FALSE, outlier.shape = NA, alpha = 0, notch = FALSE) +
   stat_summary(fun= mean, geom = "point", shape = 21, size = 3, fill = "black") +
   scale_y_continuous (limits = c(-35,-10),expand = c(0,0),breaks=seq(-35,-10,5)) +
   ylab("d13C") + xlab("TL group") +
   ggtitle("YCT d13C") +
   theme_bw() +
   scale_colour_discrete(my_clrs_yct)+
   scale_fill_discrete(my_clrs_yct)
Y_C_rain

我知道雨图中的颜色需要使用scale_fill_xxx 的一些变体进行编码,但我遇到了障碍,因为似乎每个点也需要有自己的颜色。因此,仅列出 6 种颜色的 scale_fill_xxx 的变体不起作用。

【问题讨论】:

标签: r ggplot2 colors


【解决方案1】:

你想要这样的东西吗?

library(dplyr)
library(data.table)
library(ggplot2)
# used geom_flat_violin from https://gist.github.com/dgrtwo/eb7750e74997891d7c20

my_clrs_yct <- c("#404040", "#407a8c", "#7a7a7a", "#404f86", "#a6a6a6", "#3e1451")

## used storms from dplyr as reproducible example
data("storms")
setDT(storms)
storms[, season:= factor(ifelse(month <=6, "Q12", "Q34"))]

ggplot(storms, aes(x=status, y=pressure, color=interaction(status, season), 
                   fill=interaction(status, season))) + 
    geom_point(aes(color = interaction(status, season)), 
               position = position_jitterdodge(
                   jitter.width=.1, dodge.width=.25), size = 2, alpha = .5)+
    geom_flat_violin(position = position_nudge(x = .5, y =0), alpha = .5)+
    geom_boxplot(width = .3, guides = FALSE, outlier.shape = NA, alpha = 0)+
    stat_summary(fun = mean, geom = "point", shape = 21, size = 3, 
                 fill = "black", position = position_nudge(x = c(-.075,.075), y =0)) +
    theme_bw() +
    scale_colour_manual(values=my_clrs_yct) +
    scale_fill_manual(values=my_clrs_yct)

【讨论】:

  • 是的,这很完美,interaction() 组件正是我所缺少的!
猜你喜欢
  • 1970-01-01
  • 2021-10-24
  • 2015-05-14
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
  • 2019-11-09
  • 1970-01-01
  • 2019-03-08
相关资源
最近更新 更多