简体   繁体   English

更改geom_bar中的条形颜色

[英]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()

可视化图片

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) reprex软件包 (v0.2.1)创建于2018-12-16

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. 例如,您可以键入?scale_fill_manual()并查看其工作方式。 You're probably looking for ?scale_fill_discrete() and the Blues Set if memory serves me right. 如果内存适合我,您可能正在寻找?scale_fill_discrete()和Blues Set。 Thanks to Jake's comment, I'll edit this answer. 感谢Jake的评论,我将编辑此答案。

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! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM