R语言 如何在ggplot2上填充小提琴情节

hgncfbus  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(98)

如何在ggplot2中填充小提琴?
样本数据

Ram2 <- structure(list(
    G = 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), 
                  levels = c("1", "2"), class = "factor"), 
    T = c(3, 1, 2, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3, 2, 3, 3, 3, 3, 2, 1, 3, 2, 3, 
          4, 4, 2, 2, 3, 3, 2)), row.names = c(NA, -30L), 
    class = c("tbl_df", "tbl", "data.frame"))

这是我的剧本

ggplot(Ram2, aes(x = G, y = T, 
                color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)]))+  
    geom_violin(alpha = 0.8, adjust=0.05)+
    scale_color_manual(values = c(WT = '#3CBEC3', at5g01950 = '#C3413C')) +
    geom_boxplot(width = 0.1, color = "black", alpha = 0.2, 
                 position = position_dodge(0.9)) + 
    theme_minimal() + 
    theme(plot.background = element_rect(fill = "white") ,
          panel.grid.minor = element_blank()) +
    labs(color = '')

这是我的输出

我想把小提琴涂上和轮廓一样的颜色。
谢谢你
我使用fill =“color”,但它用相同的颜色填充两个变量。

4zcjmb1e

4zcjmb1e1#

我们可以将其添加到全局aes()中,以将评估的颜色复制到填充美学中:

ggplot(Ram2, aes(x = G, y = T, 
             color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)],
             fill = after_scale(colour)))+  
...

相关问题