Creating barplot with standard errors plotted in R

0 votes

I'm trying to create barplots in R with standard errors displayed. I have two data frames in my case.

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

Plot df2 in the same way, but so that before and after are located next to each other.

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)
Nov 12, 2018 in Data Analytics by Ali
• 10,290 points
46 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

For 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
g

For 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
answered Nov 12, 2018 by Maverick
• 10,000 points

Related Questions In Data Analytics

0 votes
1 answer

how to run a logistic regression with clustered standard errors in R?

have a look at rms package. lrm is logistic ...READ MORE

answered Nov 6, 2018 in Data Analytics by Kalgi
• 35,800 points
80 views
0 votes
1 answer

What are the options for deploying models in production with R?

Well, I could say that the answer ...READ MORE

answered Apr 12, 2018 in Data Analytics by DataKing99
• 8,100 points
132 views
0 votes
1 answer

How to plot side-by-side Plots with ggplot2 in R?

By Using gridExtra library we can easily ...READ MORE

answered Apr 16, 2018 in Data Analytics by DeepCoder786
• 1,700 points
946 views
0 votes
1 answer

What is the standard naming convention for the variables in R?

Use of period separator e.g. product.prices <- c(12.01, ...READ MORE

answered Apr 25, 2018 in Data Analytics by shams
• 3,580 points
15 views
0 votes
1 answer
0 votes
1 answer

Error saying cannot open file 'file.pdf' when trying to save a plot in ggplot

You could try adding the following line ...READ MORE

answered Nov 9, 2018 in Data Analytics by Maverick
• 10,000 points
196 views
0 votes
1 answer

Error saying "Error in df$item : object of type 'closure' is not subsettable" when trying to use arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", ...READ MORE

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,000 points
54 views
0 votes
1 answer
0 votes
1 answer

Error saying "Error in rnorm() : argument "n" is missing, with no default" in R

Hey @ali, rnorm() function requires an argument, ...READ MORE

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,000 points
83 views
0 votes
1 answer

R errors in eclipse

remove import android.R Then clean and rebuild project. R ...READ MORE

answered Nov 8, 2018 in Data Analytics by Maverick
• 10,000 points
20 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.