library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.3
## ✓ tibble 3.0.1 ✓ dplyr 0.8.5
## ✓ tidyr 1.0.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ─────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(readr)
library(wordbankr)
library(here)
## here() starts at /cloud/project
library(RColorBrewer)
library(wesanderson)
library(ggthemes)
library(beyonce)
## Registered S3 method overwritten by 'beyonce':
## method from
## print.palette wesanderson
library(viridis)
## Loading required package: viridisLite
library(forcats)
library(colorblindr)
## Loading required package: colorspace
library(ggrepel)
sounds <- read_csv("http://bit.ly/cs631-meow")
## Parsed with column specification:
## cols(
## age = col_double(),
## sound = col_character(),
## kids_produce = col_double(),
## kids_understand = col_double(),
## kids_respond = col_double(),
## prop_produce = col_double(),
## prop_understand = col_double()
## )
glimpse(sounds)
## Rows: 33
## Columns: 7
## $ age <dbl> 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12,…
## $ sound <chr> "cockadoodledoo", "meow", "woof woof", "cockadoodledo…
## $ kids_produce <dbl> 1, 0, 3, 0, 2, 2, 0, 5, 4, 0, 5, 12, 0, 12, 28, 9, 12…
## $ kids_understand <dbl> 3, 10, 12, 2, 21, 22, 9, 41, 40, 4, 36, 32, 16, 59, 5…
## $ kids_respond <dbl> 35, 35, 35, 91, 93, 93, 139, 145, 143, 94, 94, 94, 14…
## $ prop_produce <dbl> 0.02857143, 0.00000000, 0.08571429, 0.00000000, 0.021…
## $ prop_understand <dbl> 0.08571429, 0.28571429, 0.34285714, 0.02197802, 0.225…
count(sounds, age)
## # A tibble: 11 x 2
## age n
## <dbl> <int>
## 1 8 3
## 2 9 3
## 3 10 3
## 4 11 3
## 5 12 3
## 6 13 3
## 7 14 3
## 8 15 3
## 9 16 3
## 10 17 3
## 11 18 3
count(sounds, sound)
## # A tibble: 3 x 2
## sound n
## <chr> <int>
## 1 cockadoodledoo 11
## 2 meow 11
## 3 woof woof 11
sounds %>%
group_by(sound) %>%
summarize(totak_produce = sum(kids_produce)) %>%
knitr::kable()
| sound | totak_produce |
|---|---|
| cockadoodledoo | 148 |
| meow | 681 |
| woof woof | 940 |
ggplot(sounds, aes(x=sound, y=kids_produce))+
geom_col()+
labs(x="Sounds", y="Total Children Producing")
ggplot(sounds, aes(x=age, y=prop_produce))+
geom_col()+
labs(x="Age (mos)", y="Proportion of Children Producing")+
facet_wrap(~sound)
ggplot(sounds, aes(x=age, y=prop_produce))+
geom_point()+
labs(x="Age (mos)", y="Proportion of Children Producing")+
facet_wrap(~sound)
Remember: Make sureto adjust the labels!!
ggplot(sounds, aes(x=age, y=prop_produce))+
geom_point(aes(color=sound),size=2)+
labs(x="Age (mos)", y="Proportion of Children Producing")
## Adding lines (Challenge #3)
ggplot(sounds, aes(x=age, y=prop_produce))+
geom_line()+
geom_point(aes(color=sound), size=2)+
labs(x="Age (months)", y="Proportion Of Children Producing")
ggplot(sounds, aes(x=age, y=prop_produce))+
geom_line(aes(group=sound))+
geom_point(aes(color=sound), size=2)+
labs(x="Age (months)", y="Proportion Of Children Producing")
ggplot(sounds, aes(x=age, y=prop_produce, color=sound))+
geom_line()+
geom_point(size=2)+
labs(x="Age (months)", y="Proportion Of Children Producing")
geom_smooth()ggplot(sounds, aes(x=age,
y=prop_produce,
color=sound))+
geom_smooth(se=FALSE, lwd=0.5) +
geom_point(size=2) +
labs(x="Age (months)", y="Proportion Of Children Producing")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
install.packages("forcats")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(forcats)
Experiment with each property in scale_color_hue() to get a sense of what it does.
sound_traj <- ggplot(sounds, aes(x = age,
y = prop_produce,
color = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(se = FALSE, lwd = .5) +
geom_point(size = 2) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
color = "sound")
sound_traj
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj+scale_color_hue()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj+scale_color_hue(h=c(0,90), l=65, c=100)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj+scale_color_hue(l=45)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj+scale_color_hue(l=75,c=50)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Experiment with scale_color_manual() and some of the various named colors that come built-in to R!
sound_traj +
scale_color_manual(values=c("cornflowerblue",
"seagreen", "coral"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
color = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(se = FALSE, lwd = .5) +
geom_point(size = 2) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
color = "sound") +
scale_fill_manual(values = c("cornflowerblue",
"seagreen", "coral"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Why doesn’t the code block change the colors? scale_fill_manual requires specification of fill aesthetic instead of color
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(se = FALSE, lwd = .5) +
geom_point(size = 2) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound") +
scale_fill_manual(values = c("cornflowerblue",
"seagreen", "coral"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(aes(color = fct_reorder2(sound, age, prop_produce)),
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(aes(color = fct_reorder2(sound, age, prop_produce)),
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound") +
scale_fill_manual(values = c("cornflowerblue",
"seagreen", "coral")) +
scale_color_manual(values = c("cornflowerblue",
"seagreen", "coral"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
my_colors<-c("cadetblue", "steelblue", "salmon")
sound_traj + scale_color_manual(values = my_colors)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sb_colorblind <- c("#0072B2", "#009E73", "#D55E00", "#CC79A7", "#F0E442", "#56B4E9")
sound_traj +
scale_color_manual(values = sb_colorblind)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
install.packages("RColorBrewer")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(RColorBrewer)
brewer.pal(5, "Dark2")
## [1] "#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E"
display.brewer.pal(5, "Dark2")
scale_color_brewer()sound_traj +
scale_color_brewer(palette = "Dark2")
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
wesandersoninstall.packages("wesanderson")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(wesanderson)
names(wes_palettes)
## [1] "BottleRocket1" "BottleRocket2" "Rushmore1" "Rushmore"
## [5] "Royal1" "Royal2" "Zissou1" "Darjeeling1"
## [9] "Darjeeling2" "Chevalier1" "FantasticFox1" "Moonrise1"
## [13] "Moonrise2" "Moonrise3" "Cavalcanti1" "GrandBudapest1"
## [17] "GrandBudapest2" "IsleofDogs1" "IsleofDogs2"
wes_palette("BottleRocket1")
wes_palette("BottleRocket1")[1:4]
## [1] "#A42820" "#5F5647" "#9B110E" "#3F5151"
wes_palette("BottleRocket1")[c(1,4)]
## [1] "#A42820" "#3F5151"
sound_traj +
scale_color_manual(values=wes_palette("Darjeeling1"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_manual(values = wes_palette("FantasticFox1"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_manual(values = wes_palette("Darjeeling1")[3:5])
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_manual(values = wes_palette("FantasticFox1")[c(2, 3, 5)])
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggthemesinstall.packages("ggthemes")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(ggthemes)
sound_traj +
scale_color_fivethirtyeight()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_economist()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
beyonceinstall.packages("devtools")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
devtools::install_github("dill/beyonce")
## Skipping install of 'beyonce' from a github remote, the SHA1 (d0a5316b) has not changed since last install.
## Use `force = TRUE` to force installation
library(beyonce)
beyonce_palette(18)
sound_traj +
scale_color_manual(values = beyonce_palette(18)[3:5])
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
viridisinstall.packages("viridis")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(viridis)
sound_traj +
scale_color_viridis(discrete = TRUE) +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_viridis(discrete = TRUE, option = "plasma") +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x=age,
y=prop_produce,
fill=fct_reorder2(sound, age, prop_produce)))+
geom_smooth(aes(color = fct_reorder2(sound, age, prop_produce)),
se = FALSE, lwd = 0.5, show.legend=FALSE) +
geom_point(size=2, shape=21, color = "midnightblue") +
labs(x= "Age (months)",
y="Proportion of Children Producing",
fill = "sound")+
scale_fill_viridis(discrete=TRUE) +
scale_color_viridis(discrete = TRUE) +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Experimenting with scale_color_grey()/scale_fill_grey()
sound_traj +
scale_color_grey() +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
sound_traj +
scale_color_grey(start = 0.2, end=0.8)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(aes(color = fct_reorder2(sound, age, prop_produce)),
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound") +
scale_fill_grey(start = 0.3, end = 1) +
scale_color_grey(start = 0.3, end = 1)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(aes(lty = fct_reorder2(sound, age, prop_produce)), color = "black",
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound") +
scale_fill_grey(start = 0.3, end = 1)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(sounds, aes(x = age,
y = prop_produce,
fill = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(aes(color = fct_reorder2(sound, age, prop_produce),
lty = fct_reorder2(sound, age, prop_produce)),
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Age (months)",
y = "Proportion of Children Producing",
fill = "sound") +
scale_fill_grey(start = 0.3, end = .8) +
scale_color_grey(start = 0.3, end = .8)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
colorblindrdevtools::install_github("wilkelab/cowplot")
## Skipping install of 'cowplot' from a github remote, the SHA1 (06aeeb44) has not changed since last install.
## Use `force = TRUE` to force installation
install.packages("colorspace", repos = "http://R-Forge.R-project.org")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
devtools::install_github("clauswilke/colorblindr")
## Skipping install of 'colorblindr' from a github remote, the SHA1 (1d0d5afe) has not changed since last install.
## Use `force = TRUE` to force installation
my_sound_traj <- sound_traj +
scale_color_manual(values = beyonce_palette(18)[c(1, 4, 5)])
library(colorblindr)
cvd_grid(my_sound_traj)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
cb_sound_traj <- sound_traj +
scale_color_OkabeIto()
cb_sound_traj
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
cvd_grid(cb_sound_traj)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# To use for line and point colors, add
sound_traj +
scale_colour_manual(values = cbbPalette[c(3, 7, 8)])
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
library(ggrepel)
sounds <- sounds %>%
mutate(label = case_when(
age == max(age) ~ sound))
ggplot(sounds, aes(x = age,
y = prop_produce,
color = fct_reorder2(sound, age, prop_produce))) +
geom_smooth(se = FALSE, lwd = .5) +
geom_point(size = 2) +
labs(x = "Age (months)",
y = "Proportion of Children Producing") +
geom_text_repel(aes(label = label),
nudge_x = 1,
direction = "y",
na.rm = TRUE) +
guides(color = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Experiment a bit with scale_color_gradient()/scale_fill_gradient()!
sound_by_age <- ggplot(sounds, aes(x = age,
y = prop_produce,
color = age)) +
geom_line(aes(group = sound), lwd = .5) +
geom_point(size = 2) +
labs(x = "Age (months)",
y = "Proportion of Children Producing")
sound_by_age
med_age <- sounds %>%
summarize(mos = median(age)) %>%
pull()
sound_by_age +
scale_color_gradient2(midpoint = med_age,
low="blue", mid="white", high="red" )
Experiment a bit with RColorBrewer and viridis
library(RColorBrewer)
sound_by_age +
scale_color_gradientn(colours = brewer.pal(n=5, name="PuBuGn"))
sound_by_age +
scale_color_gradientn(colours = rev(brewer.pal(n=5, name="PuBuGn")))
library(viridis)
sound_by_age +
scale_color_viridis()
sound_by_age +
scale_color_viridis(option = "magma")
sound_by_age +
scale_color_viridis(option = "inferno", begin = 1, end = 0)
install.packages("babynames")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)
library(babynames)
glimpse(babynames)
## Rows: 1,924,665
## Columns: 5
## $ year <dbl> 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880…
## $ sex <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F",…
## $ name <chr> "Mary", "Anna", "Emma", "Elizabeth", "Minnie", "Margaret", "Ida"…
## $ n <int> 7065, 2604, 2003, 1939, 1746, 1578, 1472, 1414, 1320, 1288, 1258…
## $ prop <dbl> 0.07238359, 0.02667896, 0.02052149, 0.01986579, 0.01788843, 0.01…
names_filtered <- babynames %>%
filter(name == "Ralph" | name == "Gregory" | name == "Michelle")
babynames_by_year <- ggplot(names_filtered, aes(x = year,
y = n,
color = n)) +
geom_line(aes(group = name), lwd = .5) +
geom_point(size = 2) +
labs(x = "Year)",
y = "Quantity")
babynames_by_year
ggplot(names_filtered, aes(x=year,
y=n,
fill=fct_reorder2(name, year, n)))+
geom_smooth(aes(color = fct_reorder2(name, year, n)),
se = FALSE, lwd = 0.5, show.legend=FALSE) +
geom_point(size=2, shape=21, color = "midnightblue") +
labs(x= "Year",
y="Quantity",
fill = "name")+
scale_fill_viridis(discrete=TRUE) +
scale_color_viridis(discrete = TRUE) +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(names_filtered, aes(x = year,
y = n,
fill = fct_reorder2(name, year, n))) +
geom_smooth(aes(lty = fct_reorder2(name, year, n)), color = "black",
se = FALSE, lwd = .5, show.legend = FALSE) +
geom_point(size = 2, shape = 21) +
labs(x = "Year",
y = "Quantity",
fill = "name") +
scale_fill_grey(start = 0.3, end = 1)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
The colors itself are actually good, but the story behind the graph is gone… I guess this could be an example of good intentions going completely wrong way…
library(viridis)
babynames_by_year +
scale_color_viridis()
babynames_by_year +
scale_color_viridis(option = "magma")
library(RColorBrewer)
babynames_by_year +
scale_color_viridis()
babynames_by_year +
scale_color_gradientn(colours = rev(brewer.pal(n=5, name="PuBuGn")))