Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active January 21, 2017 19:30
Show Gist options
  • Save valentinitnelav/25909d86ed007e56f0f13cdb76701e3f to your computer and use it in GitHub Desktop.
Save valentinitnelav/25909d86ed007e56f0f13cdb76701e3f to your computer and use it in GitHub Desktop.
Confidence lines & set order in legend - ggplot
# ======================================================================================
# Manually add confidence lines and setting order of line types in legend - ggplot
# The lower and upper values could be computed through bootstrapping.
# ======================================================================================
library(ggplot2)
# create some data
my.data <- data.frame(time = rep(1:10, 2),
means = 2:21,
lowerCI = 1:20,
upperCI = 3:22,
scenario = rep(c("A","Z"), each=10))
my.data
# plot
ggplot(data = my.data) +
# add the average lines
geom_line(aes(x=time, y=means, linetype=scenario), lwd=.8) +
# put "confidence" lines
geom_line(aes(x=time, y=lowerCI, linetype="CI", group=scenario), lwd=.5, show.legend=FALSE) +
geom_line(aes(x=time, y=upperCI, linetype="CI", group=scenario), lwd=.5, show.legend=FALSE) +
# set manually the type of line
scale_linetype_manual(name = 'Scenario',
breaks = c("Z", "A", "CI"),
values = c("Z" = "solid",
"A" = "twodash",
"CI" = "dotted")) +
# to set the order in legend as desired one needs to mention that in breaks=() in scale_linetype_manual()
# also set show.legend=FALSE in geom_line for CI-s so that it doesn’t overlap the legend corresponding to average liens.
# Note - using a factor for scenario column would not work as expected.
# Final adjustments (optional)
# set axis labels
labs(x = "Time",
y = "Population growth") +
# set number of axis ticks
scale_x_continuous(breaks=pretty(my.data$time, 10)) +
# eliminate default backgound
theme_bw() +
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 text in X-axis title
axis.title.x = element_text(size = 12, face = "bold"),
# adjust text in Y-axis title
axis.title.y = element_text(size = 12, face = "bold"),
# adjust legend title appearance
legend.title = element_text(size = 10, face = "bold"),
# adjust legend label appearance
legend.text = element_text(size = 10),
# don't draw legend box (check element_rect() for borders and backgrounds)
legend.background = element_blank(),
# Put lower-right corner of legend box in lower-right 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(1,0),
legend.position = c(1,0))
# save as pdf
ggsave("Confidence lines & legend - ggplot.pdf", width=10, height=9, units="cm")
# save as png
ggsave("Confidence lines & legend - ggplot.png", width=10, height=9, units="cm", dpi=300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment