gpt4 book ai didi

R fiddle 图和箱线图在一起,使填充仅对箱线图表现不同

转载 作者:行者123 更新时间:2023-12-02 20:02:02 25 4
gpt4 key购买 nike

好的,所以我想将 fiddle 图与白色箱线图一起绘制,但我的数据有点棘手。我将 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)

我能找到的最好方法是手动设置填充,但它会影响 fiddle 图和箱线图:

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"))

Said plot

我可以只将箱线图设为白色而不将 fiddle 设为白色吗?

附注如何只为 fiddle 制作图例而不显示箱线图?

最佳答案

试试这个:

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)

enter image description here

关于R fiddle 图和箱线图在一起,使填充仅对箱线图表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49451307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com