【发布时间】:2021-08-28 05:22:14
【问题描述】:
我需要帮助才能为 ggplot 对象(特别是 geom_bar)添加颜色。
这是我的数据
Names Family Groups Values
H.sapiens A G1 2
H.erectus A G1 6
H.erectus B G2 12
M.griseus C G2 3
A.mellifera D G3 3
L.niger D G3 8
H.erectus D G3 2
L.niger A G1 3
L.niger B G2 3
A.mellifera A G1 8
到目前为止,我成功地创建了这个情节:
使用此代码:
library(ggplot2)
library(ggstance)
library(ggthemes)
ggplot(table, aes(fill=Family, y=Names, x=Values)) +
geom_barh(stat="identity",colour="white")+ theme_minimal() +
scale_x_continuous(limits = c(0,60), expand = c(0, 0))
现在我想根据组更改颜色。更准确地说,我想为每个组选择一种主要颜色,例如:G1= blue ; G2 = Green ; G3= Red。
并为每个家庭在这些颜色中获得渐变。例如,B 是深蓝色,C 是浅蓝色。
有人有想法吗?
这是数据:
dput(table)
structure(list(Names = structure(c(3L, 2L, 2L, 5L, 1L, 4L, 2L,
4L, 4L, 1L), .Label = c("A.mellifera", "H.erectus", "H.sapiens",
"L.niger", "M.griseus"), class = "factor"), Family = structure(c(1L,
1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 1L), .Label = c("A", "B", "C",
"D"), class = "factor"), Groups = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 3L, 1L, 2L, 1L), .Label = c("G1", "G2", "G3"), class = "factor"),
Values = c(2L, 6L, 12L, 3L, 3L, 8L, 2L, 3L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-10L))
【问题讨论】:
-
在一张图表中使用多个色标,只能通过一些变通方法来实现。见this