【发布时间】:2018-01-08 17:53:13
【问题描述】:
我一直在努力为 ggplot2 中的值手动分配颜色。忽略字母顺序手动分配颜色的最简单方法是什么?
这是我的数据框,其中有一个 colours 列,表示每个 Status 所需的颜色:
summary_count <– structure(list(Status = structure(c(4L, 6L, 1L, 5L, 2L, 3L), .Label = c("Compromise",
"Launched", "Not yet rated", "Promise broken", "Promise kept",
"Stuck"), class = "factor"), n = c(15L, 4L, 4L, 7L, 9L, 21L),
total = c(60, 60, 60, 60, 60, 60), colours = c("#B71C1C",
"#F57F17", "#2196F3", "#0D47A1", "#4CAF50", "#9E9E9E")), .Names = c("Status",
"n", "total", "colours"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
还有我的 ggplot 代码:
summary_count %>%
ggplot(aes(x = Status, y = n, fill = Status)) +
geom_col() +
coord_flip() +
geom_text(aes(label = n),
hjust = 1.5,
colour = "white",
fontface = "bold",
size = 3) +
scale_x_discrete(limits = rev(order)) +
scale_fill_manual(values = summary_count$colours) +
theme_minimal() +
theme(axis.text.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
legend.position = "null")
Promise broken 应该是红色,Stuck 应该是橙色等等...
【问题讨论】:
-
使用 scale_identity ggplot2.tidyverse.org/reference/scale_identity.html
-
感谢您使用
dput,但似乎存在语法错误,可能缺少(。你能仔细检查一下吗? -
另外,要让@wibeasley 建议起作用,您需要在
aes调用中设置fill = colours -
从
dput生成的数据框现在应该很好了。 -
很好
scale_identity完全成功了!