如何手动将颜色设置为ggplot中的第二个主题?

时间:2017-07-07 16:43:24

标签: r ggplot2

很抱歉,如果我的问题有点令人困惑,请不要犹豫,编辑它。

我正在比较两组数据,现在我正在创建情节。

在某些情节中,我可以显示两个组,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

1 个答案:

答案 0 :(得分:0)

您需要的是什么?试试这个:

ggplot(aes(y = boxthis, x = f2, fill = f1), data = df) + 
    geom_boxplot() +
    facet_grid(~ f1)

Link to image