首页 > 解决方案 > 分割小提琴图(ggplot)的纯色图例?

问题描述

我试图让这个分裂小提琴情节中的传说颜色变得坚实。我已成功删除了此处的行(在此站点上的另一篇文章之后),但我不知道如何继续。

分裂小提琴

ctn_sv <- ggplot(z, aes(x = time, y = ctn_mean, fill = group), position = position_dodge(.9), alpha = .01) + 
  geom_split_violin(trim = TRUE, alpha = 0.65) +
  ggtitle("CTN: FFF vs ZZZ")+
  theme_bw() + theme(legend.title = element_blank()) +
  scale_fill_brewer(palette = "Dark2") +
  stat_summary(fun = mean, fun.min = mean, fun.max = mean,
               geom = "crossbar", 
               width = 0.25,
               position = position_dodge(width = .25)) +
  guides(fill = guide_legend(override.aes = list(linetype = 0)),
         color = guide_legend(override.aes = list(linetype = 0)))

我使用了这里的分割小提琴图的代码Split violin plot with ggplot2

提前感谢您的任何建议!

标签: rggplot2legendviolin-plot

解决方案


你的 geom="crossbar" 给你这个问题。您可以在 stat_summary 中使用 show.legend=FALSE 来解决此问题。请参见下面的代码和相应的输出:

set.seed(20160229)

z = data.frame(
  y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)),
  x=c(rep('a', 2000), rep('b', 2000)),
  group=c(rep('i', 1000), rep('j', 2000), rep('i', 1000))
)

ctn_sv <- ggplot(z, aes(x = x, y = y, fill = group), position = position_dodge(.9), alpha = .71) + 
  geom_split_violin(trim = TRUE, alpha = 0.65) +
  ggtitle("CTN: FFF vs ZZZ")+
  theme_bw() + 
  theme(legend.title = element_blank()) +
  scale_fill_brewer(palette = "Dark2") +
  stat_summary(fun = mean, fun.min = mean, fun.max = mean,
               geom = "crossbar", 
               width = 0.25,
               show.legend = FALSE,
               position = position_dodge(width = .25)) 
ctn_sv

输出


推荐阅读