【发布时间】:2017-07-07 16:43:23
【问题描述】:
抱歉,如果我的问题令人困惑,请不要犹豫,编辑它。
我正在比较 2 组数据,现在正在创建绘图。
在某些图上,我可以同时显示两个组,而 ggplot 会自动添加一些漂亮的颜色,在我的例子中,第一组为红色,第二组为蓝色。
我的问题是,在其他地块上,我必须显示一组或另一组,所以颜色总是红色。
如何手动要求 ggplot 用主题的第二种颜色填充我的情节?
如果你喜欢,这里有一些虚拟代码:
df <- data.frame(f1=factor(rbinom(100, 1, 0.45), label=c("m","w")),
f2=factor(rbinom(100, 1, 0.45), label=c("young","old")),
boxthis=rnorm(100))
#both groups on the graph, 2 colors (red and blue) :
ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + geom_boxplot()
#each group on one graph, both the same color (and I'd like one red and one blue)
ggplot(aes(y = boxthis, x = f2, fill = "m"), data = df) + geom_boxplot()
ggplot(aes(y = boxthis, x = f2, fill = "w"), data = df) + geom_boxplot()
#I know the grouping is incorrect, this is just an example where I keep the same df
【问题讨论】:
-
手动设置调色板或颜色不能解决问题吗?
-
是的,会的,但是我有很多情节,所以会很乏味。
-
我不明白你的问题,你希望你的个人组是蓝色的吗?
-
不,我想要一个红色和一个蓝色,与 ggplot 在第一个分组图中使用的颜色相同。
-
ggplot(aes(y = boxthis, x = f2, fill = "m"), data = df) + geom_boxplot(fill = '#F8766D')ggplot(aes(y = boxthis, x = f2, fill = "w"), data = df) + geom_boxplot(fill = '#00BFC4').