我正在尝试创建一个分组小提琴图(见下图),其中我为四个分类变量绘制了3个级别。除了箱子与宽的小提琴图的颜色相同使得难以查看之外,该图表的数据显示正常。理想情况下,我希望所有的箱子都保持白色。我知道箱子改变颜色的原因是我选择了
fill
参数。我想知道是否有一种方法可以将geom_violin
和geom_boxplot
的填充分开。
我使用的精简代码如下:
p <- ggplot(df, aes(x=metric, y=value, fill=variable))+
geom_violin(width=0.9, position=position_dodge(0.75), bw=1.5)+
geom_boxplot(width=0.3, outlier.shape = NA, position=position_dodge(0.75))+
scale_fill_manual(values=c("gray50", "gray75", "gray100"),
breaks=c("res.error.random", "res.error.increase", "res.error.decrease"),
labels=c("random cost", "overestimated", "underestimated"))
ggplot(aes())
中删除fill = variable
并将其放置在geom_boxplot(aes())
中。在这种情况下,只有箱线图会受到fill
参数的影响。 - bVa