【问题标题】:Changing Colors Grouped Bar Chart ggplot2改变颜色分组条形图ggplot2
【发布时间】:2018-09-19 11:49:03
【问题描述】:

我正在尝试将条形图的颜色分别调整为“#054C70”和“#05C3DE”。当我使用以下代码时:

p2 <- ggplot(b, aes(x = Currency, y = amount, color = inbound_outbound)) + geom_bar(position = "dodge", stat = "identity")  + labs(title = "Subset Avg. Order Amount") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
        + scale_fill_manual(values = c("#054C70","#05C3DE"))

我收到以下错误: +scale_fill_manual(values = c("#054C70", "#05C3DE")) 中的错误: 一元运算符的参数无效

我正在用 R 编码。任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 尝试改用scale_color_manual
  • + 需要放在行尾,而不是下一行的开头

标签: r ggplot2


【解决方案1】:

这里发生了一些事情。

  1. 第二行代码开头的 + 符号应位于第一行代码的末尾。
  2. 如果您想更改条形本身的颜色(而不仅仅是条形的轮廓),您需要使用fill 映射(而不是color 映射。

使用来自 diamonds dataset 的示例,因为我没有您正在使用的特定数据集,

library(dplyr)
library(ggplot2)

## filter dataset to only have two different colors to best match the example
df <- diamonds %>% filter(color %in% c("D","E"))

## change color to fill in this first line
p2 <- ggplot(df, aes(x = cut, y = price, fill=color)) + 
  geom_bar(position = "dodge", stat = "identity")  + 
  labs(title = "Subset Avg. Order Amount") + 
  ## make sure the plus sign is at the end of this line
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) + 
  scale_fill_manual(values = c("#054C70","#05C3DE"))

这将产生以下情节:example plot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多