曾宇辰
曾宇辰

Reputation: 1

Changing colors of bars on the plot in R

library(ggplot2)

df <- data.frame(trt=c("TAVERN","Long Term Care","Grocery Store","Restaurant"),
                 outcome=c("a","b","c","d"))

ggplot(df,aes(trt, outcome)) + 
  geom_col() +
  geom_point(colour = 'red') +
  ggtitle("Failing rate in different facility types") + 
  labs(x="Facility type",y="Failing rate") + 
  theme(panel.background = element_blank()) +
  scale_color_manual(values = c("purple","green", "red", "orange"))

I tried to change the color in the ggplot manually.

enter image description here

But I fail to do so. Really confused about that.

Question. How to set up the parameters of ggplot() function in order to change the bars' colors?

Upvotes: 0

Views: 116

Answers (1)

Kota Mori
Kota Mori

Reputation: 6750

I assume you want to change the colors of the bars. Then, you need to specify the fill option of the geom_col.

library(ggplot2)
df <- data.frame(trt=c("TAVERN","Long Term Care","Grocery Store","Restaurant"),
                 outcome=c("a","b","c","d"))
ggplot(df,aes(trt, outcome)) + 
  geom_col(fill=c("purple","green", "red", "orange")) + 
  geom_point(colour = 'red') + 
  ggtitle("Failing rate in different facility types") +
  labs(x="Facility type", y="Failing rate") +
  theme(panel.background = element_blank())

Alternatively, you can set the fill aesthetics. Note that fill is for the interior. colour is for the boundary.

ggplot(df,aes(trt, outcome, fill=trt)) +
  geom_col() +
  geom_point(colour = 'red') + 
  ggtitle("Failing rate in different facility types") +
  labs(x="Facility type", y="Failing rate") +
  theme(panel.background = element_blank()) +
  scale_fill_manual(values = c("purple","green", "red", "orange"))

Upvotes: 1

Related Questions