【问题标题】:How to customise the colour of ggplot2 boxplots?如何自定义 ggplot2 箱线图的颜色?
【发布时间】:2019-07-24 10:49:21
【问题描述】:

我想用自定义颜色填充我创建的箱线图。例如,我希望它们填充具有以下规格的颜色:red=116、blue=49、green=0、Hue=223、sat=240、lum=55。我的代码在下面,但我不知道如何将填充颜色更改为我想要的颜色

我尝试使用 scale_fill_manual() 但显示的图表没有颜色。

v1 = ggplot(data, aes(x=SYSTEMS, y=ADEQUACY)) + geom_boxplot() + coord_cartesian(ylim = c(1, 3))


v1 + scale_fill_manual(values = c('red','blue'))

我的预期结果是打印填充了具有指定值的颜色的箱线图。该怎么做?

【问题讨论】:

  • 您必须将data 中的列映射到颜色或填充(可能通过在aes() 调用中添加fill= SYSTEMS)。

标签: r ggplot2 colors


【解决方案1】:

scale_fill_manual() 不起作用,因为您没有指定任何填充美学。

这应该可行:

ggplot(mtcars, aes(x = cyl, y = wt, group = cyl, fill = as.factor(cyl))) +
  geom_boxplot() +
  scale_fill_manual(values = c("red", "blue", "grey"))

但是,您也可以在 geom_boxplot() 调用中指定填充颜色:

ggplot(mtcars, aes(x = cyl, y = wt, group = cyl)) +
  geom_boxplot(fill = c("red", "blue", "grey"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多