作为每个类的观察百分比的函数,而无需更改ggplot2中的值

问题描述 投票:0回答:1

大家早上好,

我对图形的格式有疑问。

Orignal graph

这里我表示的是平均团体人数,它是与海岸的距离的函数。可以将每列除以每小时的观察次数百分比,同时保持初始列的大小代表平均值。

有我的数据:

 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

我有使用的脚本:

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)

提前感谢您的回复

r ggplot2 bar-chart geom-bar geom-col
1个回答
0
投票

为了表示柱状图,建议您事先计算要显示的值。尝试获取将在图形中表示的确切值的表。不要让ggplot为您做一些计算。在您的情况下,它将类似于:

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()

barbarplot

但是,我对此有两个担忧。

  1. 在您的数据集中,您重复了meanerreur_std,每个Hour的值都完全相同。我怀疑此数据集计算错误。我假设您在先前的摘要计算中让组“小时”。

  2. 我们所谓的“ barbarplot”是您数据的错误表示。如果您不知道数据集的分布,则这种错误栏是没有意义的。我会推荐一个小提琴图,它不假定您的分布对称。这样的“ barbarplot”假设您想隐藏原始数据的真实性。

    我无法为您提供小提琴绘图的代码,因为您没有提供原始数据,但是有关更多信息,您可以浏览以下两篇文章:

© www.soinside.com 2019 - 2024. All rights reserved.