Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R中的ggplot2为叠层图指定颜色,在叠层图中,相同的叠层截面在面板中具有不同的排序_R_Ggplot2_Facet Wrap - Fatal编程技术网

使用R中的ggplot2为叠层图指定颜色,在叠层图中,相同的叠层截面在面板中具有不同的排序

使用R中的ggplot2为叠层图指定颜色,在叠层图中,相同的叠层截面在面板中具有不同的排序,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我试图在下图中为AMATACHEALDIGSASETSPSOLPT和其他(植物物种代码)应用特定颜色。原因是我有三个三年的数据集,我需要在所有三张图上对同一物种使用相同的颜色 这是一年的数据 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

我试图在下图中为
AMATA
CHEAL
DIGSA
SETSP
SOLPT
其他
(植物物种代码)应用特定颜色。原因是我有三个三年的数据集,我需要在所有三张图上对同一物种使用相同的颜色

这是一年的数据

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")) +
  facet_wrap( ~ Rot)

现在,为了获得正确的颜色,
物种
需要强制因子以正确的顺序给出等级:

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()

物种名称感谢您的时间和详细解释。您的解释对我的其他编码工作也有帮助。祝你今天愉快。@LittleBee我很高兴你发现我的解释很有帮助。
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()