首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >堆栈条形图作为每个类的观测值百分比的函数,而不更改ggplot2中的值

堆栈条形图作为每个类的观测值百分比的函数,而不更改ggplot2中的值
EN

Stack Overflow用户
提问于 2020-05-11 19:52:32
回答 1查看 22关注 0票数 0

早上好,各位,

我有一个关于我的图表格式的问题。

Orignal graph

在这里,我将平均群体大小表示为离海岸距离的函数。是否可以将每一列除以每类小时的观察数的百分比,同时保持初始列的大小代表平均值。

下面是我的数据:

代码语言:javascript
运行
复制
 dput(droplevels(df.long2[1:15, ]))
structure(list(Distance = c("1-40", "1-40", "1-40", "40-80", 
"40-80", "40-80", "80-120", "80-120", "80-120", "120-160", "120-160", 
"120-160", "160-225", "160-225", "160-225"), mean = c(6.66901408450704, 
6.66901408450704, 6.66901408450704, 6.33333333333333, 6.33333333333333, 
6.33333333333333, 10.2561403508772, 10.2561403508772, 10.2561403508772, 
11.3986013986014, 11.3986013986014, 11.3986013986014, 23.7051282051282, 
23.7051282051282, 23.7051282051282), erreur_std = c(0.63121621161232, 
0.63121621161232, 0.63121621161232, 0.469878994871701, 0.469878994871701, 
0.469878994871701, 1.29468464273019, 1.29468464273019, 1.29468464273019, 
1.53421016593719, 1.53421016593719, 1.53421016593719, 4.00121147880924, 
4.00121147880924, 4.00121147880924), count = c(142L, 142L, 142L, 
312L, 312L, 312L, 285L, 285L, 285L, 143L, 143L, 143L, 78L, 78L, 
78L), Heure = c("0-4", "4-8", "8-12", "0-4", "4-8", "8-12", "0-4", 
"4-8", "8-12", "0-4", "4-8", "8-12", "0-4", "4-8", "8-12"), n = c(48L, 
79L, 15L, 131L, 148L, 33L, 85L, 152L, 48L, 83L, 51L, 9L, 56L, 
11L, 11L)), row.names = c(NA, -15L), class = c("tbl_df", "tbl", 
"data.frame"))

但不幸的是,当我试图绘制这张图时,我得到了这个结果,因为线条累积了

graph with error

下面是我使用的脚本:

代码语言:javascript
运行
复制
ggplot(df.long2, aes(x=Distance, y = mean, fill = Heure)) +
  geom_col(position = "stack", fill='steelblue', color="gray", stat="identity")+
  geom_errorbar(data = df.long2, aes(ymin = mean-erreur_std, ymax = mean+erreur_std), width = .2, position = position_dodge(width = 0.9))+
  theme_bw() +
  scale_x_discrete(limits=c("1-40", "40-80", "80-120", "120-160", "160-225")) +
  labs(title = "Moyenne de la taille des groupes chez le dauphin commun \n(Delphinus delphis) en fonction de la distance à la côte ", 
       caption = "Source : Observatoire PELAGIS ",
       x = "Distance à la côte (kilomètres)",
       y = "Moyenne de la taille des groupes",
       subtitle = "n=960") +
  theme(plot.title = element_text(hjust = 0.5)) +
  geom_text(aes(label=count), y=-0.5, hjust = 0.1, stat='count', colour="black", size=3) +
  geom_text(aes(label= "n=" ), y= -0.5, hjust = 1.1, colour="black", size = 3)

提前感谢您的回复

EN

回答 1

Stack Overflow用户

发布于 2020-05-11 20:17:24

为了表示柱状图,我建议预先计算您想要表示的值。尝试获取将在您的图表中表示的精确值的表。不要让ggplot为你做一些计算。在您的示例中,这将类似于:

代码语言:javascript
运行
复制
library(ggplot2)
library(dplyr)

df.long3 <- df.long2 %>% 
  group_by(Distance) %>% 
  summarise(
    mean = mean(mean),
    erreur_std = mean(erreur_std)
  )

ggplot(df.long3, aes(x=Distance, y = mean)) +
  geom_col(position = "stack", fill='steelblue', color="gray")+
  geom_errorbar(data = df.long2, aes(ymin = mean-erreur_std, ymax = mean+erreur_std), width = .2, position = position_dodge(width = 0.9))+
  theme_bw()

然而,我对此有两点担忧。

  1. 在您的数据集中,meanerreur_std对每个Hour使用完全相同的值重复。我怀疑这个数据集的计算错误。我假设您在之前的摘要calculation.
  2. What中设置了一个“小时”组,我们称之为“条形图”,但它并不能很好地表示您的数据。如果您不知道数据集的分布,这样的误差栏是没有意义的。我建议使用小提琴图,它不会假设分布的对称性。这样的“条形图”让我们假设你想隐藏原始数据的真实情况。

我不能向您提供小提琴图的代码,因为您没有提供原始数据,但是要获得更多信息,您可以浏览这两篇文章:

代码语言:javascript
运行
复制
- rtask.thinkr.fr (FR & EN) : [https://rtask.thinkr.fr/data-visualisation-pitfalls-how-to-avoid-barbarplots/](https://rtask.thinkr.fr/data-visualisation-pitfalls-how-to-avoid-barbarplots/)
- data-to-viz.com (EN): [https://www.data-to-viz.com/caveat/error\_bar.html](https://www.data-to-viz.com/caveat/error_bar.html)

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

https://stackoverflow.com/questions/61729079

复制
相关文章

相似问题

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