savannahmm5
savannahmm5

Reputation: 11

Changing Colors Grouped Bar Chart ggplot2

I am trying to adjust the colors of my bar chart to be "#054C70" and "#05C3DE" respectively. When I use the following code:

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"))

I get the following error: Error in +scale_fill_manual(values = c("#054C70", "#05C3DE")) : invalid argument to unary operator

I am coding in R. Any help would be appreciated. Thank you.

Upvotes: 1

Views: 9787

Answers (1)

ShanEllis
ShanEllis

Reputation: 39

There are a few things going on here.

  1. The + sign at the beginning of your second line of code should be at the end of your first line of code.
  2. If you want to change the colors of the bars themselves (and not just the outline of the bars), you'll want to use the fill mapping (rather than the color mapping.

Using an example from the diamonds dataset, since I don't have the specific dataset you're using,

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"))

This would produce the following plot: example plot

Upvotes: 3

Related Questions