Skip to content

Commit

Permalink
update slide design
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jan 22, 2024
1 parent 99f37f6 commit 798cbf3
Show file tree
Hide file tree
Showing 66 changed files with 16,406 additions and 15,292 deletions.
50 changes: 50 additions & 0 deletions assignments/HW2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "Homework 2"
---

```{r global_options, include=FALSE}
library(knitr)
library(tidyverse)
opts_chunk$set(fig.align="center", fig.height=4, fig.width=5.5)
# data prep:
txhouse <- txhousing %>%
filter(city %in% c('Austin', 'Houston', 'San Antonio', 'Dallas')) %>%
filter(year %in% c('2000', '2005', '2010', '2015')) %>%
group_by(city, year) %>%
summarize(total_sales = sum(sales))
```

**This homework is due on Feb. 1, 2024 at 11:00pm. Please submit as a pdf file on Canvas.**

**Problem 1: (6 pts)** For this Problem you will be working with the `iris` dataset built into R. This data set contains measurements of flowers (sepal length, sepal width, petal length, petal width) for three different *Iris* species (*I. setosa*, *I. versicolor*, *I. virginica*).

```{r}
head(iris)
```

Use ggplot to make a histogram of the `Sepal.Length` column. Manually choose appropriate values for `binwidth` and `center`. Explain your choice of values in 2-3 sentences.

```{r}
# Your code goes here.
```

*Your explanation goes here.*

**Problem 2: (6 pts)** For this problem you will work with the dataset `txhouse` that has been derived from the `txhousing` dataset provided by **ggplot2**. See here for details of the original dataset: https://ggplot2.tidyverse.org/reference/txhousing.html. `txhouse` contains three columns: `city` (listing four Texas cities), `year` (containing four years between 2000 and 2015) and `total_sales` indicating the total number of sales for the specified year and city.

```{r}
txhouse
```

Use ggplot to make a bar plot of the total housing sales (column `total_sales`) for each `year`, color the bar borders "gray34", and fill the bars by `city`.

```{r}
# Your code goes here.
```

**Problem 3: (8 pts)** Modify the plot from Problem 2 by placing `city` bars side-by-side, rather than stacked. See Slide 35 from the lecture on visualizing amounts. Next, reorder the bars for each `year` by `total_sales` in descending order. See Slide 25 from the lecture on visualizing amounts.

```{r}
# Your code goes here.
```
462 changes: 462 additions & 0 deletions assignments/HW2.html

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion schedule.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ All homeworks are due by 11:00pm on the day they are due. Homeworks need to be s

<p class="nospace">Materials:</p>

- coming soon
- [R Markdown template](assignments/HW2.Rmd)
- [HTML](assignments/HW2.html)

### Homework 3 (due Feb 8, 2024)

Expand Down
56 changes: 28 additions & 28 deletions slides/coordinate-systems-axes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ tempnormals <- read_csv(here("datasets", "tempnormals.csv"))
## Most data visualizations use Cartesian coordinates

.center[
```{r cartesian-coord, echo = FALSE, fig.width = 5.5, fig.asp = 0.8, dev = "svg"}
```{r cartesian-coord, echo = FALSE, fig.width = 7.5, fig.asp = 0.8, dev = "svg"}
df_points <- data.frame(x = c(-1, 0, 2),
y = c(-1, 0, 1),
label = c("(–1, –1)", "(0, 0)", "(2, 1)"),
Expand All @@ -55,9 +55,9 @@ ggplot(df_points, aes(x, y)) +
linetype = 2) +
geom_point(size = 3, color = "#0072B2") +
geom_text(aes(label = label, vjust = vjust, hjust = hjust),
size = 14/.pt) +
size = 16/.pt) +
geom_text(data = df_labels, aes(label = label, hjust = hjust, vjust = vjust),
size = 14/.pt) +
size = 16/.pt) +
coord_fixed(xlim = c(-2.2, 3.2), ylim = c(-2.2, 2.2), expand = FALSE) +
scale_x_continuous(
name = "x axis",
Expand All @@ -69,7 +69,7 @@ ggplot(df_points, aes(x, y)) +
breaks = -2:2,
labels = c("–2", "–1", "0", "1", "2")
) +
theme_minimal_grid(16) +
theme_minimal_grid(18) +
theme(
axis.ticks.length = grid::unit(0, "pt")
)
Expand All @@ -85,7 +85,7 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
## Changing units does not change the plot

.pull-left[
```{r temperature-normals-Houston-San-Diego, echo = FALSE, fig.width = 5, fig.asp = 1, dev = "svg"}
```{r temperature-normals-Houston-San-Diego, echo = FALSE, fig.width = 6, fig.asp = 1, dev = "svg"}
temps_wide <- tempnormals %>%
pivot_wider(
id_cols = c("month_name", "month", "day", "date", "day_of_year"),
Expand Down Expand Up @@ -137,7 +137,7 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
--

.pull-right[
```{r temperature-normals-Houston-San-Diego-Celsius, echo = FALSE, fig.width = 5, fig.asp = 1, dev = "svg"}
```{r temperature-normals-Houston-San-Diego-Celsius, echo = FALSE, fig.width = 6, fig.asp = 1, dev = "svg"}
# Fahrenheit to Celsius conversion
F2C <- function(t) {(t-32)*5/9}
Expand Down Expand Up @@ -166,7 +166,7 @@ ggplot(temps_wide_label, aes(x = F2C(`San Diego`), y = F2C(Houston))) +
## If scale units are unrelated, aspect ratio is arbitrary

.center[
```{r temperature-normals-Houston, echo = FALSE, fig.width = 5*6/4.2, fig.asp = 3/4, dev = "svg"}
```{r temperature-normals-Houston, echo = FALSE, fig.width = 9, fig.asp = 3/4, dev = "svg"}
temp_plot <- ggplot(temps_wide_label, aes(x = date, y = Houston)) +
geom_line(linewidth = 1, color = "#0072B2") +
scale_x_date(
Expand All @@ -183,7 +183,7 @@ temp_plot <- ggplot(temps_wide_label, aes(x = date, y = Houston)) +
limits = c(50, 90),
name = "temperature (°F)"
) +
theme_minimal_grid(14)
theme_minimal_grid(16)
plot_grid(
plot_grid(
Expand Down Expand Up @@ -218,13 +218,13 @@ Visualize these five values: 1, &nbsp; 3.16, &nbsp; 10, &nbsp; 31.6, &nbsp; 100
--

.center.nogap[
```{r linear-log-scales, echo = FALSE, fig.width = 8, fig.asp = (1/3)*0.5, dev = "svg"}
```{r linear-log-scales, echo = FALSE, fig.width = 10, fig.asp = (1/3)*0.5, dev = "svg"}
df <- tibble(x = c(1, 3.16, 10, 31.6, 100))
xaxis_lin <- ggplot(df, aes(x, y = 1)) +
geom_point(size = 3, color = "#0072B2") +
scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1) +
theme_minimal_grid(16, rel_large = 1) +
theme_minimal_grid(18, rel_large = 1) +
theme(
axis.ticks.length = grid::unit(0, "pt"),
axis.text.y = element_blank(),
Expand All @@ -236,7 +236,7 @@ xaxis_lin <- ggplot(df, aes(x, y = 1)) +
xaxis_log <- ggplot(df, aes(log10(x), y = 1)) +
geom_point(size = 3, color = "#0072B2") +
scale_y_continuous(limits = c(0.8, 1.2), expand = c(0, 0), breaks = 1) +
theme_minimal_grid(16, rel_large = 1) +
theme_minimal_grid(18, rel_large = 1) +
theme(
axis.ticks.length = grid::unit(0, "pt"),
axis.text.y = element_blank(),
Expand Down Expand Up @@ -273,7 +273,7 @@ ggdraw(plotlist[[1]])
--

.center.nogap[
```{r linear-log-scales2, echo = FALSE, fig.width = 8, fig.asp = (1/3)*0.5, dev = "svg"}
```{r linear-log-scales2, echo = FALSE, fig.width = 10, fig.asp = (1/3)*0.5, dev = "svg"}
ggdraw(plotlist[[2]])
```
]
Expand All @@ -282,7 +282,7 @@ ggdraw(plotlist[[2]])
--

.center.nogap[
```{r linear-log-scales3, echo = FALSE, fig.width = 8, fig.asp = (1/3)*0.5, dev = "svg"}
```{r linear-log-scales3, echo = FALSE, fig.width = 10, fig.asp = (1/3)*0.5, dev = "svg"}
ggdraw(plotlist[[3]])
```
]
Expand All @@ -294,7 +294,7 @@ ggdraw(plotlist[[3]])
A linear scale emphasizes large counties

.center.nogap[
```{r texas-counties-linear, echo = FALSE, message = FALSE, fig.width = 7, fig.asp = 0.6, dev = "svg"}
```{r texas-counties-linear, echo = FALSE, message = FALSE, fig.width = 9, fig.asp = 0.6, dev = "svg"}
US_census <- read_csv(here("datasets", "US_census.csv"))
library(ggrepel)
Expand All @@ -316,7 +316,7 @@ ggplot(tx_counties, aes(x = index, y = popratio)) +
geom_point(size = 0.5, color = "#0072B2") +
geom_text_repel(
aes(label = label_large),
size = 10/.pt,
size = 14/.pt,
point.padding = .4, color = "black",
min.segment.length = 0,
max.overlaps = 1000
Expand All @@ -327,7 +327,7 @@ ggplot(tx_counties, aes(x = index, y = popratio)) +
breaks = NULL, #c(1, 50*(1:5)),
name = "Texas counties, from most to least populous"
) +
theme_minimal_hgrid(14) +
theme_minimal_hgrid(16) +
theme(axis.line = element_blank())
```
]
Expand All @@ -345,13 +345,13 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
A log scale shows symmetry around the median

.center.nogap[
```{r texas-counties-log, echo = FALSE, fig.width = 7, fig.asp = 0.6, dev = "svg"}
```{r texas-counties-log, echo = FALSE, fig.width = 9, fig.asp = 0.6, dev = "svg"}
ggplot(tx_counties, aes(x = index, y = popratio)) +
geom_hline(yintercept = 1, linetype = 2, color = "grey40") +
geom_point(size = 0.5, color = "#0072B2") +
geom_text_repel(
aes(label = label),
size = 10/.pt,
size = 14/.pt,
point.padding = .4, color = "black",
min.segment.length = 0,
max.overlaps = 1000
Expand All @@ -366,7 +366,7 @@ ggplot(tx_counties, aes(x = index, y = popratio)) +
breaks = NULL, #c(1, 50*(1:5)),
name = "Texas counties, from most to least populous"
) +
theme_minimal_hgrid(14) +
theme_minimal_hgrid(16) +
theme(axis.line = element_blank())
```
]
Expand All @@ -381,7 +381,7 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
## Nonlinear coordinate systems: Polar coordinates

.pull-left[
```{r cartesian-polar-left, echo = FALSE, fig.width = 5, fig.asp = 1, dev = "svg"}
```{r cartesian-polar-left, echo = FALSE, fig.width = 6, fig.asp = 1, dev = "svg"}
df_points <- tibble(
x = c(1, 3.5, 0),
y = c(3, 4, 0),
Expand Down Expand Up @@ -430,7 +430,7 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
--

.pull-right[
```{r cartesian-polar-right, echo = FALSE, fig.width = 5, fig.asp = 1, dev = "svg"}
```{r cartesian-polar-right, echo = FALSE, fig.width = 6, fig.asp = 1, dev = "svg"}
ggplot(df_points, aes(x, y)) +
geom_segment(
data = df_segments,
Expand Down Expand Up @@ -467,7 +467,7 @@ ggplot(df_points, aes(x, y)) +
## Cartesian vs polar example

.pull-left[
```{r temp-normals-vs-time-cartesian, echo=FALSE, message=FALSE, fig.width = 5, fig.asp = 0.618, dev="svg"}
```{r temp-normals-vs-time-cartesian, echo=FALSE, message=FALSE, fig.width = 6.5, fig.asp = 0.618, dev="svg"}
temps_long <- read_csv(here("datasets", "tempnormals.csv")) %>%
mutate(
location = factor(
Expand Down Expand Up @@ -495,7 +495,7 @@ ggplot(temps_long, aes(x = date, y = temperature, color = location)) +
name = NULL,
values = c("#E69F00", "#56B4E9", "#009E73", "#CC79A7")
) +
theme_minimal_grid(14)
theme_minimal_grid(16)
```
]

Expand All @@ -506,7 +506,7 @@ Figure redrawn from [Claus O. Wilke. Fundamentals of Data Visualization. O'Reill
--

.pull-right[
```{r temp-normals-vs-time-polar, echo=FALSE, message=FALSE, fig.width = 5, fig.asp = 0.618, dev="svg"}
```{r temp-normals-vs-time-polar, echo=FALSE, message=FALSE, fig.width = 6.5, fig.asp = 0.618, dev="svg"}
ggplot(temps_long, aes(x = date, y = temperature, color = location)) +
geom_line(linewidth = 1) +
scale_x_date(name = "date", expand = c(0, 0)) +
Expand All @@ -521,7 +521,7 @@ ggplot(temps_long, aes(x = date, y = temperature, color = location)) +
values = c("#E69F00", "#56B4E9", "#009E73", "#CC79A7")
) +
coord_polar(theta = "x") +
theme_minimal_grid(14)
theme_minimal_grid(16)
```
]

Expand Down Expand Up @@ -844,7 +844,7 @@ ggplot(temperatures, aes(day_of_year, temperature, color = location)) +
```

.center[
```{r temperatures-cartesian-out, echo = FALSE, ref.label="temperatures-cartesian", fig.width = 5.5, fig.asp = 0.618, dev = "svg"}
```{r temperatures-cartesian-out, echo = FALSE, ref.label="temperatures-cartesian", fig.width = 6.5, fig.asp = 0.618, dev = "svg"}
```
]]
Expand All @@ -862,7 +862,7 @@ ggplot(temperatures, aes(day_of_year, temperature, color = location)) +
```

.center[
```{r temperatures-polar-out, echo = FALSE, ref.label="temperatures-polar", fig.width = 5.5, fig.asp = 0.618, dev = "svg"}
```{r temperatures-polar-out, echo = FALSE, ref.label="temperatures-polar", fig.width = 6.5, fig.asp = 0.618, dev = "svg"}
```
]]
Expand All @@ -881,7 +881,7 @@ ggplot(temperatures, aes(day_of_year, temperature, color = location)) +
```

.center[
```{r temperatures-polar2-out, echo = FALSE, ref.label="temperatures-polar2", fig.width = 5.5, fig.asp = 0.618, dev = "svg"}
```{r temperatures-polar2-out, echo = FALSE, ref.label="temperatures-polar2", fig.width = 6.5, fig.asp = 0.618, dev = "svg"}
```
]]
Expand Down
2 changes: 1 addition & 1 deletion slides/coordinate-systems-axes.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
### Claus O. Wilke
]
.date[
### last updated: 2023-01-19
### last updated: 2024-01-22
]

---
Expand Down
Loading

0 comments on commit 798cbf3

Please sign in to comment.