r - 如何使用 ggplot 制作一帧中物种丰富度和多样性的条形图

标签 r ggplot2 bar-chart

我还是 R 的新手,我有一些关于使用 ggplot 的问题和帮助。

我一直在使用 spadeR 来获取每个采样位置的物种丰富度和多样性,并且我想为我的数据可视化制作条形图。但我在获取 ggplot 的正确代码时遇到了一些麻烦。

这是我想要的数据可视化的示例。 enter image description here

但是我的条形图看起来像这样

enter image description here

我想在框架顶部添加图例,我尝试添加它,但结果非常糟糕。

谁能告诉我如何使用 ggplot 解决这个问题,以及如何在一帧中制作 2 个条形图(如上面的示例),如何使用 parfrow?希望有人能教我如何解决这个问题。非常感谢!

这里是我在 10 个采样地点的物种丰富度数据集,包括物种丰富度的估计得分和标准误差。

datalw <- as.matrix(data.frame(Bng = c(8, 0.4),
                               Krs= c(3, 0),
                               Bny= c(3, 0),
                               Kmb= c(9.1, 7.40),
                               Sgk= c(3, 0.3),
                               Lwb= c(6.4, 1.0),
                               Lws= c(4.3, 0.7),
                               Krm= c(3, 0.5),
                               Hrt= c(7, 0.5),
                               Gmb= c(6.5, 1.0)))
rownames(datalw) <- c("Estimates", "s.e")
datalw                                                  

barplot(datalw,                                         
        col = c("#1b98e0", "#353436"))
legend("top",                                    
       legend = c("estimates", "s.e"),
       fill = c("#1b98e0", "#353436"))

最佳答案

我不得不稍微修改一下你的数据。你不必制作datalw一个矩阵,因为它最终会引起问题。您的数据还具有多列而不是多行,因此我为您重新格式化了您的数据。

datalw <- structure(list(loc = structure(c(1L, 7L, 2L, 5L, 10L, 8L, 9L, 
6L, 4L, 3L), .Label = c("Bng", "Bny", "Gmb", "Hrt", "Kmb", "Krm", 
"Krs", "Lwb", "Lws", "Sgk"), class = "factor"), V1 = c(8, 3, 
3, 9.1, 3, 6.4, 4.3, 3, 7, 6.5), V2 = c(0.4, 0, 0, 7.4, 0.3, 
1, 0.7, 0.5, 0.5, 1)), class = "data.frame", row.names = c(NA, 
-10L))

由于您希望并排显示条形图,因此可以将这些数据融合在一起以更轻松地绘制数据

library(ggplot2)
library(ggpattern)
library(reshape2)

datalw2 <- melt(datalw, id.vars='loc')

有一种方法可以用 ggplot 制作图案您可以使用ggpattern

ggplot(datalw2,aes(x=loc, y=value, fill=variable, group=variable, pattern = variable)) +
  geom_bar_pattern(stat='identity', position='dodge') +
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch"))

这将产生这样的情节。有更高级的方法可以更改图片中的图案,但是,您必须自己创建图案,而不是使用 ggpattern 中的默认图案。

example1

您的数据没有足够的信息来创建图片中显示的误差线

你也可以像这样将情节设置为黑白

ggplot(datalw2,aes(x=loc, y=value, fill=variable, group=variable, pattern = variable)) +
  geom_bar_pattern(stat='identity', position='dodge') +
  theme_bw() + 
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch")) +
  scale_fill_grey(start = .9, end = 0)

example

有一种方法可以创建像您的图片一样的并排图,但是您也没有足够的数据来制作第二个图

如果您想将误差线添加到图表中。您可以使用geom_errorbar 。使用您在下面的评论中提供的数据

datadv <- structure(list(caves = structure(c(1L, 7L, 2L, 5L, 10L, 8L, 9L,  6L, 4L, 3L),  .Label = c("Bng", "Bny", "Gmb", "Hrt", "Kmb", "Krm", "Krs", "Lwb", "Lws", "Sgk"), class = "factor"), Index = c(1.748, 0.022, 1.066, 1.213, 0.894, 0.863, 1.411, 0.179, 1.611, 1.045),  Std = c(0.078, 0.05, 0.053, 0.062, 0.120, 0.109, 0.143, 0.072, 0.152, 0.171)),  class = "data.frame", row.names = c(NA,-10L))

library(ggpattern)
library(ggplot2)
ggplot(datadv,aes(x=caves, y=Index)) +
  geom_bar_pattern(stat='identity', position='dodge') +
  theme_bw() + 
  scale_pattern_manual(values = c(V1 = "stripe", V2 = "crosshatch")) +
  scale_fill_grey(start = .9, end = 0) +
  geom_errorbar(aes(ymin=Index-Std, ymax=Index+Std), width=.2,
                position=position_dodge(.9))

example3

关于r - 如何使用 ggplot 制作一帧中物种丰富度和多样性的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70172714/

相关文章:

用于绘制带有时间线的条形图的 Javascript 库

r - 如何制作带有嵌套分组变量的堆叠条形图?

python - 改变100%堆积条形图中的颜色python

r - 从矩阵中提取后访问丢失的列名称

r - 如何在成对相关散点图中包含密度着色

r - 错误: could not find function "unit"

r - 如何在 R 中的 PAM 中获取主成分数据

r - R 中的 CVX 式凸优化?

r - 在ggplot中设置注释文本的宽度

r - ggplot2:在 R 中创建视觉直观的时间线