Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active January 14, 2018 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentinitnelav/190d70b481d854dd0c000a2efaf66ac1 to your computer and use it in GitHub Desktop.
Save valentinitnelav/190d70b481d854dd0c000a2efaf66ac1 to your computer and use it in GitHub Desktop.
Create a dotplot for publication - ggplot.R
# ==========================================================================
# Dotplot with CI/error bars for publication - ggplot
# see also: 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 dotplot with ggplot
# ==================================
# The errorbars will overlapped, so use position_dodge to move them horizontally
pd <- position_dodge(width = 0.5) # move them .05 to the left and right
ggplot(data = DF,
aes(x = Treatment, y = means, group = Species, shape=Species)) +
# add the points (means)
geom_point(size=1.5, position=pd) +
# set type of point shape
scale_shape_manual(name = 'Species:',
breaks = c("Sp1", "Sp2"),
values = c("Sp1" = 16,
"Sp2" = 17),
labels = c("Species 1", "Species 2")) +
# plot CIs
geom_errorbar(aes(ymax=upper.CI, ymin=lower.CI), size=.4, width=.15, linetype="solid", position=pd) +
# 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("dotplot with CI bars - ggplot.pdf", width=12, height=8, units="cm")
# save as png
ggsave("dotplot 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