library(tidyverse)
library(skimr)
library(ggthemes)
library(hrbrthemes)
library(ggrepel)
library(socviz)
library(DT)
theme_set(theme_fivethirtyeight()+
theme(strip.background =element_rect(fill="lightgray"),
axis.title.x =
element_text(angle = 0,
size = rel(1.33),
margin = margin(10,0,0,0)),
axis.title.y =
element_text(angle = 90,
size = rel(1.33),
margin = margin(0,10,0,0))
)
)<- function(...) scale_color_tableau(...)
scale_colour_discrete <- function(...) scale_fill_tableau(...) scale_fill_discrete
Classwork 5
Graph Tables; Add Labels; Make Notes
Below is the packages, theme, and scale settings used in this classwork:
Recreate the R code necessary to generate the following graphs.
Question 1
<- socviz::gss_sm
gss_sm
::datatable(gss_sm) # displaying an interactive data.frame DT
Click to Check the Answer!
<- gss_sm |>
rel_by_region group_by( bigregion, religion ) |>
summarize( N = n() ) |>
mutate( freq = N / sum(N),
pct = round( (freq*100), 0) )
<- ggplot(rel_by_region,
p aes(x = pct,
y = religion,
fill = religion))
+ geom_col(position = "dodge2") +
p labs(x = NULL,
y = "Percent",
fill = "Religion") +
guides(fill = FALSE) +
facet_grid(~ bigregion)
Question 2
<- socviz::organdata
organdata
::datatable(organdata) DT
Q2a
Click to Check the Answer!
<- organdata |> group_by(consent_law, country) |>
by_country summarize_if(is.numeric, lst(mean, sd), na.rm = TRUE) |>
ungroup()
<- ggplot(data = by_country,
p mapping = aes(x = donors_mean, y = reorder(country, donors_mean),
color = consent_law))
+ geom_point(size=3) +
p labs(x = "Donor Procurement Rate",
y = "", color = "Consent Law") +
theme(legend.position="top")
Q2b
Click to Check the Answer!
<- organdata |> group_by(consent_law, country) |>
by_country summarize_if(is.numeric, lst(mean, sd), na.rm = TRUE) |>
ungroup()
<- ggplot(data = by_country,
p mapping = aes(x = donors_mean,
y = reorder(country, donors_mean)))
+ geom_point(size=3) +
p facet_wrap(~ consent_law, scales = "free_y", ncol = 1) +
labs(x= "Donor Procurement Rate",
y= "")
Q2c
Click to Check the Answer!
<- ggplot(data = by_country, mapping = aes(x = reorder(country,
p y = donors_mean))
donors_mean),
+ geom_pointrange(mapping = aes(ymin = donors_mean - donors_sd,
p ymax = donors_mean + donors_sd)) +
labs(x= "", y= "Donor Procurement Rate") + coord_flip()
Q2d
Click to Check the Answer!
<- ggplot(data = organdata,
p mapping = aes(x = roads,
y = donors,
color = world))
+ geom_point() +
p scale_y_continuous(breaks = c(5, 15, 25),
labels = c("Five", "Fifteen", "Twenty Five"))
Q2e
Click to Check the Answer!
<- ggplot(data = organdata,
p mapping = aes(x = roads,
y = donors,
color = world))
+ geom_point() +
p scale_color_discrete(labels =
c("Corporatist", "Liberal",
"Social Democratic", "Unclassified")) +
labs(x = "Road Deaths",
y = "Donor Procurement",
color = "Welfare State")
Q2f
Click to Check the Answer!
|>
organdata ggplot(mapping = aes(x = roads,
y = donors,
color = consent_law)) +
geom_point() +
facet_wrap(~ consent_law, ncol = 1) +
guides(color = "none") +
labs(x = "Road Deaths",
y = "Donor Procurement")
Q2g
Click to Check the Answer!
|>
organdata ggplot(mapping = aes(x = roads,
y = donors,
color = consent_law)) +
geom_point() +
labs(title = "By Consent Law",
x = "Road Deaths",
y = "Donor Procurement",
color = "Legal Regime:") +
theme(legend.position = "bottom",
plot.title = element_text(color = "darkred",
face = "bold"))
Question 3
<- datasets::mtcars
mtcars <- mtcars %>% # A native pipe (|>) does not work here.
mtcars mutate(car = rownames(.))
rownames(mtcars) <- 1:nrow(mtcars)
::datatable(mtcars) DT
Click to Check the Answer!
<- filter(mtcars,
dat > 5 & mpg < 15)
wt
<- ggplot() +
p geom_point(data = mtcars,
aes(wt, mpg),
color = 'red')
<- p + geom_text_repel(data = dat,
p1 aes(wt, mpg,
label = car),
box.padding = 0.5)
p1
Discussion
Welcome to our Classwork 5 Discussion Board! 👋
This space is designed for you to engage with your classmates about the material covered in Classwork 5.
Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.
If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 5 materials or need clarification on any points, don’t hesitate to ask here.
All comments will be stored here.
Let’s collaborate and learn from each other!