r - 更改 geom_bar 中条形的颜色
问题描述
我试图让下面代码中的条形颜色从浅蓝色变为深蓝色(从下到上)
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()
解决方案
你的第一个问题很好的开始!下次请记住包含您的数据样本,以便我们为您提供可重复的示例。
要正确执行此操作,您只需正确考虑数据,设置填充的美学映射,然后应用缩放功能以正确设置颜色。
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()
由reprex 包(v0.2.1)于 2018 年 12 月 16 日创建
推荐阅读
- ffmpeg - 创建一个 Discord Bot 来播放 .mp3 文件
- json - 如何使用 Serde 反序列化带有 MIXED 顶级数组的 JSON?
- javascript - 如何创建一个可以随机填充数据集中数据的表?
- python - 遗传算法IndexError:列表索引超出范围
- r - 如何在ggplot的散点图中绘制配对数据?
- android - 使用自定义适配器回收光刻组件列表
- jekyll - 使用 Hostinger 部署 Jekyll 博客
- python - 如何将自定义表情符号放入嵌入页脚 | 不和谐.py
- python - 单节点 Elasticsearch Docker SDK python
- javascript - 在 chart.js 中将 mysql x-axes 日期更改为当天