Last active
September 7, 2017 14:02
-
-
Save valentinitnelav/fc78767fd5068ece1aaf2658d7341740 to your computer and use it in GitHub Desktop.
Colored confidence lines & 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 lines and setting order of line types in legend based on group - ggplot. | |
# ====================================================================================== | |
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)) + | |
# add the sin.cos lines | |
geom_line(aes(colour = Function)) + | |
# add "confidence" lines | |
geom_line(aes(y = lowerCI, colour = paste0('CI - ', Function)), | |
linetype = 'dotted', lwd=.5) + | |
geom_line(aes(y = upperCI, colour = paste0('CI - ', Function)), | |
linetype = 'dotted', lwd=.5) + | |
# set color manually | |
scale_colour_manual(name = 'Function', | |
breaks = c('cos', 'sin', 'CI - cos', 'CI - sin'), | |
values = c('cos' = 'red', | |
'sin' = 'blue', | |
'CI - cos' = 'red', | |
'CI - sin' = 'blue')) + | |
# override line type in leged so that CI lines are porperly represented | |
guides(colour = guide_legend(override.aes = list(linetype = c(rep('solid', 2), | |
rep('dotted',2))))) + | |
# 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 lines & legend - ggplot, color.pdf", width=10, height=9, units="cm") | |
# save as png | |
ggsave("Confidence lines & legend - ggplot, color.png", width=10, height=9, units="cm", dpi=300) | |
# addapted from: | |
# http://stackoverflow.com/questions/41743356/ggplot2-adding-lines-of-same-color-but-different-type-to-legend |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment