我正在尝试建立一个复杂的条形图,其中有类别可供区分。这是数据框
Treatment DCA.f Megalorchestia Talitridae Traskorchestia
1 A (-Inf,0] 8.000000 4843.6667 1394.0000
2 U (-Inf,0] 21.000000 2905.3333 483.6667
3 A (0,0.1] 25.000000 254.8571 41.0000
4 U (0,0.1] 30.714286 691.0000 360.1429
5 A (0.1,0.2] 35.400000 1355.2000 127.4000
6 U (0.1,0.2] 104.400000 705.4000 50.2000
7 A (0.2,0.3] 3.857143 649.7143 633.4286
8 U (0.2,0.3] 10.857143 510.4286 268.7143
9 A (0.3,0.4] 13.444444 981.5556 207.5556
10 U (0.3,0.4] 10.666667 1567.5556 417.5556
11 A (0.4, Inf] 0.000000 3.0000 1.2000
12 U (0.4, Inf] 0.000000 3.8000 0.0000
我想要一个条形图,每个DCA.f组显示三个生物类别(右三列)的6个值,通过处理(A v U)分开。因此,如果您阅读图的底部,那么DCA.f会有一个很大的类别,然后在该类别中会有六个条形图。每个属的两个颜色通过处理编码。然后重复所有DAC.f.我查看了很多其他的barplot帖子,他们没有让我到任何地方。
任何帮助?
答案 0 :(得分:0)
这是一种可能性。使用barplot
时,输入矩阵的每个列将对应于一组条形,并且每个行对应于组内的不同条形。因此,我们需要重新整形数据,以便列代表'DCA.f'
library(reshape2)
library(RColorBrewer)
# reshape data
df2 <- melt(df)
df3 <- dcast(df2, Treatment + variable ~ DCA.f)
# create color palette
ncols <- length(unique(df3$variable))
cols <- c(brewer.pal(ncols, "Greens"), brewer.pal(ncols, "Reds"))
# plot
barplot(as.matrix(df3[ , -c(1, 2)]),
beside = TRUE,
col = cols,
cex.names = 0.7)
# add legend
legend(x = 15, y = 4000, legend = paste(df3$Treatment, df3$variable), fill = cols)