diff --git a/block016_secrets-happy-graphing.html b/block016_secrets-happy-graphing.html index 06e2abf2..565e7193 100644 --- a/block016_secrets-happy-graphing.html +++ b/block016_secrets-happy-graphing.html @@ -10,20 +10,15 @@ + Secrets of a happy graphing life - + - - - - + + + + + + + + @@ -71,13 +95,36 @@ color: inherit; background-color: rgba(0, 0, 0, 0.04); } -img { - max-width:100%; - height: auto; +img { + max-width:100%; + height: auto; +} +.tabbed-pane { + padding-top: 12px; +} +button.code-folding-btn:focus { + outline: none; } + + +
+ + + + + + + + + +

Tidying and reshaping

-

This is an entire topic – multiple topics, in fact – covered elsewhere.

-

We covered tidyr in class meeting 012.

-

See the lesson contributed to Data Carpentry for a worked example of tidying data.

+

This is an entire topic covered elsewhere:

+

Tidy data using Lord of the Rings

Factor management

-

This is an entire topic, covered elsewhere.

-

See Be the boss of your factors to learn how to take charge of factor levels and their order and for how to map old levels into new ones. You’ll also see demos of the downstream pay-offs, e.g. more effective figures.

+

This is an entire topic covered elsewhere:

+

Be the boss of your factors

Worked example

-

Inspired by this question from a student when we first started using ggplot2: How can I focus in on country, Japan for example, and plot all the quantitative variables against year?

+

Inspired by this question from a student when we first started using ggplot2: How can I focus in on country, Japan for example, and plot all the quantitative variables against year?

Your first instinct might be to filter the Gapminder data for Japan and then loop over the variables, creating separate plots which need to be glued together. And, indeed, this can be done. But in my opinion, the data reshaping route is more “R native” given our current ecosystem, than the loop way.

Reshape your data

We filter the Gapminder data and keep only Japan. Then we gather up the variables pop, lifeExp, and gdpPercap into a single value variable, with a companion variable key.

-
suppressPackageStartupMessages(library(tidyr))
-japan_dat <- gapminder %>%
+
japan_dat <- gapminder %>%
   filter(country == "Japan")
 japan_tidy <- japan_dat %>%
   gather(key = var, value = value, pop, lifeExp, gdpPercap)
-dim(japan_dat)
-
## [1] 12  6
-
dim(japan_tidy)
-
## [1] 36  5
+dim(japan_dat) +#> [1] 12 6 +dim(japan_tidy) +#> [1] 36 5

The filtered japan_dat has 12 rows. Since we are gathering or stacking three variables in japan_tidy, it makes sense to see three times as many rows, namely 36 in the reshaped result.

Iterate over the variables via facetting

-

Now that we have the data we need in a tidy data.frame, with a proper factor representing the variables we want to “iterate” over, we just have to facet.

+

Now that we have the data we need in a tidy data frame, with a proper factor representing the variables we want to “iterate” over, we just have to facet.

p <- ggplot(japan_tidy, aes(x = year, y = value)) +
   facet_wrap(~ var, scales="free_y")
 p + geom_point() + geom_line() +
   scale_x_continuous(breaks = seq(1950, 2011, 15))
-

+

Recap

@@ -226,9 +304,10 @@

Recap

scale_x_continuous(breaks = seq(1950, 2011, 15))

This snippet demonstrates the payoffs from the rules we laid out at the start:

    -
  • We isolate the Japan data into its own data.frame.
  • -
  • We reshape the data. It’s a classic case of tidying. We gather three columns into one, because we want to depict them via position along the y-axis in the plot.
  • +
  • We isolate the Japan data into its own data frame.
  • +
  • We reshape the data. We gather three columns into one, because we want to depict them via position along the y-axis in the plot.
  • We use a factor to distinguish the observations that belong in each mini-plot, which then becomes a simple application of facetting.
  • +
  • This is an example of expedient data reshaping. I don’t actually believe that gdpPercap, lifeExp, and pop naturally belong together in one variable. But gathering them was by far the easiest way to get this plot.
@@ -237,6 +316,8 @@

Recap

This work is licensed under the CC BY-NC 3.0 Creative Commons License.
+ + diff --git a/block016_secrets-happy-graphing.md b/block016_secrets-happy-graphing.md index d0d1cb67..8a9cd889 100644 --- a/block016_secrets-happy-graphing.md +++ b/block016_secrets-happy-graphing.md @@ -2,80 +2,95 @@ +### Load gapminder and the tidyverse + + +```r +library(gapminder) +library(tidyverse) +#> Loading tidyverse: ggplot2 +#> Loading tidyverse: tibble +#> Loading tidyverse: tidyr +#> Loading tidyverse: readr +#> Loading tidyverse: purrr +#> Loading tidyverse: dplyr +#> Conflicts with tidy packages ---------------------------------------------- +#> filter(): dplyr, stats +#> lag(): dplyr, stats +``` + + ### Hidden data wrangling problems -If you are struggling to make a figure, don't assume it's a problem between you and `ggplot2`. Stop and ask yourself which of these rules you are breaking: +If you are struggling to make a figure, don't assume it's a problem between you and ggplot2. Stop and ask yourself which of these rules you are breaking: - * Keep stuff in data.frames - * Keep your data.frames *tidy*; be willing to reshape your data often + * Keep stuff in data frames + * Keep your data frames *tidy*; be willing to reshape your data often * Use factors and be the boss of them In my experience, the vast majority of graphing agony is due to insufficient data wrangling. Tackle your latent data storage and manipulation problems and your graphing problem often melts away. -### Keep stuff in data.frames +### Keep stuff in data frames -I see a fair amount of student code where variables are *copied out* of a data.frame, to exist as stand-alone objects in the workspace. +I see a fair amount of student code where variables are *copied out* of a data frame, to exist as stand-alone objects in the workspace. ```r -library(gapminder) life_exp <- gapminder$lifeExp year <- gapminder$year ``` -Problem is, `ggplot2` has an incredibly strong preference for variables in data.frames; it is virtually a requirement for the main data.frame underpinning a plot. +Problem is, ggplot2 has an incredibly strong preference for variables in data frames; it is virtually a requirement for the main data frame underpinning a plot. ```r -library(ggplot2) ggplot(aes(x = year, y = life_exp)) + geom_jitter() +#> Error: ggplot2 doesn't know how to deal with data of class uneval ``` -``` -## Error: ggplot2 doesn't know how to deal with data of class uneval -``` - -**Just leave the variables in place and pass the associated data.frame!** This advice applies to base and `lattice` graphics as well. It is not specific to `ggplot2`. +**Just leave the variables in place and pass the associated data frame!** This advice applies to base and `lattice` graphics as well. It is not specific to ggplot2. ```r ggplot(data = gapminder, aes(x = year, y = life_exp)) + geom_jitter() ``` -![](block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png) +![](block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png) -What if we wanted to filter the data by country, continent, or year? This is much easier to do safely if all affected variables live together in a data.frame, not as individual objects that can get "out of sync." +What if we wanted to filter the data by country, continent, or year? This is much easier to do safely if all affected variables live together in a data frame, not as individual objects that can get "out of sync." -Don't write-off `ggplot2` as a highly opinionated outlier! In fact, keeping data in data.frames and computing and visualizing it *in situ* are widely regarded as best practices. The option to pass a data frame via `data =` is a common feature of many high-use R functions, e.g. `lm()`, `aggregate()`, `plot()`, and `t.test()`, so make this your default *modus operandi*. +Don't write-off ggplot2 as a highly opinionated outlier! In fact, keeping data in data frames and computing and visualizing it *in situ* are widely regarded as best practices. The option to pass a data frame via `data =` is a common feature of many high-use R functions, e.g. `lm()`, `aggregate()`, `plot()`, and `t.test()`, so make this your default *modus operandi*. -#### Explicit data.frame creation via `dplyr::data_frame()` +#### Explicit data frame creation via `tibble::tibble()` and `tribble()` -If your data is already lying around and it's __not__ in a data.frame, ask yourself "why not?". Did you create those variables? Maybe you should have created them in a data.frame in the first place! The new `data_frame()` function in `dplyr` is an improved version of the built-in `data.frame()`, which makes it possible to define one variable in terms of another and that won't mangle your imports via coercion. Specifically, strings will NOT be converted to factors unless you explicitly request that. This removes my most common excuses for data.frame procrastination and avoidance. +If your data is already lying around and it's __not__ in a data frame, ask yourself "why not?". Did you create those variables? Maybe you should have created them in a data frame in the first place! The `tibble()` function is an improved version of the built-in `data.frame()`, which makes it possible to define one variable in terms of another and which won't turn character data into factor. If constructing tiny tibbles "by hand", `tribble()` can be an even handier function, in which your code will be laid out like the table you are creating. These functions should remove the most common excuses for data frame procrastination and avoidance. ```r -suppressPackageStartupMessages(library(dplyr)) my_dat <- - data_frame(x = 1:5, - y = x ^ 2, - text = c("alpha", "beta", "gamma", "delta", "epsilon")) + tibble(x = 1:5, + y = x ^ 2, + text = c("alpha", "beta", "gamma", "delta", "epsilon")) +## if you're truly "hand coding", tribble() is an alternative +my_dat <- tribble( + ~ x, ~ y, ~ text, + 1, 1, "alpha", + 2, 4, "beta", + 3, 9, "gamma", + 4, 16, "delta", + 5, 25, "epsilon" +) str(my_dat) -``` - -``` -## Classes 'tbl_df', 'tbl' and 'data.frame': 5 obs. of 3 variables: -## $ x : int 1 2 3 4 5 -## $ y : num 1 4 9 16 25 -## $ text: chr "alpha" "beta" "gamma" "delta" ... -``` - -```r +#> Classes 'tbl_df', 'tbl' and 'data.frame': 5 obs. of 3 variables: +#> $ x : num 1 2 3 4 5 +#> $ y : num 1 4 9 16 25 +#> $ text: chr "alpha" "beta" "gamma" "delta" ... ggplot(my_dat, aes(x, y)) + geom_line() + geom_text(aes(label = text)) ``` -![](block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png) +![](block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png) -Together with `dplyr::mutate()`, which adds new variables to a data.frame, this gives you the tools to work within data.frames whenever you're handling related variables of the same length. +Together with `dplyr::mutate()`, which adds new variables to a data frame, this gives you the tools to work within data frames whenever you're handling related variables of the same length. #### Sidebar: `with()` @@ -84,66 +99,60 @@ Sadly, not all functions offer a `data =` argument. Take `cor()`, for example, w ```r cor(year, lifeExp, data = gapminder) +#> Error in cor(year, lifeExp, data = gapminder): unused argument (data = gapminder) ``` -``` -## Error in cor(year, lifeExp, data = gapminder): unused argument (data = gapminder) -``` - -Sure, you can always just repeat the data.frame name like so: +Sure, you can always just repeat the data frame name like so: ```r cor(gapminder$year, gapminder$lifeExp) -``` - -``` -## [1] 0.4356112 +#> [1] 0.4356112 ``` but people hate typing. I suspect subconscious dread of repeatedly typing `gapminder` is what motivates those who copy variables into stand-alone objects in the workspace. -The `with()` function is much better workaround. Provide the data.frame as the first argument. The second argument is an expression that will be evaluated in a special environment. It could be a single command or a multi-line snippet of code. What's special is that you can refer to variables in the data.frame by name. +The `with()` function is a better workaround. Provide the data frame as the first argument. The second argument is an expression that will be evaluated in a special environment. It could be a single command or a multi-line snippet of code. What's special is that you can refer to variables in the data frame by name. ```r with(gapminder, cor(year, lifeExp)) +#> [1] 0.4356112 ``` -``` -## [1] 0.4356112 -``` - -If you use the `magrittr` package, another option is to use the `%$%` operator to expose the variables inside a data.frame for further computation: +If you use the `magrittr` package, another option is to use the `%$%` operator to expose the variables inside a data frame for further computation: ```r library(magrittr) +#> +#> Attaching package: 'magrittr' +#> The following object is masked from 'package:purrr': +#> +#> set_names +#> The following object is masked from 'package:tidyr': +#> +#> extract gapminder %$% cor(year, lifeExp) -``` - -``` -## [1] 0.4356112 +#> [1] 0.4356112 ``` ### Tidying and reshaping -*This is an entire topic -- multiple topics, in fact -- covered elsewhere.* +This is an entire topic covered elsewhere: -*We covered [`tidyr`](https://cran.r-project.org/web/packages/tidyr/index.html) in [class meeting 012](http://stat545-ubc.github.io/cm012_tidy-data-factors.html).* - -*See [the lesson contributed to Data Carpentry](http://stat545-ubc.github.io/bit002_tidying-lotr-data.html) for a worked example of tidying data.* +[Tidy data using Lord of the Rings](https://github.com/jennybc/lotr-tidy#readme) ### Factor management -*This is an entire topic, covered elsewhere.* - -See [Be the boss of your factors](block014_factors.html) to learn how to take charge of factor levels and their order and for how to map old levels into new ones. You'll also see demos of the downstream pay-offs, e.g. more effective figures. +This is an entire topic covered elsewhere: + +[Be the boss of your factors](block029_factors.html) ### Worked example -Inspired by this question from a student when we first started using `ggplot2`: How can I focus in on country, Japan for example, and plot all the quantitative variables against year? +Inspired by this question from a student when we first started using ggplot2: How can I focus in on country, Japan for example, and plot all the quantitative variables against year? Your first instinct might be to filter the Gapminder data for Japan and then loop over the variables, creating separate plots which need to be glued together. And, indeed, this can be done. But in my opinion, the data reshaping route is more "R native" given our current ecosystem, than the loop way. @@ -153,31 +162,21 @@ We filter the Gapminder data and keep only Japan. Then we *gather* up the variab ```r -suppressPackageStartupMessages(library(tidyr)) japan_dat <- gapminder %>% filter(country == "Japan") japan_tidy <- japan_dat %>% gather(key = var, value = value, pop, lifeExp, gdpPercap) dim(japan_dat) -``` - -``` -## [1] 12 6 -``` - -```r +#> [1] 12 6 dim(japan_tidy) -``` - -``` -## [1] 36 5 +#> [1] 36 5 ``` The filtered `japan_dat` has 12 rows. Since we are gathering or stacking three variables in `japan_tidy`, it makes sense to see three times as many rows, namely 36 in the reshaped result. #### Iterate over the variables via facetting -Now that we have the data we need in a tidy data.frame, with a proper factor representing the variables we want to "iterate" over, we just have to facet. +Now that we have the data we need in a tidy data frame, with a proper factor representing the variables we want to "iterate" over, we just have to facet. ```r @@ -187,7 +186,7 @@ p + geom_point() + geom_line() + scale_x_continuous(breaks = seq(1950, 2011, 15)) ``` -![](block016_secrets-happy-graphing_files/figure-html/japan-1.png) +![](block016_secrets-happy-graphing_files/figure-html/japan-1.png) #### Recap @@ -206,6 +205,7 @@ ggplot(japan_tidy, aes(x = year, y = value)) + This snippet demonstrates the payoffs from the rules we laid out at the start: - * We isolate the Japan data into its own __data.frame__. - * We __reshape__ the data. It's a classic case of __tidying__. We gather three columns into one, because we want to depict them via position along the y-axis in the plot. + * We isolate the Japan data into its own __data frame__. + * We __reshape__ the data. We gather three columns into one, because we want to depict them via position along the y-axis in the plot. * We use a __factor__ to distinguish the observations that belong in each mini-plot, which then becomes a simple application of facetting. + * This is an example of expedient data reshaping. I don't actually believe that `gdpPercap`, `lifeExp`, and `pop` naturally belong together in one variable. But gathering them was by far the easiest way to get this plot. diff --git a/block016_secrets-happy-graphing.rmd b/block016_secrets-happy-graphing.rmd index 10003b6a..ca3a83c8 100644 --- a/block016_secrets-happy-graphing.rmd +++ b/block016_secrets-happy-graphing.rmd @@ -7,61 +7,75 @@ output: --- ```{r setup, include = FALSE, cache = FALSE} -knitr::opts_chunk$set(error = TRUE) +knitr::opts_chunk$set(error = TRUE, collapse = TRUE, comment = "#>") ``` +### Load gapminder and the tidyverse + +```{r} +library(gapminder) +library(tidyverse) +``` + + ### Hidden data wrangling problems -If you are struggling to make a figure, don't assume it's a problem between you and `ggplot2`. Stop and ask yourself which of these rules you are breaking: +If you are struggling to make a figure, don't assume it's a problem between you and ggplot2. Stop and ask yourself which of these rules you are breaking: - * Keep stuff in data.frames - * Keep your data.frames *tidy*; be willing to reshape your data often + * Keep stuff in data frames + * Keep your data frames *tidy*; be willing to reshape your data often * Use factors and be the boss of them In my experience, the vast majority of graphing agony is due to insufficient data wrangling. Tackle your latent data storage and manipulation problems and your graphing problem often melts away. -### Keep stuff in data.frames +### Keep stuff in data frames -I see a fair amount of student code where variables are *copied out* of a data.frame, to exist as stand-alone objects in the workspace. +I see a fair amount of student code where variables are *copied out* of a data frame, to exist as stand-alone objects in the workspace. ```{r} -library(gapminder) life_exp <- gapminder$lifeExp year <- gapminder$year ``` -Problem is, `ggplot2` has an incredibly strong preference for variables in data.frames; it is virtually a requirement for the main data.frame underpinning a plot. +Problem is, ggplot2 has an incredibly strong preference for variables in data frames; it is virtually a requirement for the main data frame underpinning a plot. ```{r} -library(ggplot2) ggplot(aes(x = year, y = life_exp)) + geom_jitter() ``` -**Just leave the variables in place and pass the associated data.frame!** This advice applies to base and `lattice` graphics as well. It is not specific to `ggplot2`. +**Just leave the variables in place and pass the associated data frame!** This advice applies to base and `lattice` graphics as well. It is not specific to ggplot2. ```{r data-in-situ} ggplot(data = gapminder, aes(x = year, y = life_exp)) + geom_jitter() ``` -What if we wanted to filter the data by country, continent, or year? This is much easier to do safely if all affected variables live together in a data.frame, not as individual objects that can get "out of sync." +What if we wanted to filter the data by country, continent, or year? This is much easier to do safely if all affected variables live together in a data frame, not as individual objects that can get "out of sync." -Don't write-off `ggplot2` as a highly opinionated outlier! In fact, keeping data in data.frames and computing and visualizing it *in situ* are widely regarded as best practices. The option to pass a data frame via `data =` is a common feature of many high-use R functions, e.g. `lm()`, `aggregate()`, `plot()`, and `t.test()`, so make this your default *modus operandi*. +Don't write-off ggplot2 as a highly opinionated outlier! In fact, keeping data in data frames and computing and visualizing it *in situ* are widely regarded as best practices. The option to pass a data frame via `data =` is a common feature of many high-use R functions, e.g. `lm()`, `aggregate()`, `plot()`, and `t.test()`, so make this your default *modus operandi*. -#### Explicit data.frame creation via `dplyr::data_frame()` +#### Explicit data frame creation via `tibble::tibble()` and `tribble()` -If your data is already lying around and it's __not__ in a data.frame, ask yourself "why not?". Did you create those variables? Maybe you should have created them in a data.frame in the first place! The new `data_frame()` function in `dplyr` is an improved version of the built-in `data.frame()`, which makes it possible to define one variable in terms of another and that won't mangle your imports via coercion. Specifically, strings will NOT be converted to factors unless you explicitly request that. This removes my most common excuses for data.frame procrastination and avoidance. +If your data is already lying around and it's __not__ in a data frame, ask yourself "why not?". Did you create those variables? Maybe you should have created them in a data frame in the first place! The `tibble()` function is an improved version of the built-in `data.frame()`, which makes it possible to define one variable in terms of another and which won't turn character data into factor. If constructing tiny tibbles "by hand", `tribble()` can be an even handier function, in which your code will be laid out like the table you are creating. These functions should remove the most common excuses for data frame procrastination and avoidance. ```{r data_frame-love} -suppressPackageStartupMessages(library(dplyr)) my_dat <- - data_frame(x = 1:5, - y = x ^ 2, - text = c("alpha", "beta", "gamma", "delta", "epsilon")) + tibble(x = 1:5, + y = x ^ 2, + text = c("alpha", "beta", "gamma", "delta", "epsilon")) +## if you're truly "hand coding", tribble() is an alternative +my_dat <- tribble( + ~ x, ~ y, ~ text, + 1, 1, "alpha", + 2, 4, "beta", + 3, 9, "gamma", + 4, 16, "delta", + 5, 25, "epsilon" +) str(my_dat) ggplot(my_dat, aes(x, y)) + geom_line() + geom_text(aes(label = text)) ``` -Together with `dplyr::mutate()`, which adds new variables to a data.frame, this gives you the tools to work within data.frames whenever you're handling related variables of the same length. +Together with `dplyr::mutate()`, which adds new variables to a data frame, this gives you the tools to work within data frames whenever you're handling related variables of the same length. #### Sidebar: `with()` @@ -71,21 +85,21 @@ Sadly, not all functions offer a `data =` argument. Take `cor()`, for example, w cor(year, lifeExp, data = gapminder) ``` -Sure, you can always just repeat the data.frame name like so: +Sure, you can always just repeat the data frame name like so: ```{r} cor(gapminder$year, gapminder$lifeExp) ``` but people hate typing. I suspect subconscious dread of repeatedly typing `gapminder` is what motivates those who copy variables into stand-alone objects in the workspace. -The `with()` function is much better workaround. Provide the data.frame as the first argument. The second argument is an expression that will be evaluated in a special environment. It could be a single command or a multi-line snippet of code. What's special is that you can refer to variables in the data.frame by name. +The `with()` function is a better workaround. Provide the data frame as the first argument. The second argument is an expression that will be evaluated in a special environment. It could be a single command or a multi-line snippet of code. What's special is that you can refer to variables in the data frame by name. ```{r} with(gapminder, cor(year, lifeExp)) ``` -If you use the `magrittr` package, another option is to use the `%$%` operator to expose the variables inside a data.frame for further computation: +If you use the `magrittr` package, another option is to use the `%$%` operator to expose the variables inside a data frame for further computation: ```{r} library(magrittr) @@ -95,21 +109,19 @@ gapminder %$% ### Tidying and reshaping -*This is an entire topic -- multiple topics, in fact -- covered elsewhere.* - -*We covered [`tidyr`](https://cran.r-project.org/web/packages/tidyr/index.html) in [class meeting 012](http://stat545-ubc.github.io/cm012_tidy-data-factors.html).* +This is an entire topic covered elsewhere: -*See [the lesson contributed to Data Carpentry](http://stat545-ubc.github.io/bit002_tidying-lotr-data.html) for a worked example of tidying data.* +[Tidy data using Lord of the Rings](https://github.com/jennybc/lotr-tidy#readme) ### Factor management -*This is an entire topic, covered elsewhere.* - -See [Be the boss of your factors](block014_factors.html) to learn how to take charge of factor levels and their order and for how to map old levels into new ones. You'll also see demos of the downstream pay-offs, e.g. more effective figures. +This is an entire topic covered elsewhere: + +[Be the boss of your factors](block029_factors.html) ### Worked example -Inspired by this question from a student when we first started using `ggplot2`: How can I focus in on country, Japan for example, and plot all the quantitative variables against year? +Inspired by this question from a student when we first started using ggplot2: How can I focus in on country, Japan for example, and plot all the quantitative variables against year? Your first instinct might be to filter the Gapminder data for Japan and then loop over the variables, creating separate plots which need to be glued together. And, indeed, this can be done. But in my opinion, the data reshaping route is more "R native" given our current ecosystem, than the loop way. @@ -118,7 +130,6 @@ Your first instinct might be to filter the Gapminder data for Japan and then loo We filter the Gapminder data and keep only Japan. Then we *gather* up the variables `pop`, `lifeExp`, and `gdpPercap` into a single `value` variable, with a companion variable `key`. ```{r} -suppressPackageStartupMessages(library(tidyr)) japan_dat <- gapminder %>% filter(country == "Japan") japan_tidy <- japan_dat %>% @@ -131,7 +142,7 @@ The filtered `japan_dat` has `r nrow(japan_dat)` rows. Since we are gathering or #### Iterate over the variables via facetting -Now that we have the data we need in a tidy data.frame, with a proper factor representing the variables we want to "iterate" over, we just have to facet. +Now that we have the data we need in a tidy data frame, with a proper factor representing the variables we want to "iterate" over, we just have to facet. ```{r japan} p <- ggplot(japan_tidy, aes(x = year, y = value)) + @@ -156,6 +167,7 @@ ggplot(japan_tidy, aes(x = year, y = value)) + This snippet demonstrates the payoffs from the rules we laid out at the start: - * We isolate the Japan data into its own __data.frame__. - * We __reshape__ the data. It's a classic case of __tidying__. We gather three columns into one, because we want to depict them via position along the y-axis in the plot. + * We isolate the Japan data into its own __data frame__. + * We __reshape__ the data. We gather three columns into one, because we want to depict them via position along the y-axis in the plot. * We use a __factor__ to distinguish the observations that belong in each mini-plot, which then becomes a simple application of facetting. + * This is an example of expedient data reshaping. I don't actually believe that `gdpPercap`, `lifeExp`, and `pop` naturally belong together in one variable. But gathering them was by far the easiest way to get this plot. diff --git a/block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png b/block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png index 6f68b42c..4bbd063d 100644 Binary files a/block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png and b/block016_secrets-happy-graphing_files/figure-html/data-in-situ-1.png differ diff --git a/block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png b/block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png index 127261e8..f9a2d8f4 100644 Binary files a/block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png and b/block016_secrets-happy-graphing_files/figure-html/data_frame-love-1.png differ diff --git a/block016_secrets-happy-graphing_files/figure-html/japan-1.png b/block016_secrets-happy-graphing_files/figure-html/japan-1.png index baa6703c..2ee0eaa2 100644 Binary files a/block016_secrets-happy-graphing_files/figure-html/japan-1.png and b/block016_secrets-happy-graphing_files/figure-html/japan-1.png differ diff --git a/topics.html b/topics.html index b3936922..f8ea9595 100644 --- a/topics.html +++ b/topics.html @@ -175,7 +175,7 @@

Topic list

  • Data analysis 2: vectors and files
  • diff --git a/topics.md b/topics.md index 086d0c35..5c108067 100644 --- a/topics.md +++ b/topics.md @@ -18,7 +18,7 @@ This material gets updated during Sept - Nov each year. Right now we're in awkwa - [Table look up](bit008_lookup.html) - [Split-Apply-Combine](block024_group-nest-split-map.html) with the tidyverse (nesting, list-columns, map inside mutate) *under development*. * Data analysis 2: vectors and files - - [Be the boss of your factors](block029_factors.html) *under development, in light of forcats* + - [Be the boss of your factors](block029_factors.html) *under redevelopment, in light of forcats* - [Character vectors](block028_character-data.html): basic manipulation and regular expressions. - [Writing and reading files](block026_file-out-in.html) * R as a programming language