Created
August 3, 2018 01:38
-
-
Save TonyLadson/732e3dbcb8aeaf76fd25c04f6ff246b7 to your computer and use it in GitHub Desktop.
Better line graphs for hydrologic data. See the blog https://wordpress.com/post/tonyladson.wordpress.com/10384
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
###################################################################################### | |
# | |
# Better line graphs for hydrologic data | |
# | |
# | |
####################################################################################### | |
library(tidyverse) | |
library(scales) | |
library(RColorBrewer) | |
# Create a data frame with some example data | |
dog_ck <- tribble( | |
~creek_name, ~duration, ~peak_flow, | |
'Brown Dog Ck', 9, 1200, | |
'Brown Dog Ck', 12, 1300, | |
'Brown Dog Ck', 24, 1550, | |
'Brown Dog Ck', 36, 1420, | |
'Brown Dog Ck', 48, 1400, | |
'Brown Dog Ck', 72, 1500, | |
'Long Ck', 9, 100, | |
'Long Ck', 12, 100, | |
'Long Ck', 24, 100, | |
'Long Ck', 36, 70, | |
'Long Ck', 48, 60, | |
'Long Ck', 72, 60, | |
'Short Ck', 9, 700, | |
'Short Ck', 12, 620, | |
'Short Ck', 24, 800, | |
'Short Ck', 36, 800, | |
'Short Ck', 48, 820, | |
'Short Ck', 72, 900, | |
'Rocky Ck', 9, 400, | |
'Rocky Ck', 12, 520, | |
'Rocky Ck', 24, 500, | |
'Rocky Ck', 36, 450, | |
'Rocky Ck', 48, 420, | |
'Rocky Ck', 72, 400, | |
'Reedy Ck', 9, 490, | |
'Reedy Ck', 12, 550, | |
'Reedy Ck', 24, 500, | |
'Reedy Ck', 36, 540, | |
'Reedy Ck', 48, 510, | |
'Reedy Ck', 72, 600, | |
'Waterhole Ck', 9, 320, | |
'Waterhole Ck', 12, 320, | |
'Waterhole Ck', 24, 490, | |
'Waterhole Ck', 36, 450, | |
'Waterhole Ck', 48, 420, | |
'Waterhole Ck', 72, 300) | |
# Change the Creek names to factors. | |
# if we use forcats::as_factor the order of the factors will match the order of appearance in the data frame. | |
dog_ck <- dog_ck %>% | |
mutate(creek_name = forcats::as_factor(creek_name, ordered = TRUE)) | |
########################################################################################### | |
# | |
# First version of the line graph | |
# Choose an inappropriate colour palatte | |
mypalette <- RColorBrewer::brewer.pal(8,"Blues") | |
dog_ck %>% | |
ggplot(aes(factor(duration), peak_flow, group = creek_name, colour = creek_name)) + | |
geom_point() + | |
geom_line() + | |
scale_color_manual(name = '', | |
values = mypalette[3:8]) + | |
scale_y_continuous(name = 'Peak flow') + | |
scale_x_discrete(name = 'Duration (h)') + | |
theme_bw() | |
########################################################################################### | |
# | |
# Second and better version of the line graph | |
my_colours <- RColorBrewer::brewer.pal(6, 'Dark2') # distinct colours | |
improved_plot <- dog_ck %>% | |
ggplot(aes(duration, peak_flow, colour = creek_name)) + | |
geom_point() + | |
geom_line() + | |
scale_colour_manual(guide = FALSE, values = my_colours) + | |
scale_y_continuous(name = 'Peak flow (cumec)', labels = comma) + # comma separators | |
scale_x_continuous(name = 'Duration (h)', limits = c(NA, 90), breaks = c(9, 12, 24, 36, 48, 72), # natural scaling for x-axis | |
minor_breaks = FALSE) + | |
annotate(geom = 'text', # label both ends of the lines. I | |
x = 75, | |
y = 1500, | |
label = 'Brown Dog Ck', | |
hjust = 'left', | |
colour = my_colours[1]) + | |
annotate(geom = 'text', | |
x = 5, | |
y = 1170, | |
label = 'Brown Dog Ck', | |
hjust = 'left', | |
colour = my_colours[1]) + | |
annotate(geom = 'text', | |
x = 75, | |
y = 60, | |
label = 'Long Ck', | |
hjust = 'left', | |
colour = my_colours[2]) + | |
annotate(geom = 'text', | |
x = 5, | |
y = 60, | |
label = 'Long Ck', | |
hjust = 'left', | |
colour = my_colours[2]) + | |
annotate(geom = 'text', | |
x = 75, | |
y = 900, | |
label = 'Short Ck', | |
hjust = 'left', | |
colour = my_colours[3]) + | |
annotate(geom = 'text', | |
x = 0, | |
y = 750, | |
label = 'Short Ck', | |
hjust = 'left', | |
colour = my_colours[3]) + | |
annotate(geom = 'text', | |
x = 75, | |
y = 400, | |
label = 'Rocky Ck', | |
hjust = 'left', | |
colour = my_colours[4]) + | |
annotate(geom = 'text', | |
x = -3, | |
y = 400, | |
label = 'Rocky Ck', | |
hjust = 'left', | |
colour = my_colours[4]) + | |
annotate(geom = 'text', | |
x = 75, | |
y = 600, | |
label = 'Reedy Ck', | |
hjust = 'left', | |
colour = my_colours[5]) + | |
annotate(geom = 'text', | |
x = -3, | |
y = 500, | |
label = 'Reedy Ck', | |
hjust = 'left', | |
colour = my_colours[5]) + | |
annotate(geom = 'text', | |
x = 75, | |
y = 300, | |
label = 'Waterhole Ck', | |
hjust = 'left', | |
colour = my_colours[6]) + | |
annotate(geom = 'text', | |
x = 0, | |
y = 290, | |
label = 'Waterhole Ck', | |
hjust = 'left', | |
colour = my_colours[6]) | |
improved_plot # print the graph | |
########################################################################################### | |
# | |
# Minimalist version | |
improved_plot + | |
theme_bw() + | |
theme(panel.grid.major = element_blank(), # turn off the grid lines | |
panel.grid.minor = element_blank(), | |
panel.border = element_blank()) # turn off the border | |
# To retain the axes | |
improved_plot + | |
theme_bw() + | |
theme(panel.grid.major = element_blank(), # turn off the grid lines | |
panel.grid.minor = element_blank(), | |
panel.border = element_blank(), # turn off the border | |
axis.line = element_line( colour = 'black')) # draw axes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment