library("tidyverse")
library("patchwork")
library("readr")
library("sjPlot")
library("MASS")
theme_set(theme_classic())
library("tidyverse")
ggplot(data = mtcars) +
aes(mpg, wt, colour=factor(cyl))
ggplot(mpg, aes(
x = hwy,
y = cty,
colour = as.factor(year)
)) +
geom_point() +
geom_smooth(method = loess)
# Create graph and add title
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter")
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter") +
expand_limits(x = 0, y = 0)
Example benefits:
Example weakness
g1 <-ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, colour = class )) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter")
g2 <-ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class )) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter")
library("patchwork")
g1 / g2 + plot_annotation(title = "Which plot do you prefer and why?", tag_levels = 'a')
Arguably, patterns are easier to detect using the colour aesthetic but there are no hard and fast rules
g2 <-ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class )) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter")
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class) +
labs(title = "Relationship between engine displacement and fuel efficiency in the mpg automobile dataset") +
xlab("Engine displacement in (units)") +
ylab("Highway miles per liter")
Arguably the facets make the role of class more evident by making the class indicator more salient. Where possible,i t is a good idea to declutter your graph.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap( ~ class, nrow = 2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap( ~ class, nrow = 2) +
theme(legend.position = "none")
religiousity
(x-axis) and `thr_mus`` (y-axis) in the ISSP dataset. Create new axis labelsDownload the ISSP questionaire used in this study [here]: (https://github.com/go-bayes/psych-447/blob/main/data_raw/ISSP/ISSP_2018_Religion_Questionnaire_final_version1-2.pdf)
Note1
Let’s get the data into shape using dplyr. Run this code below
ip <- issp %>%
mutate(
id = factor(id),
thr_ath = as.factor(thr_ath),
thr_bd = as.factor(thr_bd),
thr_ch = as.factor(thr_ch),
thr_hd = as.factor(thr_hd),
thr_jw = as.factor(thr_jw),
thr_ms = as.factor(thr_ms),
neg_ath = as.factor(neg_ath),
neg_bd = as.factor(neg_bd),
neg_ch = as.factor(neg_ch),
neg_hd = as.factor(neg_hd),
neg_jw = as.factor(neg_jw),
neg_ms = as.factor(neg_ms),
wave = as.factor(wave),
nzeuro = as.factor(nzeuro),
eduyears = as.numeric(eduyears),
male = as.factor(male),
age = as.numeric(age),
rightwing = as.numeric(rightwing),
rural = as.factor(rural),
religiosity = as.numeric(religiosity)
)
library(ggplot2)
ggplot(data = ip, aes(y = as.numeric(thr_ms), x = religiosity, colour = wave)) +
geom_smooth(method = lm, fullrange = FALSE, alpha = 0.1) +
labs(title = "Religiosity predict Muslim acceptance post-Christchurch shootings") +
xlab("Level of Religiosity (scale 1-7) ") + ylab ("acceptance of Muslims (1-4)")
library(ggplot2)
ggplot(data = ip, aes(y = as.numeric(thr_ms), x = religiosity, colour = wave)) + geom_jitter(alpha = .1) +
geom_smooth(method = lm, fullrange = FALSE, alpha = 0.1) +
scale_y_continuous(limits = c(0,4))
library(ggplot2)
ggplot(data = ip, aes(y = as.numeric(thr_ms), x = religiosity, colour = wave)) + geom_jitter(alpha = .1) +
geom_smooth(method = lm, fullrange = FALSE, alpha = 0.1) +
scale_y_continuous(limits = c(1,4))
library(sjPlot)
plot_xtab(
ip$thr_ms,
ip$wave,
show.total = F,
show.n = F,
geom.colors = c("lightgreen", "darkred")
) +
xlab("Threatened by Muslims") + ylab("Frequency") +
#scale_y_continuous(limits=c(0,7)) + #theme(plot.title = element_text(size=09))
theme(axis.text.x = element_text(angle = 20, hjust = 1))
E.g. add a title
library(sjPlot)
plot_xtab(
ip$thr_ms,
ip$wave,
show.total = F,
show.n = F,
geom.colors = c("lightgreen", "darkred")
) +
xlab("Threatened by Muslims") + ylab("Frequency") +
#scale_y_continuous(limits=c(0,7)) + #theme(plot.title = element_text(size=09))
theme(axis.text.x = element_text(angle = 20, hjust = 1)) +
labs(title = "Comparison of sample responses to Muslim threat in 2018 an 2019")
Data were collected with Barry Milne and Martin Van dataset; the data are only authorised for the purposes of teaching.↩︎
If you see mistakes or want to suggest changes, please create an issue on the source repository.
Text and figures are licensed under Creative Commons Attribution CC BY-NC-SA 4.0. Source code is available at https://go-bayes.github.io/psych-447/, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Bulbulia & Karl (2021, April 20). Psych 447: Week 3 Workbook Solutions. Retrieved from https://vuw-psych-447.netlify.app/workbooks/W_3_s/
BibTeX citation
@misc{bulbulia2021week, author = {Bulbulia, Joseph and Karl, Johannes}, title = {Psych 447: Week 3 Workbook Solutions}, url = {https://vuw-psych-447.netlify.app/workbooks/W_3_s/}, year = {2021} }