Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active January 21, 2017 19:30
Show Gist options
  • Save valentinitnelav/e244a8bb72964af6a8c0e094d4b58370 to your computer and use it in GitHub Desktop.
Save valentinitnelav/e244a8bb72964af6a8c0e094d4b58370 to your computer and use it in GitHub Desktop.
Confidence bands & set order in legend - ggplot
# ======================================================================================
# Confidence bands and setting order of line types in legend - ggplot
# ======================================================================================
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) +
# plot CI-s as a ribbon arround each line;
# initiate the filling (fill) with color/gray differently for each scenario and
# also adjust alpha parameter for - opacity/transparency
geom_ribbon(aes(x=time, ymin = lowerCI, ymax = upperCI, fill=scenario), alpha=0.6) +
# set manually the fill color for ribbons
scale_fill_manual(name = 'Scenario',
breaks = c("Z", "A"),
values = c("A" = "gray70",
"Z" = "gray70")) +
# add the average lines on top of CI ribbons
geom_line(aes(x=time, y=means, linetype=scenario), lwd=.8) +
# set manually the type of line
scale_linetype_manual(name = 'Scenario',
breaks = c("Z", "A"),
values = c("Z" = "solid",
"A" = "twodash")) +
# NOTE: to set the order in legend as desired one needs to mention that in breaks=()
# 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 bands & legend - ggplot.pdf", width=10, height=9, units="cm")
# save as png
ggsave("Confidence bands & 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