r - 我想创建一个柱形图,其中每列是两种特定颜色之一
问题描述
我想制作一个柱形图,它采用变量“年”并为具有两个值之一的条形指定特定颜色。
我使用 ggplot 将实际情节放在一起,但是我不确定如何不仅根据“年”将其设置为颜色,而且将其设置为“第一年”的“红色”和上层阶级的“橙色”
structure(list(Hall = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, NA), .Label = c("Thurston",
"MVC", "District House", "Mitchell", "Shenkman", "Potomac", "Guthridge",
"Amdam", "South", "Lafayette", "JBKO", "Madison", "I House",
"Dakota", "1959", "2109 F", "Fulbright", "Munson", "FSK"), class = "factor"),
Total = c(286, 262, 138, 118, 95, 85, 83, 76, 72, 69, 67,
67, 64, 60, 56, 52, 44, 43, 42, NA), years = structure(c(1L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 2L, 2L), .Label = c("First Year", "Upper-Class"), class = "factor")), class = "data.frame", row.names = c(NA,
-20L))
library(ggplot2)
ggplot(fall5, aes(Hall, Total, width = .9)) +
geom_col(aes(fill = Total), position = "dodge2", color = years)+
guides(fill=FALSE) +
theme(axis.text.x=element_text(angle=45, hjust = 1))
我希望看到与上面相同的图,但每一列都是基于“年”的两种颜色之一。此外,注明颜色差异的图例将很有用。
解决方案
library(ggplot2)
ggplot(fall5, aes(Hall, Total, width = .9)) +
geom_col(aes(fill = years), position = "dodge2")+
scale_fill_manual(values = c("First Year" = "red",
"Upper-Class" = "orange")) +
theme(axis.text.x=element_text(angle=45, hjust = 1))
推荐阅读
- java - 如何使用 Java 重定向到同一选项卡中的 URL,
- javascript - 如何检查链接中是否包含“/-s”?
- excel - 数据透视表在 1 行中显示不准确的数据
- amazon-s3 - 我如何使用在特定时间后创建的 pyspark 从 s3 读取文件
- amazon-web-services - 使用 AppDynamics 监控 EKS 集群
- gcc - Gcc 编译错误 /usr/bin/ld: 在 centos8 上找不到 -lmysqlclient
- wordpress - 如何获得 Woocommerce 可变产品价格?
- flutter - 如何在更好的播放器中获取当前位置并在应用关闭时保存它
- signal-processing - 有没有办法在不使用互联网的情况下通过传感器与智能手机远程通信?
- keycloak - 使用 keycloak 的 admin-cli 进行部分导出时,如何包含客户端和角色/组?(单次调用,如 UI)