Created
March 16, 2023 13:12
-
-
Save amzoss/f9e94e16914e2d04a8fc48810519dc3c to your computer and use it in GitHub Desktop.
Game of Thrones Advanced Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Game of Thromes Character Ratings - Advanced" | |
author: "Angela Zoss" | |
date: "3/16/2023" | |
output: html_document | |
--- | |
## Setup your environment | |
```{r} | |
# Load required libraries | |
library(tidyverse) | |
library(here) | |
here::i_am("Day 1/3-got_nyt_final.Rmd") | |
``` | |
## Load your data | |
```{r} | |
# data comes from https://int.nyt.com/newsgraphics/2017/2017-07-17-got-matrix/mean.json | |
# data based on ratings using tool at https://www.nytimes.com/interactive/2017/08/09/upshot/game-of-thrones-chart.html | |
got <- read_csv(here("data", "got_ratings.csv")) | |
``` | |
## Create a scatterplot of `moral` vs. `physical` | |
```{r} | |
``` | |
## Set the alpha aesthetic (which controls transparency) for all points to .75 | |
```{r} | |
# check ?geom_point to see names of the different options available | |
# where should the new option go? what layer? inside or outside of aesthetics? | |
``` | |
## Add a new geom that will display labels for each point | |
```{r} | |
# hint: check https://ggplot2.tidyverse.org/reference/#section-geoms for all geom options | |
``` | |
## Adjust label position to limit overlap with points | |
```{r} | |
# hint: look at ?geom_text for an option that might change the position slightly | |
# where should the new option go? what layer? inside or outside of aesthetics? | |
``` | |
## Inheritance: Add colors | |
```{r} | |
# add gender as a color three different ways: | |
# - points only | |
# - labels only | |
# - both | |
``` | |
## Turn off legend for text layer | |
```{r} | |
# hint: there is another property for the geom_text layer that might help | |
``` | |
## Switch direction of Y axis | |
```{r} | |
# hint: edits to axes often use scales | |
# check https://ggplot2.tidyverse.org/reference/#section-scales for pre-built scales | |
# that might help (focus on scales for the y-axis) | |
``` | |
## Add reference lines | |
```{r} | |
# hint: there are special geoms for straight lines; | |
# check https://ggplot2.tidyverse.org/reference/#section-layer-geoms for all geom options | |
``` | |
```{r} | |
# look at how ggplot2 is layering the plot; rearrange layers as needed | |
``` | |
## Change the theme | |
```{r} | |
# Try to find a theme closer to the NYT chart; | |
# see https://ggplot2.tidyverse.org/reference/#section-themes for built-in themes | |
``` | |
## Change axis limits | |
```{r} | |
# the chart might look better if the axes go all the way from 0 to 1; edit | |
# the axes to force them to include 0 and 1 | |
# hint: remember that changes to axes usually involve a scale | |
``` | |
## Annotate the axes with descriptive text | |
```{r} | |
# hint: geoms are typically best for chart elements involving data; | |
# look for another feature that can add text to charts | |
# https://ggplot2.tidyverse.org/reference/ | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "Game of Thromes Character Ratings - Advanced" | |
author: "Angela Zoss" | |
date: "3/16/2023" | |
output: html_document | |
--- | |
## Setup your environment | |
```{r} | |
# Load required libraries | |
library(tidyverse) | |
library(here) | |
here::i_am("Day 1/3-got_nyt_final.Rmd") | |
``` | |
## Load your data | |
```{r} | |
# data comes from https://int.nyt.com/newsgraphics/2017/2017-07-17-got-matrix/mean.json | |
# data based on ratings using tool at https://www.nytimes.com/interactive/2017/08/09/upshot/game-of-thrones-chart.html | |
got <- read_csv(here("data", "got_ratings.csv")) | |
``` | |
## Create a scatterplot of `moral` vs. `physical` | |
```{r} | |
ggplot(data = got, | |
mapping = aes(x=moral,y=physical)) + | |
geom_point() | |
``` | |
## Set the alpha aesthetic (which controls transparency) for all points to .75 | |
```{r} | |
# check ?geom_point to see names of the different options available | |
# where should the new option go? what layer? inside or outside of aesthetics? | |
ggplot(got, aes(x=moral,y=physical)) + | |
geom_point(alpha=.75) | |
``` | |
## Add a new geom that will display labels for each point | |
```{r} | |
# hint: check https://ggplot2.tidyverse.org/reference/#section-geoms for all geom options | |
# wrong way: aes() in geom_point does not carry down; has to be in ggplot() or in both geom layers | |
#ggplot(got) + | |
# geom_point(aes(x=moral,y=physical)) + | |
# geom_text(aes(label=label)) | |
ggplot(got, aes(x=moral,y=physical)) + | |
geom_point(alpha=.75) + | |
geom_text(aes(label=label)) | |
``` | |
## Adjust label position to limit overlap with points | |
```{r} | |
# hint: look at ?geom_text for an option that might change the position slightly | |
# where should the new option go? what layer? inside or outside of aesthetics? | |
ggplot(got, aes(x=moral,y=physical)) + | |
geom_point(alpha=.75) + | |
geom_text(aes(label=label), nudge_y = -.025) | |
``` | |
## Inheritance: Add colors | |
```{r} | |
# add gender as a color three different ways: | |
# - points only | |
# - labels only | |
# - both | |
ggplot(got, aes(x=moral,y=physical)) + | |
geom_point(aes(color=gender), alpha=.75) + | |
geom_text(aes(label=label), nudge_y = -.025) | |
ggplot(got, aes(x=moral,y=physical)) + | |
geom_point(alpha=.75) + | |
geom_text(aes(label=label, color=gender), nudge_y = -.025) | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
geom_point(alpha=.75) + | |
geom_text(aes(label=label), nudge_y = -.025) | |
``` | |
## Turn off legend for text layer | |
```{r} | |
# hint: there is another property for the geom_text layer that might help | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
geom_point(alpha=.75) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) | |
``` | |
## Switch direction of Y axis | |
```{r} | |
# hint: edits to axes often use scales | |
# check https://ggplot2.tidyverse.org/reference/#section-scales for pre-built scales | |
# that might help (focus on scales for the y-axis) | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
scale_y_reverse() | |
``` | |
## Add reference lines | |
```{r} | |
# hint: there are special geoms for straight lines; | |
# check https://ggplot2.tidyverse.org/reference/#section-layer-geoms for all geom options | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
scale_y_reverse() + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5) | |
``` | |
```{r} | |
# look at how ggplot2 is layering the plot; rearrange layers as needed | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
scale_y_reverse() + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5)+ | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) | |
``` | |
## Change the theme | |
```{r} | |
# Try to find a theme closer to the NYT chart; | |
# see https://ggplot2.tidyverse.org/reference/#section-themes for built-in themes | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
scale_y_reverse() + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5)+ | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
theme_bw() | |
``` | |
## Change axis limits | |
```{r} | |
# the chart might look better if the axes go all the way from 0 to 1; edit | |
# the axes to force them to include 0 and 1 | |
# hint: remember that changes to axes usually involve a scale | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
scale_y_reverse(lim=c(1,0)) + | |
scale_x_continuous(limits=c(0,1)) + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5)+ | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
theme_bw() | |
``` | |
## Annotate the axes with descriptive text | |
```{r} | |
# hint: geoms are typically best for chart elements involving data; | |
# look for another feature that can add text to charts | |
# https://ggplot2.tidyverse.org/reference/ | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
scale_y_reverse(lim=c(1,0)) + | |
scale_x_continuous(limits=c(0,1)) + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5)+ | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
theme_bw() + | |
annotate(geom = "text", x=.5, y=1, label="Ugly") + | |
annotate(geom = "text", x=.5, y=0, label="Beautiful") + | |
annotate(geom = "text", x=0, y=.48, label="Evil") + | |
annotate(geom = "text", x=1, y=.52, label="Good") | |
# alternate syntax - combine all annotations into one layer | |
ggplot(got, aes(x=moral,y=physical, color=gender)) + | |
scale_y_reverse(lim=c(1,0)) + | |
scale_x_continuous(limits=c(0,1)) + | |
geom_hline(yintercept=.5) + | |
geom_vline(xintercept=.5)+ | |
geom_point(aes()) + | |
geom_text(aes(label=label), nudge_y = -.025, show.legend = FALSE) + | |
theme_bw() + | |
annotate(geom = "text", x=c(.5,.5,0,1), y=c(1,0,.48,.52), label=c("Ugly","Beautiful","Evil","Good")) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment