Last active
September 7, 2017 14:03
-
-
Save valentinitnelav/becf4704c0eef0180546f958e453fc1e to your computer and use it in GitHub Desktop.
Colored confidence bands & set order in legend - ggplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ====================================================================================== | |
# Color case. | |
# Confidence bands and setting order of line types in legend based on group - ggplot. | |
# The lower and upper values could be computed through bootstrapping. | |
# ====================================================================================== | |
library(ggplot2) | |
# create some data | |
x=seq(from=0, to=2*pi, by=0.1) | |
my.data <- data.frame(x = rep(x,2), | |
sin.cos = c(sin(x),cos(x)), | |
lowerCI = c(sin(x),cos(x)) - 0.2, | |
upperCI = c(sin(x),cos(x)) + 0.2, | |
Function = rep(c("sin","cos"), each=63)) | |
# plot | |
ggplot(my.data, aes(x = x, y = sin.cos)) + | |
# plot CI-s as a ribbon arround each line | |
geom_ribbon(aes(ymin = lowerCI, ymax = upperCI, fill=Function), alpha=0.4) + | |
# set manually the fill color for ribbons | |
scale_fill_manual(name = 'Function', | |
breaks = c('cos', 'sin'), | |
values = c('cos' = 'red', | |
'sin' = 'blue')) + | |
# add the average lines on top of CI ribbons | |
geom_line(aes(color=Function), lwd=.8, linetype='solid') + | |
# set manually the color of lines | |
scale_color_manual(name = 'Function', | |
breaks = c('cos', 'sin'), | |
values = c('cos' = 'red', | |
'sin' = 'blue')) + | |
# NOTE: to set the order in legend as desired one needs to mention that in breaks=() | |
# Final adjustments (optional) | |
# set axis labels | |
labs(x = "x", | |
y = "sin & cos") + | |
# 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-left corner of legend box in lower-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,0), | |
legend.position = c(0,0)) | |
# save as pdf | |
ggsave("Confidence bands & legend - ggplot, color.pdf", width=10, height=9, units="cm") | |
# save as png | |
ggsave("Confidence bands & legend - ggplot, color.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