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

标签 r textures bar-chart fill

我已经使用 ggplot2 绘制了堆叠条形图,并且我想用模式填充条形图。但问题好像是very complicated由ggplot2解决。

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

我的情节与此条形图类似: enter image description here

我希望条形图看起来像这样,填充图案或纹理: 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

最佳答案

大部分所需的工作是使数据成形。函数?barplot使用起来很简单,但是你想给它提供一个矩阵。您可以使用密度= 和角度= 参数的向量来区分堆积条形图的元素。

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

关于r - 如何在 R 中用图案或纹理填充堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193799/

相关文章:

r - 基于另一列的多行创建一个新列

R 颜色 - 许多独特的颜色仍然很漂亮

python - 水平堆积条形图并为每个部分添加标签

r - 数据帧的独立行的总和以递增的方式

r - 带 Plotly 的树形图 : Blank Screen?

sprite-kit - 如何确定 SKTexture 使用@2x 还是@3x 图像?

java - 有纹理的三角形,仅渲染一种颜色

opengl - sRGB纹理。这样对吗?

gnuplot - Gnuplot:如何用彩色背景和图案填充条形图

java - MPAndroidChart BarChart(分组数据集)未显示所有条形图