在 R 中使用 ggplot2 为堆叠图指定颜色,其中相同的堆叠部分在面板之间具有不同的排名
Specifying color with ggplot2 in R for stacked graph in which the same stacked section bearing different ranking across panels
我正在尝试为 AMATA
CHEAL
DIGSA
SETSP
SOLPT
和 Others
(植物物种代码)应用特定颜色在下图中。原因是我有三年的三个数据集,我需要在所有三个图上对同一个物种使用相同的颜色。
这是一年的数据
data <-
structure(list(Rot = c("2-year", "2-year", "2-year", "2-year",
"2-year", "2-year", "2-year", "2-year", "2-year", "2-year", "2-year",
"2-year", "3-year", "3-year", "3-year", "3-year", "3-year", "3-year",
"3-year", "3-year", "3-year", "3-year", "3-year", "3-year", "4-year",
"4-year", "4-year", "4-year", "4-year", "4-year", "4-year", "4-year",
"4-year", "4-year", "4-year", "4-year"), Herb = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("conv", "low"), class = "factor"), species = c("AMATA",
"CHEAL", "DIGSA", "Others", "SETSP", "SOLPT", "AMATA", "CHEAL",
"DIGSA", "Others", "SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA",
"Others", "SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA", "Others",
"SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA", "Others", "SETSP",
"SOLPT", "AMATA", "CHEAL", "DIGSA", "Others", "SETSP", "SOLPT"
), m.adens = c(2197.4261496, 1192.447112475, 0, 18.7667669625,
104.09825015, 17.0234713875, 4660.7003427875, 3764.4214453625,
16.6470464875, 84.80790515, 492.4442869375, 16.9923696125, 2247.36022525833,
2307.16391086667, 0, 73.9299205416667, 262.936172, 186.0226796,
5495.27938680833, 9735.14487680833, 10.909839225, 360.878508416667,
2322.27422545833, 126.091509308333, 4969.48674180625, 1711.9130538625,
205.3436674125, 494.4264206125, 1254.0715623, 124.4742832125,
3825.15189476875, 3038.0082425, 181.47105726875, 163.71343195,
3379.4791432, 41.786807975)), .Names = c("Rot", "Herb", "species",
"m.adens"), row.names = c(NA, -36L), class = "data.frame")
为了着色,我使用了
ggplot(data, aes(x=Herb, y=m.adens, fill=species))+ geom_bar(stat="identity")+
scale_fill_manual(values = RColorBrewer::brewer.pal(n = 6, name = "Set1"),
breaks= c("AMATA","CHEAL","DIGSA","SETSP","SOLPT","Others"))
使用此代码,颜色被应用于正确的物种。我试图通过将 breaks
更改为 labels
来指定颜色。但是,使用 labels
不会正确地为物种着色。
labels
只是在图例面板中任意排列颜色,不匹配每个堆叠部分。请看看我的代码做错了什么。非常感谢任何帮助。
您在寻找切面吗?
ggplot(data, aes(x = Herb, y = m.adens, fill = species)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = RColorBrewer::brewer.pal(n = 6, name = "Set1"),
breaks = c("AMATA", "CHEAL", "DIGSA", "SETSP", "SOLPT", "Others")) +
facet_wrap( ~ Rot)
现在,为了得到正确的着色,species
需要被强制分解以正确的顺序给出级别:
species_names <- c("AMATA", "CHEAL", "DIGSA", "SETSP", "SOLPT", "Others")
data$species <- factor(data$species, levels = rev(species_names))
ggplot(data, aes(x = Herb, y = m.adens, fill = species)) +
geom_col() +
scale_fill_manual(values = rev(RColorBrewer::brewer.pal(n = 6, name = "Set1")),
breaks = species_names) +
facet_wrap( ~ Rot) +
theme_bw()
请注意rev()
在这里用了两次。第一次 rev()
用于将 AMATA
作为最后一个因子水平,因此它被绘制在堆积条的底部。第二次用于反转色标,使颜色再次与物种相匹配。
我正在尝试为 AMATA
CHEAL
DIGSA
SETSP
SOLPT
和 Others
(植物物种代码)应用特定颜色在下图中。原因是我有三年的三个数据集,我需要在所有三个图上对同一个物种使用相同的颜色。
这是一年的数据
data <-
structure(list(Rot = c("2-year", "2-year", "2-year", "2-year",
"2-year", "2-year", "2-year", "2-year", "2-year", "2-year", "2-year",
"2-year", "3-year", "3-year", "3-year", "3-year", "3-year", "3-year",
"3-year", "3-year", "3-year", "3-year", "3-year", "3-year", "4-year",
"4-year", "4-year", "4-year", "4-year", "4-year", "4-year", "4-year",
"4-year", "4-year", "4-year", "4-year"), Herb = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("conv", "low"), class = "factor"), species = c("AMATA",
"CHEAL", "DIGSA", "Others", "SETSP", "SOLPT", "AMATA", "CHEAL",
"DIGSA", "Others", "SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA",
"Others", "SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA", "Others",
"SETSP", "SOLPT", "AMATA", "CHEAL", "DIGSA", "Others", "SETSP",
"SOLPT", "AMATA", "CHEAL", "DIGSA", "Others", "SETSP", "SOLPT"
), m.adens = c(2197.4261496, 1192.447112475, 0, 18.7667669625,
104.09825015, 17.0234713875, 4660.7003427875, 3764.4214453625,
16.6470464875, 84.80790515, 492.4442869375, 16.9923696125, 2247.36022525833,
2307.16391086667, 0, 73.9299205416667, 262.936172, 186.0226796,
5495.27938680833, 9735.14487680833, 10.909839225, 360.878508416667,
2322.27422545833, 126.091509308333, 4969.48674180625, 1711.9130538625,
205.3436674125, 494.4264206125, 1254.0715623, 124.4742832125,
3825.15189476875, 3038.0082425, 181.47105726875, 163.71343195,
3379.4791432, 41.786807975)), .Names = c("Rot", "Herb", "species",
"m.adens"), row.names = c(NA, -36L), class = "data.frame")
为了着色,我使用了
ggplot(data, aes(x=Herb, y=m.adens, fill=species))+ geom_bar(stat="identity")+
scale_fill_manual(values = RColorBrewer::brewer.pal(n = 6, name = "Set1"),
breaks= c("AMATA","CHEAL","DIGSA","SETSP","SOLPT","Others"))
使用此代码,颜色被应用于正确的物种。我试图通过将 breaks
更改为 labels
来指定颜色。但是,使用 labels
不会正确地为物种着色。
labels
只是在图例面板中任意排列颜色,不匹配每个堆叠部分。请看看我的代码做错了什么。非常感谢任何帮助。
您在寻找切面吗?
ggplot(data, aes(x = Herb, y = m.adens, fill = species)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = RColorBrewer::brewer.pal(n = 6, name = "Set1"),
breaks = c("AMATA", "CHEAL", "DIGSA", "SETSP", "SOLPT", "Others")) +
facet_wrap( ~ Rot)
现在,为了得到正确的着色,species
需要被强制分解以正确的顺序给出级别:
species_names <- c("AMATA", "CHEAL", "DIGSA", "SETSP", "SOLPT", "Others")
data$species <- factor(data$species, levels = rev(species_names))
ggplot(data, aes(x = Herb, y = m.adens, fill = species)) +
geom_col() +
scale_fill_manual(values = rev(RColorBrewer::brewer.pal(n = 6, name = "Set1")),
breaks = species_names) +
facet_wrap( ~ Rot) +
theme_bw()
请注意rev()
在这里用了两次。第一次 rev()
用于将 AMATA
作为最后一个因子水平,因此它被绘制在堆积条的底部。第二次用于反转色标,使颜色再次与物种相匹配。