更改颜色分组的条形图ggplot2

时间:2018-09-19 11:49:03

标签: r ggplot2

我正在尝试将条形图的颜色分别调整为“#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(值= c(“#054C70”,“#05C3DE”))中的错误:   一元运算符的参数无效

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

1 个答案:

答案 0 :(得分:1)

这里发生了一些事情。

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

使用菱形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