与asked here的问题非常相似。但是,在这种情况下,两个图的填充参数不同。对于我的情况,两个图的填充参数相同,但是我想要不同的配色方案。
我想手动更改框线图和散点图中的颜色(例如,将框设置为白色,将点着色)。
示例:
require(dplyr)
require(ggplot2)
n<-4*3*10
myvalues<- rexp((n))
days <- ntile(rexp(n),4)
doses <- ntile(rexp(n), 3)
test <- data.frame(values =myvalues,
day = factor(days, levels = unique(days)),
dose = factor(doses, levels = unique(doses)))
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot( aes(fill = dose))+
geom_point( aes(fill = dose), alpha = 0.4,
position = position_jitterdodge())
使用'scale_fill_manual()'会覆盖箱线图和散点图的美感。
通过在geom_point中添加'colour',我发现了一个黑点,然后当我使用scale_fill_manual()时,散点颜色未更改:
p<- ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(fill = dose), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = factor(test$dose)),
position = position_jitterdodge(jitter.width = 0.1))+
scale_fill_manual(values = c('white', 'white', 'white'))
是否有更有效的方法来获得相同的结果?
答案 0 :(得分:1)
您可以使用group
来设置不同的箱形图。无需设置填充然后覆盖它:
ggplot(data = test, aes(x = day, y = values)) +
geom_boxplot(aes(group = interaction(day, dose)), outlier.shape = NA)+
geom_point(aes(fill = dose, colour = dose),
position = position_jitterdodge(jitter.width = 0.1))
并且您永远不要在data$column
内使用aes
-仅使用裸列。在简单的情况下,使用data$column
就可以了,但是只要有stat
层或构面,就会中断。