我正在尝试找到在R中创建显示标准错误的条形图的最佳方法.我看过其他文章,但无法弄清楚用于我自己的数据的代码(以前没有使用过ggplot,这似乎是最常用的方式,并且barplot不与数据帧配合使用).在两种情况下,我需要使用它来创建两个示例数据框:
对df1进行绘制,以便x轴具有站点ac,y轴显示V1的平均值并突出显示标准误差,类似于此
用于df2图
#按时间和站点分组数据grouped_df2< -group_by(df2,when,site)#汇总分组数据并使用功能mean和std.error计算平均值和标准误差summarised_df2< -summarise_each(grouped_df2,funs(平均值=平均值,std_error = std.error))#定义错误栏的顶部和底部限制<-aes(ymax =平均值+ std_error,ymin = mean-std_error)#开始您的ggplot#这里我们绘制站点与平均值的关系图,并在出现以下情况时用另一个因子变量填充g< -ggplot(summarised_df2,aes(site,mean,fill = when))#创建栏以显示因子变量position_dodge#确保并排创建因子栏g< -g + geom_bar(stat ="identity",position = position_dodge())#创建错误栏g< -g + geom_errorbar(限制,宽度= 0.25,位置= position_dodge(宽度= 0.9))#print图形G
I am trying to find the best way to create barplots in R with standard errors displayed. I have seen other articles but I cannot figure out the code to use with my own data (having not used ggplot before and this seeming to be the most used way and barplot not cooperating with dataframes). I need to use this in two cases for which I have created two example dataframes:
Plot df1 so that the x-axis has sites a-c, with the y-axis displaying the mean value for V1 and the standard errors highlighted, similar to this example with a grey colour. Here, plant biomass should the mean V1 value and treatments should be each of my sites.
Plot df2 in the same way, but so that before and after are located next to each other in a similar way to this, so pre-test and post-test equate to before and after in my example.
x <- factor(LETTERS[1:3]) site <- rep(x, each = 8) values <- as.data.frame(matrix(sample(0:10, 3*8, replace=TRUE), ncol=1)) df1 <- cbind(site,values) z <- factor(c("Before","After")) when <- rep(z, each = 4) df2 <- data.frame(when,df1)Apologies for the simplicity for more experienced R users and particuarly those that use ggplot but I cannot apply snippets of code that I have found elsewhere to my data. I cannot even get enough code together to produce a start to a graph so I hope my descriptions are sufficient. Thank you in advance.
解决方案I used group_by and summarise_each function for this and std.error function from package plotrix
library(plotrix) # for std error function library(dplyr) # for group_by and summarise_each function library(ggplot2) # for creating ggplotFor df1 plot
# Group data by when and site grouped_df1<-group_by(df1,site) #summarise grouped data and calculate mean and standard error using function mean and std.error(from plotrix) summarised_df1<-summarise_each(grouped_df1,funs(mean=mean,std_error=std.error)) # Define the top and bottom of the errorbars limits <- aes(ymax = mean + std_error, ymin=mean-std_error) #Begin your ggplot #Here we are plotting site vs mean and filling by another factor variable when g<-ggplot(summarised_df1,aes(site,mean)) #Creating bar to show the factor variable position_dodge #ensures side by side creation of factor bars g<-g+geom_bar(stat = "identity",position = position_dodge()) #creation of error bar g<-g+geom_errorbar(limits,width=0.25,position = position_dodge(width = 0.9)) #print graph gFor df2 plot
# Group data by when and site grouped_df2<-group_by(df2,when,site) #summarise grouped data and calculate mean and standard error using function mean and std.error summarised_df2<-summarise_each(grouped_df2,funs(mean=mean,std_error=std.error)) # Define the top and bottom of the errorbars limits <- aes(ymax = mean + std_error, ymin=mean-std_error) #Begin your ggplot #Here we are plotting site vs mean and filling by another factor variable when g<-ggplot(summarised_df2,aes(site,mean,fill=when)) #Creating bar to show the factor variable position_dodge #ensures side by side creation of factor bars g<-g+geom_bar(stat = "identity",position = position_dodge()) #creation of error bar g<-g+geom_errorbar(limits,width=0.25,position = position_dodge(width = 0.9)) #print graph g更多推荐
使用R中绘制的标准错误创建条形图
发布评论