# A tibble: 16 × 4
method gender name value
<fct> <fct> <chr> <dbl>
1 car female weight_mn 61.4
2 car female weight_sd 5.08
3 car female height_mn 163.
4 car female height_sd 3.20
5 bike female weight_mn 65.3
6 bike female weight_sd 5.66
7 bike female height_mn 167.
8 bike female height_sd 3.95
9 bike male weight_mn 80.8
10 bike male weight_sd 10.1
11 bike male height_mn 177.
12 bike male height_sd 5.77
13 car male weight_mn 81.1
14 car male weight_sd 6.44
15 car male height_mn 177.
16 car male height_sd 4.45
Reshaping Data
We can now separate() the values in name
into = c("stat", "type") defines the new column names
# A tibble: 16 × 5
method gender stat type value
<fct> <fct> <chr> <chr> <dbl>
1 car female weight mn 61.4
2 car female weight sd 5.08
3 car female height mn 163.
4 car female height sd 3.20
5 bike female weight mn 65.3
6 bike female weight sd 5.66
7 bike female height mn 167.
8 bike female height sd 3.95
9 bike male weight mn 80.8
10 bike male weight sd 10.1
11 bike male height mn 177.
12 bike male height sd 5.77
13 car male weight mn 81.1
14 car male weight sd 6.44
15 car male height mn 177.
16 car male height sd 4.45
# A tibble: 8 × 5
method gender stat mn sd
<fct> <fct> <chr> <dbl> <dbl>
1 car female weight 61.4 5.08
2 car female height 163. 3.20
3 bike female weight 65.3 5.66
4 bike female height 167. 3.95
5 bike male weight 80.8 10.1
6 bike male height 177. 5.77
7 car male weight 81.1 6.44
8 car male height 177. 4.45
Now we have something we can use for barplots with errorbars
Reshaping Data
For the sake of brevity going forward, save that as summ_df
p +ggtitle("Weight and Height Across Participants")
Using Themes
Most theme() elements have their own modification functions
text: element_text()
lines: element_line()
rectangles: element_rect()
All can be removed using element_blank
The plot title needs element_text()
Using Themes
hjust controls the horizontal adjustment
hjust = 0.5 is centre-aligned
p +ggtitle("Weight and Height Across Participants") +theme(plot.title =element_text(hjust =0.5))
Using Themes
We can resize all primary text
p +ggtitle("Weight and Height Across Participants") +theme(text =element_text(size =14),plot.title =element_text(hjust =0.5) )
Using Themes
Control legend position
Doesn’t need an element_*() function
p +ggtitle("Weight and Height Across Participants") +theme(text =element_text(size =14),plot.title =element_text(hjust =0.5),legend.position ="bottom" )
Using Themes
Hide the background grid & rotates x-axis text
p +ggtitle("Weight and Height Across Participants") +theme(text =element_text(size =14),plot.title =element_text(hjust =0.5),legend.position ="bottom",panel.grid =element_blank(),axis.text.x =element_text(angle =90, vjust =0.5, hjust =1 ) )
Exporting Plots
Making plots in R is nice \(\implies\)How do we get them into our paper??!!!
ggsave() will save the last plot
The file extension will determine the format
Can be png, jpg, pdf, svg, tiff etc
width & height default to inches but can be changed
Getting font sizes right can be infuriating
Always add the save after you create the plot
Open immediately and check the font sizes
Closing Comments
Can now (hopefully) make the figures for our next paper
ggplot2 is very powerful \(\implies\) takes a long time to master
Getting data structured correctly is an important part
Note that once we loaded data \(\implies\) never modified
We saved four objects
cols, transport, summ_df, p
The last two were only to fit the code on slides
This keeps a clean workspace
No need for transport, transport1, transport1_mod etc