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
• 11,360 points
891 views

1 answer to this question.

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,840 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
• 52,360 points
1,487 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,720 points

edited Jun 9, 2020 by MD 8,496 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,670 points
451 views
0 votes
1 answer

How to print new lines with print() in R?

You can use cat() instead of writeLines(): ...READ MORE

answered May 3, 2018 in Data Analytics by kappa3010
• 2,090 points
578 views
0 votes
1 answer
+1 vote
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,840 points
7,542 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,840 points
1,414 views
+1 vote
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,840 points
2,970 views
+1 vote
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,840 points
712 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP