【问题标题】:R boxplot how is this manual fill wrongR boxplot 这个手册怎么填错了
【发布时间】:2023-03-13 00:25:01
【问题描述】:

我想给我的箱线图变量上色。我看了here 并尝试了以下方法,但绘图框都是标准的白色(我在类型中有 6 个因素)。我应该改变什么?

library(ggplot2)
ggplot(PGcounts, aes(Type, Word)) + 
            geom_boxplot() + 
            coord_trans(y = "log10") +
            scale_fill_manual(values = c("white","white","white","red","blue","white"))

【问题讨论】:

  • -1 是不幸的,因为问题是真实的并且背景工作是链接的。此外,答案表明,为什么在一种情况下您会通过“scale_fill_manual”列出手动颜色而在另一种情况下通过 geom_boxplot() 添加它们并不明显。

标签: r ggplot2 colors boxplot


【解决方案1】:

您也可以将原始代码中的geom_boxplot() 更改为geom_boxplot(aes(fill=Type))

例如:

ggplot(PGcounts, aes(Type, Word)) + 
        geom_boxplot(aes(fill=Type)) + 
        coord_trans(y = "log10") +
        scale_fill_manual(values = c("white","white","white","red","blue","white"))

【讨论】:

  • 谢谢 - 很高兴知道,我试过了。但是在这种特殊情况下,我想要特定的颜色。
  • 您在问题中的原始代码应该适用于通过此更改手动填充颜色(即分配特定颜色)。如果将aes(fill=Type) 添加到geom_boxplot() 中,则scale_fill_manual() 将起作用。
  • 我很感激。我没有意识到我需要两者都到位才能工作 - 我认为这是一个或另一个。
【解决方案2】:

需要改变的是

geom_boxplot() +

geom_boxplot(fill = c("white","white","white","red","blue","white")) + 

并删除

scale_fill_manual(values = c("white","white","white","red","blue","white"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多