R小提琴图和箱线图在一起,使填充行为仅对箱线图不同
R Violin plots and boxplots together, make fill behave differently only for boxplots
好的,所以我想将小提琴图与 white 箱线图一起绘制,但我的数据有点棘手。我将来自 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)
我能找到的最好的方法是手动设置填充,但它会同时影响小提琴图和箱线图:
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"))
我可以只将箱线图设为白色而不是小提琴图吗?
P.S。我怎样才能只为小提琴制作图例而不显示箱线图?
试试这个:
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)
你也可以试试这个:
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)
好的,所以我想将小提琴图与 white 箱线图一起绘制,但我的数据有点棘手。我将来自 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)
我能找到的最好的方法是手动设置填充,但它会同时影响小提琴图和箱线图:
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"))
我可以只将箱线图设为白色而不是小提琴图吗?
P.S。我怎样才能只为小提琴制作图例而不显示箱线图?
试试这个:
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)
你也可以试试这个:
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)