From 750ff19348d1a6777e1fea6cea29f0a3b7ed2937 Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Thu, 26 Dec 2024 15:36:54 -0700 Subject: [PATCH 1/7] fixing spacing --- modules/Data_Visualization/Data_Visualization.Rmd | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 7759a05a2..5bb5293fd 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -292,7 +292,7 @@ ggplot(Orange, aes(x = circumference, y = age, color = Tree)) + You can change look of each layer separately. Note the arguments like `linetype` and `alpha` that allow us to change the opacity of the points and style of the line respectively. -```{r, fig.width=5, fig.height=3, fig.align='center'} +```{r, fig.width=4.5, fig.height=2.5, fig.align='center'} ggplot(Orange, aes(x = circumference, y = age)) + geom_point(size = 5, color = "red", alpha = 0.5) + geom_line(size = 0.8, color = "black", linetype = 2) @@ -320,8 +320,11 @@ ggplot(Orange, aes(x = circumference, y = age)) + ## More themes! There's not only the built in ggplot2 themes but all kinds of themes from other packages! + - [ggthemes](https://jrnold.github.io/ggthemes/) + - [ThemePark package](https://github.com/MatthewBJane/ThemePark) + - [hrbr themes](https://github.com/hrbrmstr/hrbrthemes) ## Customize the look of the plot From 8966a4a354a82cf8fdff3f7248463a139fb7da0e Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Thu, 26 Dec 2024 15:41:37 -0700 Subject: [PATCH 2/7] removing and moving theme slides --- .../Data_Visualization/Data_Visualization.Rmd | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 5bb5293fd..581f84fb5 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -327,17 +327,6 @@ There's not only the built in ggplot2 themes but all kinds of themes from other - [hrbr themes](https://github.com/hrbrmstr/hrbrthemes) -## Customize the look of the plot - -You can change the look of whole plot - **specific elements, too** - like changing [font](http://www.cookbook-r.com/Graphs/Fonts/) and font size - or even more [fonts](https://blog.revolutionanalytics.com/2012/09/how-to-use-your-favorite-fonts-in-r-charts.html) - -```{r, fig.width=6, fig.height=3.5, fig.align='center'} -ggplot(Orange, aes(x = circumference, y = age)) + - geom_point(size = 5, color = "red", alpha = 0.5) + - geom_line(size = 0.8, color = "brown", linetype = 2) + - theme_bw() + - theme(text=element_text(size=16, family="Comic Sans MS")) -``` ## Adding labels {.codesmall} @@ -445,18 +434,7 @@ Orange %>% ggplot(aes(x = circumference, geom_line(size = 0.8) ``` -## Removing the legend label -You can use `theme(legend.position = "none")` to remove the legend. - -```{r, fig.width=5, fig.height=3, fig.align='center'} - -Orange %>% ggplot(aes(x = circumference, - y = age, - color = Tree)) + - geom_line(size = 0.8) + - theme(legend.position = "none") -``` ## Overwriting specifications @@ -973,3 +951,18 @@ knitr::include_graphics(here::here("images/the-end-g23b994289_1280.jpg")) ``` Image by Gerd Altmann from Pixabay + +## Extra Slides + +## Customize the look of the plot + +You can change the look of whole plot - **specific elements, too** - like changing [](http://www.cookbook-r.com/Graphs/Fonts/) and font size - or even more [fonts](https://blog.revolutionanalytics.com/2012/09/how-to-use-your-favorite-fonts-in-r-charts.html) + +```{r, fig.width=6, fig.height=3.5, fig.align='center'} +ggplot(Orange, aes(x = circumference, y = age)) + + geom_point(size = 5, color = "red", alpha = 0.5) + + geom_line(size = 0.8, color = "brown", linetype = 2) + + theme_bw() + + theme(text=element_text(size=16, family="Comic Sans MS")) +``` + From 57ade06ec0b128c6f4347f2b5749931b3143e904 Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Thu, 26 Dec 2024 15:47:51 -0700 Subject: [PATCH 3/7] some rearrangements --- .../Data_Visualization/Data_Visualization.Rmd | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 581f84fb5..758edc231 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -332,7 +332,7 @@ There's not only the built in ggplot2 themes but all kinds of themes from other The `labs()` function can help you add or modify titles on your plot. The `title` argument specifies the title. The `x` argument specifies the x axis label. The `y` argument specifies the y axis label. -```{r, fig.width=4, fig.height=2.5, fig.align='center'} +```{r, fig.width=4, fig.height=3, fig.align='center'} ggplot(Orange, aes(x = circumference, y = age)) + geom_point(size = 5, color = "red", alpha = 0.5) + geom_line(size = 0.8, color = "brown", linetype = 2) + @@ -345,7 +345,7 @@ ggplot(Orange, aes(x = circumference, y = age)) + Line breaks can be specified using `\n` within the `labs()` function to have a label with multiple lines. -```{r, fig.width=4, fig.height=2.5, fig.align='center'} +```{r, fig.width=4, fig.height=3, fig.align='center'} ggplot(Orange, aes(x = circumference, y = age)) + geom_point(size = 5, color = "red", alpha = 0.5) + geom_line(size = 0.8, color = "brown", linetype = 2) + @@ -368,19 +368,7 @@ plot_scale <-ggplot(Orange, aes(x = circumference, y = age)) + scale_y_continuous(breaks = seq(from = 100, to = 1600, by = 200)) ``` -## Changing axis: specifying axis limits - -`xlim()` and `ylim()` can specify the limits for each axis -```{r, fig.width=5, fig.height=3, fig.align='center'} - -ggplot(Orange, aes(x = circumference, y = age)) + - geom_point(size = 5, color = "red", alpha = 0.5) + - geom_line(size = 0.8, color = "brown", linetype = 2) + - labs(title = "My plot of orange tree circumference vs age") + - xlim(100, max(pull(Orange, circumference))) - -``` ## Changing axis: specifying axis scale {.codesmall} @@ -408,6 +396,7 @@ ggplot(Orange, mapping = aes(x = circumference, y = age)) + ``` + ## Modifying plot objects You can add to a plot object to make changes! Note that we can save our plots as an object like `plt1` below. And now if we reference `plt1` again our plot will print out! From 3a5aa329d819bef884b4ce6efffec15246cc7eb9 Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Fri, 27 Dec 2024 11:55:34 -0700 Subject: [PATCH 4/7] fix typo --- modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd b/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd index af6bbaf42..51f74fd1d 100644 --- a/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd +++ b/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd @@ -41,7 +41,7 @@ bike_agg ### 1.1 -Use `ggplot2` package make plot of average length of lanes (`lane_avg_length`; y-axis) for each year (`dateInstalled`; x-axis). You can use lines layer (`+ geom_line()`) or points layer (`+ geom_point()`), or both! +Use the `ggplot2` package to make plot of average length of lanes (`lane_avg_length`; y-axis) for each year (`dateInstalled`; x-axis). You can use lines layer (`+ geom_line()`) or points layer (`+ geom_point()`), or both! Assign the plot to variable `my_plot`. Type `my_plot` in the console to have it displayed. From 575d6aef561483581ff1155e9b62c785af613067 Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Fri, 27 Dec 2024 12:11:37 -0700 Subject: [PATCH 5/7] adding some DaSEH updates --- .../Data_Visualization/Data_Visualization.Rmd | 20 ++++++++++++++++++- .../lab/Data_Visualization_Lab_Key.Rmd | 17 +++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 758edc231..380d58efb 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -436,7 +436,17 @@ Orange %>% ggplot(aes(x = circumference, geom_line(size = 0.8, color = "black") ``` +## GUT CHECK: If we get an empty plot what might we need to do? +A. Add a `plot_` layer like `plot_point()` +B. Add a `geom_` layer like `geom_point()` + + +## GUT CHECK: How do we add more layers in ggplot2 plots? +A. `%>%` +B. `&` + +C. `+ ## Summary @@ -452,9 +462,12 @@ Orange %>% ggplot(aes(x = circumference, ## Lab 1 -🏠 [Class Website](https://jhudatascience.org/intro_to_r/)\ +🏠 [Class Website](https://jhudatascience.org/intro_to_r/) + 💻 [Lab](https://jhudatascience.org/intro_to_r/modules//Data_Visualization/lab/Data_Visualization_Lab.Rmd) +📃[Day 6 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-6.pdf) + ## theme() function: The `theme()` function can help you modify various elements of your plot. Here we will adjust the font size of the plot title. @@ -933,8 +946,13 @@ Check out this [guide](https://jhudatascience.org/tidyversecourse/dataviz.html#m ## Lab 2 🏠 [Class Website](https://jhudatascience.org/intro_to_r/)\ + 💻 [Lab](https://jhudatascience.org/intro_to_r/modules//Data_Visualization/lab/Data_Visualization_Lab.Rmd) +📃[Day 6 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-6.pdf) + +📃[Posit's theme cheatsheet](https://github.com/claragranell/ggplot2/blob/main/ggplot_theme_system_cheatsheet.pdf) + ```{r, fig.alt="The End", out.width = "50%", echo = FALSE, fig.align='center'} knitr::include_graphics(here::here("images/the-end-g23b994289_1280.jpg")) ``` diff --git a/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd b/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd index 51f74fd1d..cf516d221 100644 --- a/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd +++ b/modules/Data_Visualization/lab/Data_Visualization_Lab_Key.Rmd @@ -11,12 +11,10 @@ knitr::opts_chunk$set(echo = TRUE) # Part 1 -Load the libraries +Load the packages. ```{r, message=FALSE} -library(readr) -library(ggplot2) -library(dplyr) +library(tidyverse) library(jhur) ``` @@ -64,7 +62,7 @@ my_plot ### 1.2 -"Update" your plot by adding a title and changing the x and y axis titles. +"Update" your plot by adding a title and changing the x and y axis titles. (Hint: use the `labs` function.) ```{r 1.2response} my_plot <- my_plot + @@ -93,6 +91,13 @@ my_plot <- my_plot + breaks = c(2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013) ) +my_plot + +my_plot <- my_plot + + scale_x_continuous( + breaks = seq(from = 2006, to = 2013, by = 1) + ) + my_plot ``` @@ -176,6 +181,8 @@ Redo the above plot by adding a faceting (`+ facet_wrap( ~ type, ncol = 3)`) to (You may see `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?` warning as some bike lane types will have only 1 point plotted while trying to plot a line). Assign the new plot as an object called `facet_plot`. +Try adjusting the number of columns in the `facet_wrap` to see how this changes the plot. + ```{r 2.3response} facet_plot <- ggplot(bike_agg_2, aes( From 15808e3b027f46a98c7e9b9317a745f96b3d056d Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Fri, 27 Dec 2024 12:13:39 -0700 Subject: [PATCH 6/7] adding more gut checks --- .../Data_Visualization/Data_Visualization.Rmd | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 380d58efb..55e14627a 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -439,11 +439,14 @@ Orange %>% ggplot(aes(x = circumference, ## GUT CHECK: If we get an empty plot what might we need to do? A. Add a `plot_` layer like `plot_point()` + B. Add a `geom_` layer like `geom_point()` ## GUT CHECK: How do we add more layers in ggplot2 plots? + A. `%>%` + B. `&` C. `+ @@ -927,6 +930,20 @@ ggsave(filename = "saved_plot.png", # will save in working directory width = 6, height = 3.5) # by default in inches ``` + +## GUT CHECK: How to we make sure that the boxplots are filled with color instead of just the outside boarder? + +A. Use the `fill` argument in the `aes` specification + +B. Use `color` argument in `geom_boxplot()` + +## GUT CHECK: If our plot is too complicated to read, what might be a good option to fix this? + +A. add more `theme()` layers + +B. Use `facet_grid()` to split the plot up + + ## Summary - The `theme()` function helps you specify aspects about your plot From c22ef4f9317108c68fefff8b90ecc226fa1a777d Mon Sep 17 00:00:00 2001 From: carriewright11 Date: Fri, 27 Dec 2024 12:25:30 -0700 Subject: [PATCH 7/7] fixing cheatsheet day --- modules/Data_Visualization/Data_Visualization.Rmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/Data_Visualization/Data_Visualization.Rmd b/modules/Data_Visualization/Data_Visualization.Rmd index 55e14627a..a971c44b9 100644 --- a/modules/Data_Visualization/Data_Visualization.Rmd +++ b/modules/Data_Visualization/Data_Visualization.Rmd @@ -469,7 +469,7 @@ C. `+ 💻 [Lab](https://jhudatascience.org/intro_to_r/modules//Data_Visualization/lab/Data_Visualization_Lab.Rmd) -📃[Day 6 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-6.pdf) +📃[Day 7 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-7.pdf) ## theme() function: @@ -966,7 +966,7 @@ Check out this [guide](https://jhudatascience.org/tidyversecourse/dataviz.html#m 💻 [Lab](https://jhudatascience.org/intro_to_r/modules//Data_Visualization/lab/Data_Visualization_Lab.Rmd) -📃[Day 6 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-6.pdf) +📃[Day 7 Cheatsheet](https://jhudatascience.org/intro_to_r//modules/cheatsheets/Day-7.pdf) 📃[Posit's theme cheatsheet](https://github.com/claragranell/ggplot2/blob/main/ggplot_theme_system_cheatsheet.pdf)