如何在百分比条形图中显示平均值和误差线 [英] How to display the mean value and error bars in a percent bar graph
问题描述
我一直试图制作百分比条形图来显示两种蝌蚪在三种不同背景下的死亡率(用于伪装研究)
我的数据集是:
I've been trying to make a percent bar chart to show mortality of two species of tadpoles on three different backgrounds (for a camouflage study) My dataset is:
[[1]]
campanha fundo sobreviventes especie intactos percentsob
1 5 light 10 Bs 9 66.66667
2 5 mixed 8 Bs 5 53.33333
3 5 dark 8 Bs 8 53.33333
4 6 light 15 Bs 13 100.00000
5 6 mixed 15 Bs 11 100.00000
6 6 dark 14 Bs 11 93.33333
7 5 light 7 Sm 5 46.66667
8 5 mixed 10 Sm 9 66.66667
9 5 dark 12 Sm 10 80.00000
10 6 light 14 Sm 6 93.33333
11 6 mixed 14 Sm 6 93.33333
12 6 dark 15 Sm 9 100.00000
我的脚本是(文件名= odonatapint,15 =总数)
and my script is (file name=odonatapint, and 15 = total number of individuals used per trial):
odonatapint$percentsob<-odonatapint$sobreviventes*100/15
ggplot(data=odonatapint,aes(x=fundo,y=percentsob,fill=especie)) +
geom_bar(method="mean",stat="identity",position="dodge") +
scale_fill_manual(values=c("#999999", "#000000")) +
xlab("Background type (natural)") +
ylab("Tadpoles surviving (%)")
然而我注意到图表显示了每个类别的最高值,而不是平均值(我试图发布图表,但我没有被允许,因为我刚刚注册)。
However I noticed the graph to show the highest value for each category instead of the mean (I tried to post the graph but I wasn't allowed because I just registered).
我应该怎么做才能解决它?如何在显示平均值后添加误差线?
What should I do to fix it? And how can I add error bars after I get to display the mean value?
推荐答案
这就是我所做的,首先计算平均值和标准偏差(对于误差线)
This is what I did, first calculate the mean and standard deviation (for error bars)
library(dplyr)
odonatapint <- odonatapint %>% group_by(fundo,especie) %>% mutate(mean = mean(percentsob), sd = sd(percentsob))
$ b然后使用 geom_errorbar
添加错误栏<$ c geom_bar
>> / p>
then plot using ggplot (first plot bars using geom_bar
, then use geom_errorbar
to add error bars
ggplot(data=odonatapint,aes(x=fundo,y=mean,fill=especie)) +
geom_bar(method="mean",stat="identity",position=position_dodge()) +
geom_errorbar(aes(ymax = mean + sd, ymin = mean - sd), position = position_dodge()) +
scale_fill_manual(values=c("#999999", "#000000")) +
xlab("Background type (natural)") +
ylab("Tadpoles surviving (%)")
生成的图如下所示
这篇关于如何在百分比条形图中显示平均值和误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!