John Adams
John Adams

Reputation: 1

Changing colors of bars in geom_bar

I am trying to have the color of the bars in the code below to go from a light blue to a dark blue (bottom to top)

q2r <- c("Not good at all", "slightly good",
         "somewhat good", "quite good", "extremely good")

q2 <- survey %>%
  ggplot(aes(x = connect_goals)) +
  geom_bar() +
  scale_x_discrete(limits = q2r) + 
  scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, 1)) +
  labs(title = "During class, how good is the teacher at connecting 
                the material being taught to clearly stated learning goals?",
       x = "", y = "") +
  coord_flip()

Picture of visualization

Upvotes: 0

Views: 4018

Answers (2)

Jake Kaupp
Jake Kaupp

Reputation: 8072

Great start to your first question! Next time remember to include a sample of your data to make a reproducible example for us to help you.

To do this properly, you'll just want to factor your data properly, set up an aesthetic mapping for fill, then apply scale function to get your colours right.

library(tidyverse)

#Reproducible data for your example
survey <- tibble(connect_goals = c("Not good at all","slightly good","slightly good","somewhat good",
                                   "somewhat good","somewhat good","somewhat good","somewhat good", "extremely good","extremely good",
                                   "extremely good","extremely good","extremely good"))

q2r <- c("Not good at all", "slightly good",
         "somewhat good", "quite good", "extremely good")

# Create a factor out of connect goals, which sets the proper order for scales.
survey %>%
  mutate(connect_goals = factor(connect_goals, q2r)) %>% 
  ggplot(aes(x = connect_goals, fill = connect_goals)) +
  geom_bar() +
  scale_x_discrete(limits = q2r) + 
  scale_fill_brewer(direction = -1, palette = "Blues") +
  scale_y_continuous(limits = c(0, 10),
                     breaks = seq(0, 10, 1)) +
  labs(title = "During class, how good is the teacher at connecting 
                the material being taught to clearly stated learning goals?",
       x = "", y = "") +
  coord_flip()

Created on 2018-12-16 by the reprex package (v0.2.1)

Upvotes: 3

NelsonGon
NelsonGon

Reputation: 13319

Without your data here is a suggested solution.You should set fill in your aesthetics. Also there are multiple ways to do manual color changes. For instance you can type ?scale_fill_manual() and see how it works. You're probably looking for ?scale_fill_discrete() and the Blues Set if memory serves me right. Thanks to Jake's comment, I'll edit this answer.

q2r <- c("Not good at all", "slightly good",
    "somewhat good", "quite good", "extremely good")

  q2 <- survey %>%
        ggplot(aes(x = connect_goals,fill=connect_goals)) +
        geom_bar() +
        scale_x_discrete(limits = q2r) + 
        scale_y_continuous(limits = c(0, 10),
             breaks = seq(0, 10, 1)) +
        labs(title = "During class, how good is the teacher at connecting 
            the material being taught to clearly stated learning goals?",
             x = "", y = "") +
         coord_flip()

Cheers!

Upvotes: 1

Related Questions