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

时间:2015-05-12 14:24:54

标签: r textures bar-chart fill

我用ggplot2画了一个堆积的条形图,我想用条纹填充条形图。但似乎问题是very complicated由ggplot2解决。

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

我的情节类似于这个条形图: enter image description here

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

我的数据来自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 个答案:

答案 0 :(得分:1)

大部分要求的工作是让您的数据保持良好状态。函数?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)

enter image description here