class: center, middle, inverse, title-slide # ggploting your way to beautiful figures ##
### Daniel Roelfs (
@dthroelfs
)
Adriano Winterton (
@fuyu00
)
Linn Sofie Saether (
@linnssaether
) --- class: center, middle, inverse # How to improve our figures --- class: center, middle, inverse # When it comes to figure making <img src="images/ggplot2.png" width="150px" align="middle"/> <font size="+6"> > </font> <img src="images/spss_logo.png" width="150px" align="middle"/> <img src="images/ggplot2.png" width="150px" align="middle"/> <font size="+6"> > </font> <img src="images/excel_logo.png" width="150px" align="middle"/> --- class: center, middle, inverse # The anatomy of a ggplot --- class: inverse <img src="images/GrammarOfGraphics_fig.png" class="center"/> --- class: inverse <img src="images/GrammarOfGraphics_fig.png" height="300px" class="centerfig"/> # Geometries = boxplot, line, scatter, etc. # Aesthetics = x-axis, y-axis, color, fill, shape # Data = your awesome TOP data 😃 --- class: middle # Installing the packages we need ```r install.packages("tidyverse") # <- includes ggplot2 devtools::install_github("norment/normentR") # <- themes and colors ``` --- class: middle # Loading the packages ```r library(tidyverse) library(normentR) ``` --- ## The skeleton <img src="images/GrammarOfGraphics_fig_black.png" height="150px" align="centerfig"/> -- ## The code ```r ggplot(data = mtcars, aes(x = wt, y = mpg)) + geom_point() ``` <p style="color:#d3d800;">Data: mtcars <p style="color:#0081c9;">Aethetics: aes(x = wt, y = mpg) <p style="color:#5c2483;">Geometry: geom_point() --- class: center, middle, inverse # ggplot, the layer cake 🍰 --- ```r ggplot(mtcars, aes(x = wt, y = mpg)) ``` <img src="presentation_files/figure-html/unnamed-chunk-4-1.png" width="60%" style="display: block; margin: auto;" /> --- ```r ggplot(mtcars, aes(x = wt, y = mpg)) + * geom_point() ``` <img src="presentation_files/figure-html/unnamed-chunk-5-1.png" width="60%" style="display: block; margin: auto;" /> --- ```r *ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + * geom_point(size = 3) ``` <img src="presentation_files/figure-html/unnamed-chunk-6-1.png" width="60%" style="display: block; margin: auto;" /> --- ```r ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3) + * labs(x = "Weight (1000 lbs)", * y = "Miles/(US) gallon", * color = "Number of cylinders") ``` <img src="presentation_files/figure-html/unnamed-chunk-7-1.png" width="60%" style="display: block; margin: auto;" /> --- ```r ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point(size = 3) + labs(x = "Weight (1000 lbs)", y = "Miles/(US) gallon", color = "Number of cylinders") + * scale_color_norment(discrete = TRUE, palette = "logo") ``` <img src="presentation_files/figure-html/unnamed-chunk-8-1.png" width="60%" style="display: block; margin: auto;" /> --- ```r p <- ggplot(mpg %>% filter(class != "2seater"), aes(x = reorder(class,-cty), y = cty, fill = reorder(class,-cty))) + geom_violin() + geom_boxplot(width = 0.1, outlier.shape = 4) + geom_curve(aes(xend = 1.1, yend = 36, x = 1.6, y = 38), curvature = 0.2, arrow = arrow(length = unit(2,"mm"))) + geom_text(data = . %>% filter(cty == max(cty)), aes(x = 2.4, y = 38, label = paste(manufacturer, model, sep = " "))) + labs(x = "Class of vehicle", y = "City mileage", fill = "Class", title = "What class of car has the\nbest mileage in the city?", caption = "Source: mpg") + scale_x_discrete() + scale_y_continuous(limits = c(0,40)) + scale_fill_norment(discrete = TRUE, palette = "nejm") + theme_norment(legend = FALSE, grid = "Y") + theme( plot.title = element_text(hjust = -0.1) ) ``` --- ```r print(p) ``` <img src="presentation_files/figure-html/unnamed-chunk-12-1.png" width="90%" height="90%" style="display: block; margin: auto;" /> --- # What's possible with ggplot <img src="images/BBC_time_image.png" style="height:180%, width=180%"> --- # What's possible with ggplot <img src="images/Bach_productivity.png" width="200%" style="display: block; margin: auto;" /> --- # What's possible with ggplot <img src="images/column_wheel_permonth.png" width="605" style="display: block; margin: auto;" /> --- # What's possible with ggplot <img src="images/Wine_VarietyxPrice.png" width="200%" style="display: block; margin: auto;" /> --- class: center,middle,inverse # One more (very) important concept: -- <br> # Long vs wide format 😃 --- # Long vs wide format #### `ggplot` wants a single column for x-axis, y-axis, color, etc This is often not a problem, e.g.: ```r str(your_data) ``` ``` ## 'data.frame': 30 obs. of 3 variables: ## $ id : Factor w/ 30 levels "HC_0130","HC_0152",..: 1 15 19 23 18 14 11 3 8 17 ... ## $ age : int 64 32 60 45 53 31 24 54 28 63 ... ## $ measure: num 1.92 1.28 2.76 0.32 3.8 1.14 1.66 0.82 1.54 3.52 ... ``` ```r print(your_data) ``` ``` ## id age measure ## 1 HC_0130 64 1.92 ## 2 HC_0471 32 1.28 ## 3 HC_0576 60 2.76 ## 4 HC_0682 45 0.32 ## 5 HC_0556 53 3.80 ## 6 HC_0455 31 1.14 ## 7 HC_0422 24 1.66 ## 8 HC_0155 54 0.82 ## 9 HC_0382 28 1.54 ## 10 HC_0513 63 3.52 ## 11 HC_0697 44 2.38 ## 12 HC_0656 54 0.52 ## 13 HC_0450 40 3.02 ## 14 HC_0439 52 3.60 ## 15 HC_0582 64 0.96 ## 16 HC_0605 39 1.52 ## 17 HC_0415 52 2.60 ## 18 HC_0744 29 1.20 ## 19 HC_0807 28 0.86 ## 20 HC_0403 55 1.90 ## 21 HC_0162 53 3.64 ## 22 HC_0511 50 1.68 ## 23 HC_0320 40 2.40 ## 24 HC_0693 19 0.28 ## 25 HC_0289 38 2.72 ## 26 HC_0352 51 0.20 ## 27 HC_0834 60 0.64 ## 28 HC_0865 20 2.06 ## 29 HC_0871 21 2.02 ## 30 HC_0152 33 0.80 ``` --- # Long vs wide format #### `ggplot` wants a single column for x-axis, y-axis, color, etc This is often not a problem, e.g.: ```r ggplot(your_data, aes(x = age, y = measure)) + geom_point() ``` <img src="presentation_files/figure-html/unnamed-chunk-18-1.png" width="50%" style="display: block; margin: auto;" /> --- # Long vs wide format #### Example of wide format ```r View(wide) ``` <img src="images/wide.png" width="630" style="display: block; margin: auto;" /> --- # Long vs wide format #### Conversion to long format ```r long <- pivot_longer(data = wide, cols = c(iq,workmem,procspeed,cpt)) View(long) ``` <img src="images/long.png" width="378" style="display: block; margin: auto;" /> --- # Long vs wide format #### Conversion to long format ```r long <- pivot_longer(data = wide, cols = c(iq,workmem,procspeed,cpt)) View(long) ``` <img src="images/long_annot.png" width="378" style="display: block; margin: auto;" /> --- # Long vs wide format #### Conversion to long format ```r ggplot(long, aes(x = name, y = value, fill = diag)) + geom_boxplot() ``` <img src="presentation_files/figure-html/unnamed-chunk-26-1.png" width="400px" style="display: block; margin: auto;" /> --- # Long vs wide format #### Conversion to long format <img src="images/wide_to_long.png" width="800px" style="display: block; margin: auto;" /> --- class: center, middle, inverse <img src="images/cat_meme.png" width="800px" style="display: block; margin: auto;" /> --- class: center, middle, inverse # Download the data here: <!--# bit.ly/2UxUNnc--> # bit.ly/2SntWYd