首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在R中使用图案或纹理填充堆叠条形图

如何在R中使用图案或纹理填充堆叠条形图
EN

Stack Overflow用户
提问于 2015-05-12 22:24:54
回答 1查看 6.6K关注 0票数 2

我已经使用ggplot2绘制了一个堆叠的条形图,并且我想用图案填充条形图。但是这个问题似乎是由ggplot2来解决的。

那么,有没有一种方法可以用base R或另一个R包来填充堆叠的条形图呢?

我的图类似于这个柱状图:

我想让柱状图看起来像这样,填充图案或纹理:

我的数据来自my previous post

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-05-13 21:55:01

所需的大部分工作是使您的数据整齐。函数?barplot使用起来很简单,但是您需要向它提供一个矩阵。可以使用density=angle=参数的矢量来区分堆叠条形图的元素。

代码语言:javascript
复制
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)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30193799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档