如何用R中的图案或纹理填充堆积的条形图

时间:2023-02-09 21:42:44

I have used ggplot2 to draw a stacked barplot, and I want to fill the barplot with patterns. But it seems that the problem is very complicated to be solved by ggplot2.

我用ggplot2绘制了一个堆积的条形图,我想用条纹填充条形图。但似乎ggplot2解决了这个问题非常复杂。

So is there a way to fill stacked barplot with patterns or textures with base R or with another R package?

那么有没有办法用基础R或另一个R包填充带有图案或纹理的堆积条形图?

My plot is similar to this barplot: 如何用R中的图案或纹理填充堆积的条形图

我的情节类似于这个条形图:

and I want the barplot looks like this, fill with patterns or textures: 如何用R中的图案或纹理填充堆积的条形图

我希望barplot看起来像这样,填充图案或纹理:

My data is from my previous post:

我的数据来自我以前的帖子:

plant   group   n   percentage
1   Cucumber-1  [3.19e-39,2]    14729   0.8667686695
2   Cucumber-1  (2,4]   1670    0.0982757606
3   Cucumber-1  (4,6]   447 0.0263049491
4   Cucumber-1  (6,8]   131 0.0077090567
5   Cucumber-1  (8,10]  16  0.0009415642
6   Cucumber-2  [3.19e-39,2]    20206   0.9410394933
7   Cucumber-2  (2,4]   1155    0.0537909836
8   Cucumber-2  (4,6]   90  0.0041915052
9   Cucumber-2  (6,8]   16  0.0007451565
10  Cucumber-2  (8,10]  5   0.0002328614
11  Eggplant-1  [3.19e-39,2]    11273   0.9012631916
12  Eggplant-1  (2,4]   960 0.0767508794
13  Eggplant-1  (4,6]   181 0.0144707387
14  Eggplant-1  (6,8]   31  0.0024784138
15  Eggplant-1  (8,10]  63  0.0050367765
16  Eggplant-2  [3.19e-39,2]    16483   0.9493721921
17  Eggplant-2  (2,4]   725 0.0417578620
18  Eggplant-2  (4,6]   140 0.0080635871
19  Eggplant-2  (6,8]   12  0.0006911646
20  Eggplant-2  (8,10]  2   0.0001151941
21  Pepper-1    [3.19e-39,2]    4452    0.9763157895
22  Pepper-1    (2,4]   97  0.0212719298
23  Pepper-1    (4,6]   11  0.0024122807
24  Pepper-2    [3.19e-39,2]    23704   0.9560763119
25  Pepper-2    (2,4]   905 0.0365022385
26  Pepper-2    (4,6]   184 0.0074214496

1 个解决方案

#1


Most of the required work is to get your data in shape. The function ?barplot is simple to use, but you want to feed it a matrix. You can use vectors for the density= and angle= arguments to distinguish the elements of the stacked bar plot.

大多数必要的工作是使您的数据保持良好状态。函数?barplot易于使用,但您想要将其作为矩阵。您可以使用density =和angle =参数的矢量来区分堆积条形图的元素。

d = read.table(text="plant   ...
                                   ...   184 0.0074214496", header=T)

d$group <- factor(d$group, levels=c(levels(d$group)[c(5,1:4)]),
                           labels=c("(0,2]", levels(d$group)[1:4]))
levels(d$group)
# [1] "(0,2]"  "(2,4]"  "(4,6]"  "(6,8]"  "(8,10]"
tab <- table(d$group, d$plant)
tab
# output omitted
d <- rbind(d,
           c("Pepper-1", "(6,8]",  0, 0),
           c("Pepper-1", "(8,10]", 0, 0),
           c("Pepper-2", "(6,8]",  0, 0),
           c("Pepper-2", "(8,10]", 0, 0) )
d <- d[order(d$plant, d$group),]
d
# output omitted
mat                  <- matrix(as.numeric(d$percentage), nrow=5, ncol=6)
rownames(mat)        <- levels(d$group)
colnames(mat)        <- levels(d$plant)
names(dimnames(mat)) <- c("group", "plant")
mat
#         plant
# group      Cucumber-1   Cucumber-2  Eggplant-1   Eggplant-2    Pepper-1   Pepper-2
#   (0,2]  0.8667686695 0.9410394933 0.901263192 0.9493721921 0.976315789 0.95607631
#   (2,4]  0.0982757606 0.0537909836 0.076750879 0.0417578620 0.021271930 0.03650224
#   (4,6]  0.0263049491 0.0041915052 0.014470739 0.0080635871 0.002412281 0.00742145
#   (6,8]  0.0077090567 0.0007451565 0.002478414 0.0006911646 0.000000000 0.00000000
#   (8,10] 0.0009415642 0.0002328614 0.005036777 0.0001151941 0.000000000 0.00000000

barplot(mat, density=5:9, angle=seq(40, 90, 10), cex.names=.8)

如何用R中的图案或纹理填充堆积的条形图

#1


Most of the required work is to get your data in shape. The function ?barplot is simple to use, but you want to feed it a matrix. You can use vectors for the density= and angle= arguments to distinguish the elements of the stacked bar plot.

大多数必要的工作是使您的数据保持良好状态。函数?barplot易于使用,但您想要将其作为矩阵。您可以使用density =和angle =参数的矢量来区分堆积条形图的元素。

d = read.table(text="plant   ...
                                   ...   184 0.0074214496", header=T)

d$group <- factor(d$group, levels=c(levels(d$group)[c(5,1:4)]),
                           labels=c("(0,2]", levels(d$group)[1:4]))
levels(d$group)
# [1] "(0,2]"  "(2,4]"  "(4,6]"  "(6,8]"  "(8,10]"
tab <- table(d$group, d$plant)
tab
# output omitted
d <- rbind(d,
           c("Pepper-1", "(6,8]",  0, 0),
           c("Pepper-1", "(8,10]", 0, 0),
           c("Pepper-2", "(6,8]",  0, 0),
           c("Pepper-2", "(8,10]", 0, 0) )
d <- d[order(d$plant, d$group),]
d
# output omitted
mat                  <- matrix(as.numeric(d$percentage), nrow=5, ncol=6)
rownames(mat)        <- levels(d$group)
colnames(mat)        <- levels(d$plant)
names(dimnames(mat)) <- c("group", "plant")
mat
#         plant
# group      Cucumber-1   Cucumber-2  Eggplant-1   Eggplant-2    Pepper-1   Pepper-2
#   (0,2]  0.8667686695 0.9410394933 0.901263192 0.9493721921 0.976315789 0.95607631
#   (2,4]  0.0982757606 0.0537909836 0.076750879 0.0417578620 0.021271930 0.03650224
#   (4,6]  0.0263049491 0.0041915052 0.014470739 0.0080635871 0.002412281 0.00742145
#   (6,8]  0.0077090567 0.0007451565 0.002478414 0.0006911646 0.000000000 0.00000000
#   (8,10] 0.0009415642 0.0002328614 0.005036777 0.0001151941 0.000000000 0.00000000

barplot(mat, density=5:9, angle=seq(40, 90, 10), cex.names=.8)

如何用R中的图案或纹理填充堆积的条形图