Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active March 7, 2017 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentinitnelav/cb7fa490e242d2b1a80a117a8844e44c to your computer and use it in GitHub Desktop.
Save valentinitnelav/cb7fa490e242d2b1a80a117a8844e44c to your computer and use it in GitHub Desktop.
Create a barplot for publication - ggplot.R
# ==========================================================================
# Barplot with CI/error bars for publication - ggplot
# see more at: http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/
# ==========================================================================
library(ggplot2)
# ==================================
# create some data
# ==================================
set.seed(1)
DF <- data.frame(Treatment = rep(c("control", "T1", "T2", "T1&2"), times=2),
Species = rep(c("Sp1", "Sp2"), each=4),
means = c(rnorm(n=4, mean=5),
rnorm(n=4, mean=6)))
# Note that this is fake data, this is not the way to statistically compute CI-s or SE-s
DF$lower.CI <- DF$means - rnorm(n=8, mean=.5, sd=.1)
DF$upper.CI <- DF$means + rnorm(n=8, mean=.5, sd=.1)
# ==================================
# Plot barplot with ggplot
# ==================================
ggplot(data = DF,
aes(x = Treatment,
y = means,
fill = Species)) +
# add the bars (means)
geom_bar(stat="identity", position=position_dodge()) +
# fill the bars manually
scale_fill_manual(name = 'Species:',
breaks = c("Sp1", "Sp2"),
values = c("Sp1" = "gray70",
"Sp2" = "gray40"),
labels = c("Species 1", "Species 2")) +
# plot CIs (add a point shape as well)
geom_errorbar(aes(ymax=upper.CI, ymin=lower.CI), size=.4, width=.15, linetype="solid", position=position_dodge(.9)) +
geom_point(position=position_dodge(width=.9), shape="-", show.legend = FALSE) +
# set order of discrete values on OX axes & adjust the distance (gap) from OY axes
scale_x_discrete(limits = c("control", "T1", "T2", "T1&2"),
labels = c("control", "Treatment 1", "Treatment 2", "Treatment 1 \n and Treatment 2"),
expand = c(0, .5)) +
# set range on OY axes and adjust the distance (gap) from OX axes
scale_y_continuous(limits = c(0, 10), expand = c(0, 0)) +
# Final adjustments:
# set axis labels
labs(x = "",
y = "Means") +
theme_bw() + # eliminate default background
theme(panel.grid.major = element_blank(), # eliminate major grids
panel.grid.minor = element_blank(), # eliminate minor grids
# set font family for all text within the plot ("serif" should work as "Times New Roman")
# note that this can be overridden with other adjustment functions below
text = element_text(family="serif"),
# adjust X-axis title
axis.title.x = element_text(size = 10, face = "bold"),
# adjust X-axis labels
axis.text.x = element_text(size = 10, face = "bold", color="black"),
# adjust Y-axis title
axis.title.y = element_text(size = 10, face = "bold"),
# adjust legend title appearance
legend.title = element_text(size = 8, face = "bold"),
# adjust legend label appearance
legend.text = element_text(size = 8, face = "italic"),
# change spacing between legend items
legend.key.height = unit(4, "mm"),
# don't draw legend box (check element_rect() for borders and backgrounds)
legend.background = element_blank(),
# Put upper-left corner of legend box in upper-left corner of graph
# Note that the numeric position in legend.position below is relative to the entire area,
# including titles and labels, not just the plotting area
legend.justification = c(0,1),
legend.position = c(0,1))
# save as pdf
ggsave("barplot with CI bars - ggplot.pdf", width=12, height=8, units="cm")
# save as png
ggsave("barplot with CI bars - ggplot.png", width=12, height=8, units="cm", dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment