- Please complete the Student Voice section on NOW.
- Complete last week’s exercises.
- This is the last session on
ggplot2. We will move one to shiny apps next week.
ggplot2. We will move one to shiny apps next week.By the end of this session, you should be able to \(\dots\)
ggplot2 visualisations using scales, themes, and labels to enhance clarity and impact.Download exercises from Week 5 folder on NOW and move them into your R-project directory.
Revisiting Tufte’s Principles (Tufte, 2001):
ggplot2How does ggplot2 supports Tufte’s principles:
theme_minimal(), theme_void())facet_wrap(), facet_grid())position_dodge(), alpha = .5, size = .5The grammar of graphics implemented in ggplot2 by Hadley Wickham aligns with Tufte’s principles.
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs()
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(title = "My scatter plot")
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(title = "My scatter plot",
subtitle = "I'm a subtitle")
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(caption = "Caption for data source")
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(tag = "A")
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(x = "Production duration in msecs",
y = "Reaction time in msecs")
labstitlesubtitlecaptiontagxycolour, shape, linetype, fillggplot(d_spellname, aes(y = rt, x = dur,
colour = modality)) +
geom_point() +
labs(colour = "Legend\ntitle:")
Complete RMarkdown document 1_add_labs.Rmd
scale_colour_discretescale_colour_continuousscale_colour_manualcolourggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
scale_colour_discrete(
labels = c("Spoken output", "Written output"))
ggthemesggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
scale_colour_manual(
labels = c("Spoken output", "Written output"),
values = c("firebrick", "turquoise2")
ggthemesggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
scale_colour_manual(
labels = c("Spoken output", "Written output"),
values = c("firebrick", "turquoise2"))
ggthemes# RGB codes of "colorblind" function
mycolours <- c("#000000", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7")# RGB codes of "colorblind" function scales::show_col(colorblind_pal()(8))
ggthemesggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
scale_colour_manual(
labels = c("Spoken output", "Written output"),
values = mycolours[1:2])
ggthemesggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
scale_colour_colorblind(
labels = c("Spoken output", "Written output"))
Complete the RMarkdown 2_customising_aes.Rmd
ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality)
ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality, labeller = label_both)
d_spellname <- mutate(d_spellname,
modality = recode(modality,
"speech" = "Spoken output",
"writing" = "Written output"))
d_spellname <- mutate(d_spellname,
modality = recode(modality,
"speech" = "Spoken output",
"writing" = "Written output"))
themes() or using wrapper functionsggplot2 wrappers:[1] "theme_bw" "theme_classic" "theme_dark" "theme_grey" [5] "theme_light" "theme_linedraw" "theme_minimal" "theme_void"
ggthemes package for more themes:[1] "theme_base" "theme_calc" "theme_clean" [4] "theme_economist" "theme_economist_white" "theme_excel" [7] "theme_excel_new" "theme_few" "theme_fivethirtyeight" [10] "theme_foundation" "theme_gdocs" "theme_hc" [13] "theme_map" "theme_pander" "theme_par" [16] "theme_solarized" "theme_solarized_2" "theme_solid" [19] "theme_stata" "theme_stata_base" "theme_stata_colors" [22] "theme_tufte" "theme_wsj"
ggplot2 default)ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality) + theme_grey(base_size = 11)
ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality) + theme_minimal(base_size = 14)
ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality) + theme_light(base_size = 14)
ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality) + theme_dark(base_size = 14)
ggthemes)ggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality) + theme_clean()
axislegendpanelplotstripggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme()
axisaxis.text
axis.text.xaxis.text.yaxis.title
axis.title.xaxis.title.yggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme(axis.text = element_text(face = "italic"))
axisaxis.text
axis.text.xaxis.text.yaxis.title
axis.title.xaxis.title.yggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme(axis.title = element_text(face = "bold"))
axisaxis.text
axis.text.xaxis.text.yaxis.title
axis.title.xaxis.title.yggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme(axis.title.y = element_text(face = "bold"))
legendlegend.backgroundlegend.marginlegend.spacinglegend.keylegend.textlegend.titlelegend.positionlegend.orientationlegend.justificationlegend.boxggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) + geom_point() + theme()
legendlegend.backgroundlegend.marginlegend.spacinglegend.keylegend.textlegend.titlelegend.positionlegend.orientationlegend.justificationlegend.boxggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) + geom_point() + theme(legend.position = "top")
legendlegend.backgroundlegend.marginlegend.spacinglegend.keylegend.textlegend.titlelegend.positionlegend.orientationlegend.justificationlegend.boxggplot(d_spellname, aes(y = rt, x = dur, colour = modality)) +
geom_point() +
theme(legend.position = "top",
legend.justification = "right")
legendlegend.backgroundlegend.marginlegend.spacinglegend.keylegend.textlegend.titlelegend.positionlegend.orientationlegend.justificationlegend.boxggplot(d_spellname, aes(y = rt, x = dur, colour = modality))+ geom_point() + theme(legend.position = c(.15,.8))
panelpanel.backgroundpanel.borderpanel.spacingpanel.grid
panel.grid.majorpanel.grid.minorggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme()
panelpanel.backgroundpanel.borderpanel.spacingpanel.grid
panel.grid.majorpanel.grid.minorggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme(panel.background = element_blank())
plotplot.backgroundplot.marginplot.titleplot.subtitleplot.captionplot.tagggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme()
plotplot.backgroundplot.marginplot.titleplot.subtitleplot.captionplot.tagggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + theme(plot.background = element_rect(fill = "pink"))
plotplot.backgroundplot.marginplot.titleplot.subtitleplot.captionplot.tagggplot(d_spellname, aes(y = rt, x = dur)) +
geom_point() +
theme(plot.background = element_rect(fill = "pink"),
plot.margin = unit(c(2,2,2,2), "cm"))
plotplot.backgroundplot.marginplot.titleplot.subtitleplot.captionplot.tagggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + labs(title = "I'm a title") + theme(plot.title = element_text(colour = "pink"))
plotplot.backgroundplot.marginplot.titleplot.subtitleplot.captionplot.tagggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + labs(caption = "I'm a caption") + theme(plot.caption = element_text(face = "italic"))
strip.backgroundstrip.placementstrip.textggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality, labeller = label_both) + theme()
strip.backgroundstrip.backgroundstrip.placementstrip.textggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality, labeller = label_both) + theme(strip.background = element_blank())
strip.backgroundstrip.backgroundstrip.placementstrip.textggplot(d_spellname, aes(y = rt, x = dur)) + geom_point() + facet_grid(~modality, labeller = label_both) + theme(strip.background = element_rect(fill = "forestgreen"))
strip.textstrip.backgroundstrip.placementstrip.textggplot(d_spellname, aes(y = rt, x = dur)) +
geom_point() +
facet_grid(~modality, labeller = label_both)+
theme(strip.background = element_rect(fill = "forestgreen"),
strip.text = element_text(colour = "white", hjust = 0))
strip.textstrip.backgroundstrip.placementstrip.textggplot(d_spellname, aes(y = rt, x = dur)) +
geom_point() +
facet_grid(~modality, labeller = label_both) +
theme(strip.background = element_rect(fill = "forestgreen"),
strip.text = element_text(colour = "white", hjust = 0,
face = "bold", size = 16,
angle = 180))
Complete RMarkdown 3_defining_themes.Rmd
ggsave("name of plot.png", width = 5, height = 5)
.eps, .pdf, .svg, .wmf, .png, .jpg, .bmp, .tifffig.width in RMarkdown)ggplot2 CheatSheetTask: Select a behavioural dataset—-i.e., data related to human behaviour (e.g., psychological experiments, survey responses, reaction times, decision-making patterns, social media behaviour, etc.).
Feedback focus:
Let’s rework a figures (e.g. Figure 4) from van Lieburg et al. (2023); code and data are HERE.
Andrews, M. (2021). Doing data science in R: An introduction for Social Scientists. SAGE Publications Ltd.
Tufte, E. R. (2001). The visual display of quantitative information (2nd ed.). Graphics Press.
van Lieburg, R., Sijyeniyo, E., Hartsuiker, R. J., & Bernolet, S. (2023). The development of abstract syntactic representations in beginning L2 learners of Dutch. Journal of Cultural Cognitive Science, 7, 289–309. https://doi.org/10.1007/s41809-023-00131-5
Wickham, H. (2016). ggplot2: Elegant graphics for data analysis. Springer.
Wickham, H., & Grolemund, G. (2016). R for data science: Import, tidy, transform, visualize, and model data. O’Reilly Media, Inc.