
```{R}
library(tidyverse)
library(HumanitiesDataAnalysis)
```


```{R}
populations = CESTA |>
  pivot_longer(`1790`:`2010`, "year", values_to = "population") |>
  mutate(year = year |> as.numeric()) |>
  ggplot() |>
  KEEP_GOING()
```

Change the code that shows population for NY, MA, NV, NY, TX, and VA to
use color instead of faceting to show changes in population.

```{R}
populations |>
  filter(ST %in% c("NY", "MA", "CA", "TX", "VA", "NV")) |>
  group_by(ST, year) |>
  filter(population > 0) |>
  summarize(pop = sum(population)) |>
  ggplot() +
  geom_line() +
  aes(x = year, y = pop) +
  scale_y_log10() +
  facet_wrap(~ST)
```

Using the `rank` function inside a function and `facet`, make a
`geom_text` plot that ranks the top ten cities for each census. (Hint:
You will need to group by year and sort)

```{R}
KEEP_GOING()
```

Modify the plot that shows book years so that instead of looking at the
years books are written, you’re seeing the dates in which they *died*.

```{R}
books |>
  clean_books() |>
  ggplot() +
  KEEP_GOING()
```

Make a scatterplot using this data and the grammar of graphics.

It should:

1.  Have the x-axis represent birth year.
2.  Have the y-axis represent publication year.
3.  Use the `geom_point` function to make a point chart.

<div class="cell"
hash="Visualizing_Data_cache/json/unnamed-chunk-30_584510265d891453f481783098ac2e38">

```{R}
books |>
  clean_books() |>
  ggplot() +
  KEEP_GOING()
```


```{R}
books |>
  clean_books() |>
  ggplot() +
  KEEP_GOING()
```

<div class="cell-output-stderr">

</div>

<div class="cell-output-display">

![](Visualizing_Data_files/figure-json/unnamed-chunk-30-1.png)

</div>

</div>

Now let’s look at some plots where we perform data transformations
first.

Translate the following set of instructions into code:

1.  Clean the books set;
2.  Count the number of authors;
3.  Limit the list to the most authors;
4.  Draw a bar chart (`geom_col`) of the counts data you have made.

```{R}
books |> 
  clean_books() |>
  group_by(author) |>
  KEEP_GOING()
```

Recall this code from the text that tidies the data so that each year is
a population: you may need to run it yourself.

```{R}
populations = CESTA |>
  pivot_longer(`1790`:`2010`, "year", values_to = "population") |>
  mutate(year = year |> as.numeric()) |> select(City, ST, year, population, LAT, LON)

```

The chart below shows the combined population of California and
Michigan. Make it instead show them *separately*, represented by either
the `color` or `lty` aesthetic. (You’ll need to change the data analysis
code before the ggplot function, and the plotting code after.)

<div class="cell"
hash="Visualizing_Data_cache/json/unnamed-chunk-34_9da8277b0edbdbb06abe186888bf38d1">

```{R}
populations |> filter(ST %in% c("CA", "MI")) |>
  group_by(year) |> 
  summarize(population = sum(population)) |>
  ggplot() + 
  geom_line() + 
  aes(x=year, y = population)
```


```{R}
populations |> filter(ST %in% c("CA", "MI")) |>
  group_by(year) |> 
  summarize(population = sum(population)) |>
  ggplot() + 
  geom_line() + 
  aes(x=year, y = population)
```

<div class="cell-output-display">

![](Visualizing_Data_files/figure-json/unnamed-chunk-34-1.png)

</div>

</div>

In what year was NYC the maximum share of the national urban population?

<div class="cell"
hash="Visualizing_Data_cache/json/unnamed-chunk-36_9822f9cd214efa2046077702991a6029">

```{R}
populations |>
  group_by(year) |>
  mutate(total_population = sum(population, na.rm = TRUE)) |>
  filter(City == "New York City" & ST == "NY") |>
  KEEP_GOING()
```


```{R}
populations |>
  group_by(year) |>
  mutate(total_population = sum(population, na.rm = TRUE)) |>
  filter(City == "New York City" & ST == "NY") |>
  KEEP_GOING()
```

</div>

There’s something funny going on in that chart between 1880 and 1920.
Can you figure out what it is? If so, make a chart that explains why the
shape of the line in the previous chart isn’t to be believed.

<div class="cell"
hash="Visualizing_Data_cache/json/unnamed-chunk-38_82586dffe71471c4aa0b559ccb173152">

</div>

Make a map of the United States in which each city is colorized by the
year that it had its maximum population according to the census data.
(Hard, very skippable.) ::: {.cell
hash=‘Visualizing_Data_cache/json/unnamed-chunk-40_c0ba6801c8c585c73a768aeef64c61df’}

## Free exercise

At this point, you should be able to find some kind of tabular dataset
that’s a little bit interesting to you. Load it in, and start to explore
it through a number of plots.

In previous chapters, we’ve laid out a number of functions for you to
work with data frames.

(On data frames)

1.  `filter`
2.  `group_by`
3.  `summarize`
4.  `mutate`
5.  `count`
6.  `arrange`
7.  `mutate`

(Inside summary/mutate calls.)

1.  `sum`
2.  `n`
3.  `mean`
4.  `str_extract` (inside mutate)
5.  `str_replace` (inside mutate)

Use at least 7 of these functions to create multiple different views of
your dataset. Be sure to use the `labs` function to put a title on top
of your dataset that provides some interpretation of it.

<div class="cell"
hash="Visualizing_Data_cache/json/unnamed-chunk-42_5f391ccc1c2d718c4cb9c745bf17a512">

</div>
