简体   繁体   English

R小提琴图和箱形图一起使填充表现出不同的箱形图

[英]R Violin plots and boxplots together, make fill behave differently only for boxplots

Ok, so I want to plot violin plots together with white boxplots, but my data is a little bit tricky. 好的,所以我想把小提琴情节和白色的盒子图一起绘制,但我的数据有点棘手。 I melted data from data.frame with several columns, each of which has values corresponding to factor with two levels, here is approximation of my data: 我将data.frame中的数据与多个列融合,每个列的值都对应于两个级别的因子,这里是我的数据的近似值:

library(ggplot2)
library(reshape2)

dat <- list(
  A = rbind(
    data.frame(group = "1",
               vals = rnorm(500)),
    data.frame(group = "2",
               vals = rnorm(100))
  ),
  B = rbind(
    data.frame(group = "1",
               vals = rnorm(500)),
    data.frame(group = "2",
               vals = rnorm(100))
  ),
  C = rbind(
    data.frame(group = "1",
               vals = rnorm(500)),
    data.frame(group = "2",
               vals = rnorm(100))
  )
)

dat.melt <- melt(dat)

The best I could find is to set fill manually, but it affects both violin plots and boxplots: 我能找到的最好的是手动设置填充,但它会影响小提琴图和箱形图:

dodge <- position_dodge(width = 1)

p <- ggplot(dat.melt, aes(x = L1, y = value, fill = group))+
  geom_violin(position = dodge)+
  geom_boxplot(width = 0.3,
                 position = dodge,
                 outlier.shape = NA)+
  scale_fill_manual(values=c("white", "white"))

说情节

Can I make only boxplots white and not violins? 我可以只制作白色的箱形图而不是小提琴吗?

PS How can I make legends only for violins and not showing boxplots? PS我怎样才能为小提琴制作传说而不显示箱形图?

Try this: 试试这个:

p <- ggplot(dat.melt, aes(x = L1, y = value)) +
  geom_violin(aes(fill = group), position = dodge) +
  geom_boxplot(aes(group=interaction(group,L1)), 
            width=0.3, fill="white", position=dodge,
            outlier.shape=NA)
print(p)

在此输入图像描述

You can also try this: 你也可以试试这个:

ggplot(dat.melt, aes(x = L1, y = value, fill=group))+
  geom_violin(position = position_dodge(width = 1)) +
  geom_boxplot(data = dat.melt, 
               aes(x = L1, y = value, col=group), fill="white", 
               position = position_dodge(width = 1), width=0.3,outlier.shape=NA)+
  geom_boxplot(position = position_dodge(width = 1), alpha=0, width=0.3)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM