- Warm-up: data visualization
- Data Analysis by Hand
- Elements of Visualization
- Group Activity
- Grammar of Graphics
2026-4-15
## group 1 group 2 group 3 group 4 ## 1 Qin, Celine Mahoney, Brigette Pham, Canon Ong, Alyssa ## 2 Knowles, Genny Barga, Jolie Mendoza, Ava Bell, Mary Rose ## 3 Wolfenstein, Luci Pacheco, Alex Batson, Anthony Smith, Reid ## group 5 group 6 ## 1 Moore, Allana Devir, Lindsey ## 2 Leahy, Olivia ## 3 Kang, Christine Myoung, Sein
## group 1 group 2 group 3 group 4 ## 1 Qin, Celine Mahoney, Brigette Pham, Canon Ong, Alyssa ## 2 Knowles, Genny Barga, Jolie Mendoza, Ava Bell, Mary Rose ## 3 Wolfenstein, Luci Pacheco, Alex Batson, Anthony Smith, Reid ## group 5 group 6 ## 1 Moore, Allana Devir, Lindsey ## 2 Leahy, Olivia ## 3 Kang, Christine Myoung, Sein
## group 1 group 2 group 3 group 4 ## 1 Qin, Celine Mahoney, Brigette Pham, Canon Ong, Alyssa ## 2 Knowles, Genny Barga, Jolie Mendoza, Ava Bell, Mary Rose ## 3 Wolfenstein, Luci Pacheco, Alex Batson, Anthony Smith, Reid ## group 5 group 6 ## 1 Moore, Allana Devir, Lindsey ## 2 Leahy, Olivia ## 3 Kang, Christine Myoung, Sein
gg in ggplot2ggplot2!)ggplot2ggplot2 with the gapminder datagapminder data?library(gapminder) View(gapminder)
ggplot2ggplotlibrary(ggplot2) # plot blank canvas ggplot()
ggplot2library(gapminder) # add data and aesthetics ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
ggplot2ggplot2 works in layers!geom_ layer+ sign adds layers to our ggplotgeom_pointgeom?# create a plot
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))+
geom_point()
p) to our plot?# create the plot
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))
# plot it
p + geom_point()
geom_point function# create the plot
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp))+
geom_point(col = "purple")
geom_point function# create the plot
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp,
col = continent))+
geom_point()
yearalpha to 0.5size to vary by pop# create the plot
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp,
col = continent))+
geom_point()
# create a dataframe (note that : returns a sequence) df <- data.frame(year = 2000:2020, pop = 40:60) # look at the first 6 rows head(df)
## year pop ## 1 2000 40 ## 2 2001 41 ## 3 2002 42 ## 4 2003 43 ## 5 2004 44 ## 6 2005 45
library(dplyr)
# function to get from millions to people
millions_to_people <- function(m){
p <- m/1000000
return(p)
}
# run function to create new variable
df %>%
mutate(pop_p = millions_to_people(pop)) %>%
head()
## year pop pop_p ## 1 2000 40 4.0e-05 ## 2 2001 41 4.1e-05 ## 3 2002 42 4.2e-05 ## 4 2003 43 4.3e-05 ## 5 2004 44 4.4e-05 ## 6 2005 45 4.5e-05
pop by 100# create a dataframe
df <- data.frame(year = 2000:2020, pop = 40:60)
# create your function here
<- function(x){
}
# modify your dataframe here
df
Instead of this:
fourth_function(third_function(second_function(first_function(x))))
We do this:
x %>% first_function() %>% second_function() %>% third_function() %>% fourth_function()
df?# run function on to create new variable df %>% mutate(pop_p = millions_to_people(pop)) %>% head()
## year pop pop_p ## 1 2000 40 4.0e-05 ## 2 2001 41 4.1e-05 ## 3 2002 42 4.2e-05 ## 4 2003 43 4.3e-05 ## 5 2004 44 4.4e-05 ## 6 2005 45 4.5e-05
magrittr pipe (%<>%)library(magrittr) # we use the magrittr pipe df %<>% mutate(pop_p = millions_to_people(pop)) # check that it worked head(df)
magrittr pipe!<- and %>% operators?library(magrittr) # we use the magrittr pipe df %<>% mutate(pop_p = millions_to_people(pop))
filter to observations of interestlibrary(readr)
library(RCurl)
# input url from github
url <- getURL("https://raw.githubusercontent.com/tylermcdaniel/soc10/refs/heads/deployment/Data/ca_dem_table.csv")
# download wikipedia data (from github)
ca_dem_table <- read_csv(url)
2000_num by the total for that year (33871648) and then remove the “total” row?ca_dem_table %<>%
ggplot with the California demographic datamagrittr