【问题标题】:How to manually set the color as the second one of the theme in ggplot?如何手动将颜色设置为ggplot中的第二个主题?
【发布时间】: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').

标签: r ggplot2


【解决方案1】:

是您要寻找的方面吗?试试这个:

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

Link to image

【讨论】:

    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多