From dbe965486b0706ad6f77a8f1c42e74774fd4dad6 Mon Sep 17 00:00:00 2001 From: albhasan Date: Fri, 3 Nov 2023 10:05:19 -0300 Subject: [PATCH] Closes #142 --- episodes/07-plot-ggplot2.Rmd | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/episodes/07-plot-ggplot2.Rmd b/episodes/07-plot-ggplot2.Rmd index 03d25e28..75c7834b 100644 --- a/episodes/07-plot-ggplot2.Rmd +++ b/episodes/07-plot-ggplot2.Rmd @@ -25,21 +25,21 @@ library(dplyr) ``` Plotting our data is one of the best ways to quickly explore it and the various -relationships between variables. There are three main plotting systems in R, the -[base plotting system](https://www.statmethods.net/graphs/), the +relationships between variables. There are three main plotting systems in R, +the [base plotting system](https://www.statmethods.net/graphs/), the [lattice](https://www.statmethods.net/advgraphs/trellis.html) package, and the -[ggplot2](https://www.statmethods.net/advgraphs/ggplot2.html) package. Today and -tomorrow we'll be learning about the ggplot2 package, because it is the most -effective for creating publication quality graphics. In this episode, we will -introduce the key features of a ggplot and make a few example plots. We will -expand on these concepts and see how they apply to geospatial data types when we -start working with geospatial data in the [R for Raster and Vector +[ggplot2](https://www.statmethods.net/advgraphs/ggplot2.html) package. Today +and tomorrow we'll be learning about the ggplot2 package, because it is one of +the most popular for creating publication quality graphics. In this episode, we +will introduce the key features of a ggplot and make a few example plots. We +will expand on these concepts and see how they apply to geospatial data types +when we start working with geospatial data in the [R for Raster and Vector Data](https://datacarpentry.org/r-raster-vector-geospatial/) lesson. ::::::::::::::::::::::::::::::::::::::: instructor - This episode introduces `geom_col` and `geom_histogram`. These geoms are used - in the rest of the workshop, along with geoms specifically for geospatial + in the rest of the workshop, along with geoms specifically for geospatial data. - Emphasize that we will go much deeper into visualization and creating publication-quality graphics later in the workshop. @@ -73,7 +73,7 @@ for histograms. ```{r lifeExp-vs-gdpPercap-scatter, fig.cap="Histogram of life expectancy by country showing bimodal distribution with modes at 45 and 75", message=FALSE} library("ggplot2") -ggplot(data = gapminder, aes(x = lifeExp)) + +ggplot(data = gapminder, aes(x = lifeExp)) + geom_histogram() ``` @@ -89,7 +89,7 @@ tells `ggplot` we want to visually represent the distribution of one variable (in our case "lifeExp"): ```{r lifeExp-vs-gdpPercap-scatter2, fig.cap="Histogram of life expectancy by country showing bimodal distribution with modes at 45 and 75"} -ggplot(data = gapminder, aes(x = lifeExp)) + +ggplot(data = gapminder, aes(x = lifeExp)) + geom_histogram() ``` @@ -106,7 +106,7 @@ expectancy: ## Solution to challenge 1 ```{r ch1-sol} -ggplot(data = gapminder, aes(x = gdpPercap)) + +ggplot(data = gapminder, aes(x = gdpPercap)) + geom_histogram() ``` @@ -131,7 +131,7 @@ We will plot countries on the x-axis (listed in alphabetic order by default) and gdp per capita on the y-axis. ```{r hist-subset-gapminder, fig.cap="Barplot of GDP per capita. Country names on x-axis overlap and are not readable"} -ggplot(data = gapminder_small, aes(x = country, y = gdpPercap)) + +ggplot(data = gapminder_small, aes(x = country, y = gdpPercap)) + geom_col() ``` @@ -140,7 +140,7 @@ x-axis labels. A quick fix to this is the add the `coord_flip()` function to the end of our plot code. ```{r hist-subset-gapminder-flipped, fig.cap="Barplot showing GDP per capita. Country names on the y-axis are readable"} -ggplot(data = gapminder_small, aes(x = country, y = gdpPercap)) + +ggplot(data = gapminder_small, aes(x = country, y = gdpPercap)) + geom_col() + coord_flip() ``` @@ -185,10 +185,10 @@ The default behavior for `postion` in `geom_col()` is "stack". ```{r gpd-per-cap} -ggplot(gapminder_small_2, - aes(x = country, y = gdpPercap, +ggplot(gapminder_small_2, + aes(x = country, y = gdpPercap, fill = as.factor(year))) + - geom_col(position = "dodge") + + geom_col(position = "dodge") + coord_flip() ```