Support
Quality
Security
License
Reuse
kandi has reviewed oxbow and discovered the below as its top functions. This is intended to give you an instant insight into oxbow implemented functionality, and help decide if they suit your requirements.
Get all kandi verified functions for this library.
Get all kandi verified functions for this library.
QUESTION
Adding error bars to a clustered bar graph in R with ggplot
Asked 2020-Jan-13 at 22:55I am trying to create a clustered bar graph with error bars for my independent variables (y axis) for each of my dependent variables (metrics on the x axis).
My data:
df <- data.frame (Parameter_Estimate=c('Burnham','Calumet','Northerly','Orland','Hickory Hammock','Lake O','MacArthur','Site E','Corrales','Oxbow','Tingley','Galilee','Jacobs Point','Prudence Island','Town Pond','Trial 1','Trial 2','2017','2018','Spring','Summer','0.5m','1m','Chicago','Florida','New Mexico','Rhode Island'),
Species.Richness=c(79.4, -20.6, -12.6, -4, -63.15, -66.4, -69.15, -70.65, -52.07, -36.07, -67.23, -67.98, -69.9, -70.53, -74.73, 30.877, -2.743, 29.346, 0.9053, 29.721, -0.266, 28.898, 2.697, 70.1, -58.04, -42.49, -61.51),
FQI=c(29.272,-0.271,-1.095,-2.652,-20.24,-19.66,-21.2,-22.56,-10.36,-5.158,-18.66,-8.802,-8.431,-7.372,-15.25,18.474,-0.14,17.869,2.1378,17.809,0.9189,18.155,1.004,28.268,-19.91,-10.39,-9.196),
MeanC=c(3.294,0.4964,0.2252,-0.212,-0.631,-0.299,-0.339,-0.758,0.3185,0.3831,-0.135,2.7739,3.5218,4.089,3.1736,4.0308,0.1061,4.003,0.3103,4.0638,0.0266,4.0943,-0.052,3.4215,-0.634,0.0615,3.1985),
Per.Non.Nat=c(11.912,-3.756,7.666,10.192,14.888,71.874,-4.33,-3.784,-6.288,9.517,3.31,-10.91,-11.65,-11.88,-6.388,16.086,-1.423,14.33,4.372,12.659,4.25,15.582,-0.702,15.437,16.136,-1.346,13.58),
Shannon= c(3.6465,-0.449,-0.216,-0.4,-2.219,-2.897,-2.712,-2.504,-1.507,-0.798,-2.428,-2.066,-2.233,-2.528,-3.289,1.9313,-0.052,1.8939,0.268,1.8433,0.0975,1.8852,0.086,3.3804,-2.317,-1.311,-2.263),
Weight.MeanC=c(3.3643,1.0304,0.4379,-0.428,-1.906,-2.679,-1.352,-1.106,1.8142,0.7625,0.1685,4.6151,5.1463,4.3978,3.6472,4.3178,0.0385,4.4046,-0.273,4.3543,-0.028,4.3357,-0.011,3.6243,-2.021,0.6551,4.1965),
SE1=c(2.697,3.814,3.814,3.814,4.045,4.045,4.045,4.045,3.21,3.21,3.21,3.21,3.21,3.438,3.21,12.194,1.89,12.185,2.2041,12.299,2.0314,12.394,2.271,2.249,3.374,2.805,2.713))
Where SE1 is the standard error for each of my independent variables' data points for Species Richness (I haven't had the chance to add the other SE values for my other metrics, I'm just trying to experiment with the one for now).
I've created a figure using this code:
mm <- melt (df, id.vars='Parameter_Estimate')
ggplot (mm, aes (x=Parameter_Estimate, y=value) + geom_bar (stat='identity') + coord_flip()+facet_grid (.~variable) + labs (x=',y=')+scale_x_discrete (limits=df$Parameter_Estimate) +
geom_errorbarh (data=df, aes (y=Species.Richness, xmin=SE1, xmax=SE1), size=5, color="blue", inherit.aes = FALSE)
The barplot is not correct (SE1 is now one of the metrics along the x axis), and the error bars are not following my data for Species Richness.
Figure:
I'm unsure of what I'm doing wrong with the error bars and how to fix it!
ANSWER
Answered 2020-Jan-13 at 22:55You can remove the extra SE1 column by excluding it from melt:
mm <- melt (df[,-ncol(df)], id.vars='Parameter_Estimate')
Then for the error bar to appear in the Species Richness facet, you need to specify variable for it, with same levels as mm above:
se_df = df[,c("Parameter_Estimate","Species.Richness","SE1")]
se_df$variable="Species.Richness"
se_df$variable=factor(se_df$variable,levels=levels(mm$variable))
Now we plot:
ggplot (mm, aes (x=Parameter_Estimate, y=value)) +
geom_bar (stat='identity') +
facet_grid (~variable) + labs (x='',y='')+
geom_errorbar(data=se_df, inherit.aes=FALSE,aes (x=Parameter_Estimate,
ymin=Species.Richness-SE1, ymax=Species.Richness+SE1),
size=1, color="blue")+
scale_x_discrete(limits=df$Parameter_Estimate)+
coord_flip()
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Find more information at:
Save this library and start creating your kit
See Similar Libraries in
Save this library and start creating your kit
Open Weaver – Develop Applications Faster with Open Source