diff --git a/public/2023-08-delta/search.json b/public/2023-08-delta/search.json index 5501606c..86c0d4eb 100644 --- a/public/2023-08-delta/search.json +++ b/public/2023-08-delta/search.json @@ -137,7 +137,7 @@ "href": "session_03.html#overview", "title": "3  Data Visualization", "section": "3.1 Overview", - "text": "3.1 Overview\nggplot2 is a popular package for visualizing data in R. From the home page:\n\nggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.\n\nIt’s been around for years and has pretty good documentation and tons of example code around the web (like on StackOverflow). The goal of this lesson is to explain the fundamentals of how ggplot2 work, introduce useful functions for customizing your plots and inspire you to go and explore this awesome resource for visualizing your data.\n\n\n\n\n\n\nggplot2 vs base graphics in R vs others\n\n\n\nThere are many different ways to plot your data in R. All of them work! However, ggplot2 excels at making complicated plots easy and easy plots simple enough\nBase R graphics (plot(), hist(), etc) can be helpful for simple, quick and dirty plots. ggplot2 can be used for almost everything else.\n\n\nLet’s dive into creating and customizing plots with ggplot2.\n\n\n\n\n\n\nSetup\n\n\n\n\nMake sure you’re in the right project (training_{USERNAME}) and use the Git workflow by Pulling to check for any changes. Then, create a new Quarto document, delete the default text, and save this document.\nLoad the packages we’ll need:\n\n\nlibrary(readr)\nlibrary(dplyr)\nlibrary(tidyr)\nlibrary(forcats) # makes working with factors easier\nlibrary(ggplot2)\nlibrary(leaflet) # interactive maps\nlibrary(DT) # interactive tables\nlibrary(scales) # scale functions for visualization\nlibrary(janitor) # expedite cleaning and exploring data\n\n\nLoad the data directly from the EDI Data Repository: Sacramento-San Joaquin Delta Socioecological Monitoring. Navegate to the link above, scroll down and under Resources, hover over the “Download” button for the “Socioecological monitoring data”, right click, and select “Copy Link Address”.\n\n\ndelta_visits <- read_csv(\"https://portal.edirepository.org/nis/dataviewer?packageid=edi.587.1&entityid=cda8c1384af0089b506d51ad8507641f\") %>% \n clean_names()\n\n\nLearn about the data. For this session we are going to be working with data on Socioecological Monitoring on the Sacramento-San Joaquin Delta. Check out the documentation.\nFinally, let’s explore the data we just read into our working environment.\n\n\n## Check out column names\ncolnames(delta_visits)\n\n## Peak at each column and class\nglimpse(delta_visits)\n\n## From when to when\nrange(delta_visits$date)\n\n## First and last rows\nhead(delta_visits)\ntail(delta_visits)\n\n## Which time of day?\nunique(delta_visits$time_of_day)" + "text": "3.1 Overview\nggplot2 is a popular package for visualizing data in R. From the home page:\n\nggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.\n\nIt’s been around for years and has pretty good documentation and tons of example code around the web (like on StackOverflow). The goal of this lesson is to explain the fundamentals of how ggplot2 work, introduce useful functions for customizing your plots and inspire you to go and explore this awesome resource for visualizing your data.\n\n\n\n\n\n\nggplot2 vs base graphics in R vs others\n\n\n\nThere are many different ways to plot your data in R. All of them work! However, ggplot2 excels at making complicated plots easy and easy plots simple enough\nBase R graphics (plot(), hist(), etc) can be helpful for simple, quick and dirty plots. ggplot2 can be used for almost everything else.\n\n\nLet’s dive into creating and customizing plots with ggplot2.\n\n\n\n\n\n\nSetup\n\n\n\n\nMake sure you’re in the right project (training_{USERNAME}) and use the Git workflow by Pulling to check for any changes. Then, create a new Quarto document, delete the default text, and save this document.\nLoad the packages we’ll need:\n\n\nlibrary(readr)\nlibrary(dplyr)\nlibrary(tidyr)\nlibrary(forcats) # makes working with factors easier\nlibrary(ggplot2)\nlibrary(leaflet) # interactive maps\nlibrary(DT) # interactive tables\nlibrary(scales) # scale functions for visualization\nlibrary(janitor) # expedite cleaning and exploring data\n\n\nLoad the data directly from the EDI Data Repository: Sacramento-San Joaquin Delta Socioecological Monitoring. Navegate to the link above, scroll down and under Resources, hover over the “Download” button for the “Socioecological monitoring data”, right click, and select “Copy Link Address”.\n\n\ndelta_visits <- read_csv(\"https://portal.edirepository.org/nis/dataviewer?packageid=edi.587.1&entityid=cda8c1384af0089b506d51ad8507641f\") %>% \n janitor::clean_names()\n\n\nLearn about the data. For this session we are going to be working with data on Socioecological Monitoring on the Sacramento-San Joaquin Delta. Check out the documentation.\nFinally, let’s explore the data we just read into our working environment.\n\n\n## Check out column names\ncolnames(delta_visits)\n\n## Peak at each column and class\nglimpse(delta_visits)\n\n## From when to when\nrange(delta_visits$date)\n\n## First and last rows\nhead(delta_visits)\ntail(delta_visits)\n\n## Which time of day?\nunique(delta_visits$time_of_day)" }, { "objectID": "session_03.html#getting-the-data-ready", @@ -151,14 +151,14 @@ "href": "session_03.html#plotting-with-ggplot2", "title": "3  Data Visualization", "section": "3.3 Plotting with ggplot2", - "text": "3.3 Plotting with ggplot2\n\n3.3.1 Essentials components\nFirst, we’ll cover some ggplot2 basics to create the foundation of our plot. Then, we’ll add on to make our great customized data visualization.\n\n\n\n\n\n\nThe basics\n\n\n\n\nIndicate we are using ggplot() (call the ggplot2::ggplot() function)\nWhat data do we want to plot? (data = my_data)\nWhat is my mapping aesthetics? What variables do we want to plot? (define usingaes() function)\nDefine the geometry of our plot. This specifies the type of plot we’re making (use geom_*() to indicate the type of plot e.g: point, bar, etc.)\n\nNote To add layers to our plot, for example, additional geometries/aesthetics and theme elements or any ggplot object we use +.\n\n\nNow, let’s plot total daily visits by restoration location. We will show this by creating the same plot in 3 slightly different ways. Each of the options below have the 4 essential pieces of a ggplot.\n\n## Option 1 - data and mapping called in the ggplot() function\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))+\n geom_col()\n\n\n## Option 2 - data called in ggplot function; mapping called in geom\nggplot(data = daily_visits_loc) +\n geom_col(aes(x = restore_loc, y = daily_visits))\n\n\n## Option 3 - data and mapping called in geom\nggplot() +\n geom_col(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))\n\nThey all will create the same plot:\n(Apologies for the crumble text on the x-axis, we will learn how to make this look better soon)\n\n\n\n\n\n\n\n3.3.2 Looking at different geoms\nHaving the basic structure with the essential components in mind, we can easily change the type of graph by updating the geom_*().\n\n\n\n\n\n\nggplot2 and the pipe operator\n\n\n\nJust like in dplyr and tidyr, we can also pipe a data.frame directly into the first argument of the ggplot function using the %>% operator.\nThis can certainly be convenient, but use it carefully! Combining too many data-tidying or subsetting operations with your ggplot call can make your code more difficult to debug and understand.\n\n\nWe will use the pipe operator to pass into ggplot() a filtered version of daily_visit_loc, and make a plot with different geometries.\nBoxplot Note: These examples are to demonstrate case uses of wrangling function proir to plotting. They are not necessarily plotting best practices.\n\ndaily_visits_loc %>%\n separate(date, c(\"year\", \"month\", \"day\"), sep = \"-\") %>%\n filter(daily_visits < 30,\n visitor_type %in% c(\"sm_boat\", \"med_boat\", \"lrg_boat\")) %>%\n ggplot(aes(x = visitor_type, y = daily_visits)) +\n geom_boxplot()\n\n\n\n\nViolin plot\n\ndaily_visits_loc %>%\n separate(date, c(\"year\", \"month\", \"day\"), sep = \"-\") %>%\n filter(daily_visits < 30,\n visitor_type %in% c(\"sm_boat\", \"med_boat\", \"lrg_boat\")) %>%\n ggplot(aes(x = visitor_type, y = daily_visits)) +\n geom_violin()\n\n\n\n\nLine and point\n\ndaily_visits_loc %>%\n filter(restore_loc == \"Decker Island\",\n visitor_type == \"med_boat\") %>%\n ggplot(aes(x = date, y = daily_visits)) +\n geom_line() +\n geom_point()\n\n\n\n3.3.3 Customizing our plot\nLet’s go back to our base bar graph. What if we want our bars to be blue instead of gray? You might think we could run this:\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = \"blue\"))+\n geom_col()\n\n\n\n\nWhy did that happen?\nNotice that we tried to set the fill color of the plot inside the mapping aesthetic call. What we have done, behind the scenes, is create a column filled with the word “blue” in our data frame, and then mapped it to the fill aesthetic, which then chose the default fill color of red.\nWhat we really wanted to do was just change the color of the bars. If we want do do that, we can call the color option in the geom_col() function, outside of the mapping aesthetics function call.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))+\n geom_col(fill = \"blue\")\n\n\n\n\nWhat if we did want to map the color of the bars to a variable, such as visitor_type. ggplot() is really powerful because we can easily get this plot to visualize more aspects of our data.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()\n\n\n\n\n\n\n\n\n\n\nKeep in mind\n\n\n\n\nIf you want to map a variable onto a graph aesthetic (e.g., point color should be based on a specific region), put it within aes().\nIf you want to update your plot base on a constant (e.g. “Make ALL the points BLUE”), you can add the information directly to the relevant geom_ layer.\n\n\n\n\n3.3.3.1 Setting ggplot themes\nWe have successfully plotted our data. But, this is clearly not a nice plot. Let’s work on making this plot look a bit nicer. We are going to”\n\nInclude a built in theme using theme_bw()\nFlip the x and y axis to better read the graph\nAdd a title, subtitle and adjust labels using labs()\n\nThere are a wide variety of built in themes in ggplot that help quickly set the look of the plot. Use the RStudio auto-complete theme_ <TAB> to view a list of theme functions.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")\n\n\n\n\nYou can see that the theme_bw() function changed a lot of the aspects of our plot! The background is white, the grid is a different color, etc. There are lots of other built in themes like this that come with the ggplot2 package.\n\n## Useful baseline themes are\ntheme_minimal()\ntheme_light()\ntheme_classic()\n\nThe built in theme functions (theme_*()) change the default settings for many elements that can also be changed individually using thetheme() function. The theme() function is a way to further fine-tune the look of your plot. This function takes MANY arguments (just have a look at ?theme). Luckily there are many great ggplot resources online so we don’t have to remember all of these, just Google “ggplot cheat sheet” and find one you like.\nLet’s look at an example of a theme() call, where we change the position of the legend from the right side to the bottom, and remove its title.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n theme(legend.position = \"bottom\")\n\n\n\n\nNote that the theme() call needs to come after any built-in themes like theme_bw() are used. Otherwise, theme_bw() will likely override any theme elements that you changed using theme().\nYou can also save the result of a series of theme() function calls to an object to use on multiple plots. This prevents needing to copy paste the same lines over and over again!\n\nmy_theme <- theme_bw(base_size = 14) +\n theme(legend.position = \"bottom\",\n axis.ticks.x = element_blank())\n\nSo now our code will look like this:\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n coord_flip()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\n\n\n\n\n\nExercise\n\n\n\nWhat changes do you expect to see in your plot by adding the following line of code? Discuss with your neighbor and then try it out!\nscale_y_continuous(breaks = seq(0,120, 20))\n\n\n\n\nAnswer\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\nFinally we are going to expand the bars all the way to the axis line. In other words, remove the gap between the bars and the vertical “x-axis” line.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\n\n3.3.3.2 Reordering things\nggplot() loves putting things in alphabetical order. But more frequent than not, that’s not the order you actually want things to be plotted. One way to do this is to use the fct_reorder() function from the forcats package. forcats provide tools for working with categorical variables. In this case, we want to reorder or categorical variable of “Restoration Location” base on the total number of visits.\nThe fist thing we need to do is to add a column to our data with the total number of visits by location.\n\ndaily_visits_totals <- daily_visits_loc %>% \n group_by(restore_loc) %>%\n mutate(n = sum(daily_visits)) %>% \n ungroup()\n\nhead(daily_visits_totals)\n\n# A tibble: 6 × 5\n restore_loc date visitor_type daily_visits n\n <chr> <date> <chr> <dbl> <dbl>\n1 Decker Island 2017-07-07 bank_angler 4 37\n2 Decker Island 2017-07-07 cars 0 37\n3 Decker Island 2017-07-07 lrg_boat 0 37\n4 Decker Island 2017-07-07 med_boat 6 37\n5 Decker Island 2017-07-07 scientist 0 37\n6 Decker Island 2017-07-07 sm_boat 0 37\n\n\nNext, we will run the code for our plot adding the fct_reorder() function.\n\nggplot(data = daily_visits_totals,\n aes(x = fct_reorder(restore_loc, n), y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\nWhat if you want to plot the other way around? In this case from least to greater? We add the desc() to the variable we are sorting by.\n\nggplot(data = daily_visits_totals,\n aes(x = fct_reorder(restore_loc, desc(n)), y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\n?? Add somthing about reordering the legend??\n\n\n3.3.3.3 Colors\nColor can be fun and overwhelming Introduce color pallets things to keep in mind (how many colors you need) Provide resources\n\n\n3.3.3.4 Saving plots\nUDPDATE CODE\nSaving plots using ggplot is easy! The ggsave() function will save either the last plot you created, or any plot that you have saved to a variable. You can specify what output format you want, size, resolution, etc. See ?ggsave() for documentation.\n\nggsave(\"figures/nyears_data_kus.jpg\", width = 8, height = 6, units = \"in\")\n\nWe can also save our facet plot showing annual delta_visitsments by region calling the plot’s object.\n\nggsave(annual_region_plot, \"figures/annual_esc_region.png\", width = 12, height = 8, units = \"in\")\n\n\n\n3.3.3.5 Creating multiple plots\nUPDATE TEXT\nWe know that in the graph we just plotted, each bar includes visit for multiple days. Let’s leverage the power of ggplot to plot more aspects of our data in one plot.\nWe are going to plot visits by species over time, from 2000 to 2016, for each region. \nAn easy way to plot another aspect of your data is using the function facet_wrap(). This function takes a mapping to a variable using the syntax ~{variable_name}. The ~ (tilde) is a model operator which tells facet_wrap() to model each unique value within variable_name to a facet in the plot.\nThe default behavior of facet wrap is to put all facets on the same x and y scale. You can use the scales argument to specify whether to allow different scales between facet plots (e.g scales = \"free_y\" to free the y axis scale). You can also specify the number of columns using the ncol = argument or number of rows using nrow =.\nADD TEXT ABOUT SCALES\n\nggplot(data = daily_visits_totals,\n aes(x = visitor_type, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n facet_wrap(~restore_loc,\n scales = \"free_y\",\n ncol = 5,\n nrow = 2)+\n labs(x = \"Type of visitor\",\n y = \"Number of Visits\",\n title = \"Total Number of Visits to Delta Restoration Areas\",\n subtitle = \"Sum of all visits during study period\")+\n theme_bw()+\n theme(legend.position = \"bottom\",\n axis.ticks.x = element_blank(),\n axis.text.x = element_blank())\n\n\n\n\n\n\n3.3.3.6 Smarter tick labels using scales\nFixing tick labels in ggplot can be super annoying. The y-axis labels in the plot above don’t look great. We could manually fix them, but it would likely be tedious and error prone.\nThe scales package provides some nice helper functions to easily rescale and relabel your plots. Here, we use scale_y_continuous() from ggplot2, with the argument labels, which is assigned to the function name comma, from the scales package. This will format all of the labels on the y-axis of our plot with comma-formatted numbers." + "text": "3.3 Plotting with ggplot2\n\n3.3.1 Essentials components\nFirst, we’ll cover some ggplot2 basics to create the foundation of our plot. Then, we’ll add on to make our great customized data visualization.\n\n\n\n\n\n\nThe basics\n\n\n\n\nIndicate we are using ggplot() (call the ggplot2::ggplot() function)\nWhat data do we want to plot? (data = my_data)\nWhat is my mapping aesthetics? What variables do we want to plot? (define usingaes() function)\nDefine the geometry of our plot. This specifies the type of plot we’re making (use geom_*() to indicate the type of plot e.g: point, bar, etc.)\n\nNote To add layers to our plot, for example, additional geometries/aesthetics and theme elements or any ggplot object we use +.\n\n\nNow, let’s plot total daily visits by restoration location. We will show this by creating the same plot in 3 slightly different ways. Each of the options below have the 4 essential pieces of a ggplot.\n\n## Option 1 - data and mapping called in the ggplot() function\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))+\n geom_col()\n\n\n## Option 2 - data called in ggplot function; mapping called in geom\nggplot(data = daily_visits_loc) +\n geom_col(aes(x = restore_loc, y = daily_visits))\n\n\n## Option 3 - data and mapping called in geom\nggplot() +\n geom_col(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))\n\nThey all will create the same plot:\n(Apologies for the crumble text on the x-axis, we will learn how to make this look better soon)\n\n\n\n\n\n\n\n3.3.2 Looking at different geoms\nHaving the basic structure with the essential components in mind, we can easily change the type of graph by updating the geom_*().\n\n\n\n\n\n\nggplot2 and the pipe operator\n\n\n\nJust like in dplyr and tidyr, we can also pipe a data.frame directly into the first argument of the ggplot function using the %>% operator.\nThis can certainly be convenient, but use it carefully! Combining too many data-tidying or subsetting operations with your ggplot call can make your code more difficult to debug and understand.\n\n\nWe will use the pipe operator to pass into ggplot() a filtered version of daily_visit_loc, and make a plot with different geometries.\nBoxplot Note: These examples are to demonstrate case uses of wrangling function prior to plotting. They are not necessarily plotting best practices.\n\ndaily_visits_loc %>%\n separate(date, c(\"year\", \"month\", \"day\"), sep = \"-\") %>%\n filter(daily_visits < 30,\n visitor_type %in% c(\"sm_boat\", \"med_boat\", \"lrg_boat\")) %>%\n ggplot(aes(x = visitor_type, y = daily_visits)) +\n geom_boxplot()\n\n\n\n\nViolin plot\n\ndaily_visits_loc %>%\n separate(date, c(\"year\", \"month\", \"day\"), sep = \"-\") %>%\n filter(daily_visits < 30,\n visitor_type %in% c(\"sm_boat\", \"med_boat\", \"lrg_boat\")) %>%\n ggplot(aes(x = visitor_type, y = daily_visits)) +\n geom_violin()\n\n\n\n\nLine and point\n\ndaily_visits_loc %>%\n filter(restore_loc == \"Decker Island\",\n visitor_type == \"med_boat\") %>%\n ggplot(aes(x = date, y = daily_visits)) +\n geom_line() +\n geom_point()\n\n\n\n3.3.3 Customizing our plot\nLet’s go back to our base bar graph. What if we want our bars to be blue instead of gray? You might think we could run this:\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = \"blue\"))+\n geom_col()\n\n\n\n\nWhy did that happen?\nNotice that we tried to set the fill color of the plot inside the mapping aesthetic call. What we have done, behind the scenes, is create a column filled with the word “blue” in our data frame, and then mapped it to the fill aesthetic, which then chose the default fill color of red.\nWhat we really wanted to do was just change the color of the bars. If we want do do that, we can call the color option in the geom_col() function, outside of the mapping aesthetics function call.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits))+\n geom_col(fill = \"blue\")\n\n\n\n\nWhat if we did want to map the color of the bars to a variable, such as visitor_type. ggplot() is really powerful because we can easily get this plot to visualize more aspects of our data.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()\n\n\n\n\n\n\n\n\n\n\nKeep in mind\n\n\n\n\nIf you want to map a variable onto a graph aesthetic (e.g., point color should be based on a specific region), put it within aes().\nIf you want to update your plot base on a constant (e.g. “Make ALL the points BLUE”), you can add the information directly to the relevant geom_ layer.\n\n\n\n\n3.3.3.1 Setting ggplot themes\nWe have successfully plotted our data. But, this is clearly not a nice plot. Let’s work on making this plot look a bit nicer. We are going to”\n\nAdd a title, subtitle and adjust labels using labs()\nFlip the x and y axis to better read the graph\nInclude a built in theme using theme_bw()\n\nThere are a wide variety of built in themes in ggplot that help quickly set the look of the plot. Use the RStudio auto-complete theme_ <TAB> to view a list of theme functions.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during July 2017 and March 2018\")+\n coord_flip()+\n theme_bw()\n\n\n\n\nYou can see that the theme_bw() function changed a lot of the aspects of our plot! The background is white, the grid is a different color, etc. There are lots of other built in themes like this that come with the ggplot2 package.\n\n## Useful baseline themes are\ntheme_minimal()\ntheme_light()\ntheme_classic()\n\nThe built in theme functions (theme_*()) change the default settings for many elements that can also be changed individually using thetheme() function. The theme() function is a way to further fine-tune the look of your plot. This function takes MANY arguments (just have a look at ?theme). Luckily there are many great ggplot resources online so we don’t have to remember all of these, just Google “ggplot cheat sheet” and find one you like.\nLet’s look at an example of a theme() call, where we change the position of the legend from the right side to the bottom, and remove its title.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n coord_flip()+\n theme_bw()+\n theme(legend.position = \"bottom\",\n axis.ticks.x = element_blank())\n\n\n\n\nNote that the theme() call needs to come after any built-in themes like theme_bw() are used. Otherwise, theme_bw() will likely override any theme elements that you changed using theme().\nYou can also save the result of a series of theme() function calls to an object to use on multiple plots. This prevents needing to copy paste the same lines over and over again!\n\nmy_theme <- theme_bw(base_size = 16) +\n theme(legend.position = \"bottom\",\n axis.ticks.x = element_blank())\n\nSo now our code will look like this:\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n coord_flip()+\n my_theme\n\n\n\n\n\n\n\n\n\n\nExercise\n\n\n\nWhat changes do you expect to see in your plot by adding the following line of code? Discuss with your neighbor and then try it out!\nscale_y_continuous(breaks = seq(0,120, 20))\n\n\n\n\nAnswer\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\nFinally we are going to expand the bars all the way to the axis line. In other words, remove the gap between the bars and the vertical “x-axis” line.\n\nggplot(data = daily_visits_loc,\n aes(x = restore_loc, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n my_theme\n\n\n\n\n\n\n3.3.3.2 Reordering things\nggplot() loves putting things in alphabetical order. But more frequent than not, that’s not the order you actually want things to be plotted. One way to do this is to use the fct_reorder() function from the forcats package. forcats provide tools for working with categorical variables. In this case, we want to reorder or categorical variable of “Restoration Location” base on the total number of visits.\nThe fist thing we need to do is to add a column to our data with the total number of visits by location. This will be our “sorting” variable.\n\ndaily_visits_totals <- daily_visits_loc %>% \n group_by(restore_loc) %>%\n mutate(n = sum(daily_visits)) %>% \n ungroup()\n\nhead(daily_visits_totals)\n\n# A tibble: 6 × 5\n restore_loc date visitor_type daily_visits n\n <chr> <date> <chr> <dbl> <dbl>\n1 Decker Island 2017-07-07 bank_angler 4 37\n2 Decker Island 2017-07-07 cars 0 37\n3 Decker Island 2017-07-07 lrg_boat 0 37\n4 Decker Island 2017-07-07 med_boat 6 37\n5 Decker Island 2017-07-07 scientist 0 37\n6 Decker Island 2017-07-07 sm_boat 0 37\n\n\nNext, we will run the code for our plot adding the fct_reorder() function.\n\nggplot(data = daily_visits_totals,\n aes(x = fct_reorder(restore_loc, n), y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n my_theme\n\n\n\n\nWhat if you want to plot the other way around? In this case from least to greater? We add the desc() to the variable we are sorting by.\n\nggplot(data = daily_visits_totals,\n aes(x = fct_reorder(restore_loc, desc(n)), y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n coord_flip()+\n scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+\n labs(x = \"Restoration Location\",\n y = \"Number of Visits\",\n fill = \"Type of Visitor\",\n title = \"Total Number of Visits to Delta Restoration Areas by visitor type\",\n subtitle = \"Sum of all visits during study period\")+\n my_theme\n\n\n\n\n\n\n\n3.3.3.3 Colors\nColor can be fun and overwhelming Introduce color pallets things to keep in mind (how many colors you need) Provide resources\n\n\n3.3.3.4 Saving plots\nSaving plots using ggplot is easy! The ggsave() function will save either the last plot you created, or any plot that you have saved to a variable. You can specify what output format you want, size, resolution, etc. See ?ggsave() for documentation.\n\nggsave(\"figures/visit_restore_site_delta.jpg\", width = 8, height = 6, units = \"in\")\n\n\n\n3.3.3.5 Creating multiple plots\nAn easy way to plot another aspect of your data is using the function facet_wrap(). This function takes a mapping to a variable using the syntax ~{variable_name}. The ~ (tilde) is a model operator which tells facet_wrap() to model each unique value within variable_name to a facet in the plot.\nThe default behavior of facet wrap is to put all facets on the same x and y scale. You can use the scales argument to specify whether to allow different scales between facet plots (e.g scales = \"free_y\" to free the y axis scale). You can also specify the number of columns using the ncol = argument or number of rows using nrow =.\n\nggplot(data = daily_visits_totals,\n aes(x = visitor_type, y = daily_visits,\n fill = visitor_type))+\n geom_col()+\n theme_bw()+\n facet_wrap(~restore_loc,\n scales = \"free_y\",\n ncol = 5,\n nrow = 2)+\n labs(x = \"Type of visitor\",\n y = \"Number of Visits\",\n title = \"Total Number of Visits to Delta Restoration Areas\",\n subtitle = \"Sum of all visits during study period\")+\n theme_bw()+\n theme(legend.position = \"bottom\",\n axis.ticks.x = element_blank(),\n axis.text.x = element_blank())" }, { "objectID": "session_03.html#interactive-visualization", "href": "session_03.html#interactive-visualization", "title": "3  Data Visualization", "section": "3.4 Interactive visualization", - "text": "3.4 Interactive visualization\n\n3.4.1 Tables with DT\nNow that we know how to make great static visualizations, let’s introduce two other packages that allow us to display our data in interactive ways. These packages really shine when used with GitHub Pages, so at the end of this lesson we will publish our figures to the website we created earlier.\nFirst let’s show an interactive table of unique sampling locations using DT. Write a data.frame containing unique sampling locations with no missing values using two new functions from dplyr and tidyr: distinct() and drop_na().\n\nlocations <- visits_long %>%\n distinct(restore_loc, .keep_all = T) %>%\n select(restore_loc, latitude, longitude)\n\nAnd display it as an interactive table using datatable() from the DT package.\n\ndatatable(locations)\n\n\n\n\n\n\n\n\n3.4.2 Maps with leaflet\nSimilar to ggplot2, you can make a basic leaflet map using just a couple lines of code. Note that unlike ggplot2, the leaflet package uses pipe operators (%>%) and not the additive operator (+).\nThe addTiles() function without arguments will add base tiles to your map from OpenStreetMap. addMarkers() will add a marker at each location specified by the latitude and longitude arguments. Note that the ~ symbol is used here to model the coordinates to the map (similar to facet_wrap() in ggplot).\n\nleaflet(locations) %>%\n addTiles() %>%\n addMarkers(\n lng = ~ longitude,\n lat = ~ latitude,\n popup = ~ restore_loc\n )\n\n\n\n\n\n\nYou can also use leaflet to import Web Map Service (WMS) tiles. Here is an example that utilizes the General Bathymetric Map of the Oceans (GEBCO) WMS tiles. In this example, we also demonstrate how to create a more simple circle marker, the look of which is explicitly set using a series of style-related arguments.\n\nleaflet(locations) %>%\n addWMSTiles(\n \"https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=-90,-180,90,360&crs=EPSG:4326&format=image/jpeg&layers=gebco_latest&width=1200&height=600&version=1.3.0\",\n layers = 'GEBCO_LATEST',\n attribution = \"Imagery reproduced from the GEBCO_2022 Grid, WMS 1.3.0 GetMap, www.gebco.net\"\n ) %>%\n addCircleMarkers(\n lng = ~ Longitude,\n lat = ~ Latitude,\n popup = ~ Location,\n radius = 5,\n # set fill properties\n fillColor = \"salmon\",\n fillOpacity = 1,\n # set stroke properties\n stroke = T,\n weight = 0.5,\n color = \"white\",\n opacity = 1\n )\n\n\nLeaflet has a ton of functionality that can enable you to create some beautiful, functional maps with relative ease. Here is an example of some we created as part of the State of Alaskan Salmon and People (SASAP) project, created using the same tools we showed you here. This map hopefully gives you an idea of how powerful the combination of R Markdown and GitHub Pages can be." + "text": "3.4 Interactive visualization\n\n3.4.1 Tables with DT\nNow that we know how to make great static visualizations, let’s introduce two other packages that allow us to display our data in interactive ways. These packages really shine when used with GitHub Pages, so at the end of this lesson we will publish our figures to the website we created earlier.\nFirst let’s show an interactive table of unique sampling locations using DT. Write a data.frame containing unique sampling locations with no missing values using two new functions from dplyr and tidyr: distinct() and drop_na().\n\nlocations <- visits_long %>%\n distinct(restore_loc, .keep_all = T) %>%\n select(restore_loc, latitude, longitude)\n\nhead(locations)\n\n# A tibble: 6 × 3\n restore_loc latitude longitude\n <chr> <dbl> <dbl>\n1 Decker Island 38.1 -122.\n2 SW Suisun Marsh 38.2 -122.\n3 Grizzly Bay 38.1 -122.\n4 Prospect 38.2 -122.\n5 SJ River 38.1 -122.\n6 Wildlands 38.3 -122.\n\n\nThe dplyr::distinct() function comes pretty handy when you want to filter unique values in a column. In this case we use the .keep_all = T argument to keep all the columns of our data frame so we can have the lat and long of each of the locations. If we don’t add this argument, we would end up with a data frame with only one column: restore_loc and 10 rows, one for each of the unique locations.\nNow we can display this table as an interactive table using datatable() from the DT package.\n\ndatatable(locations)\n\n\n\n\n\n\n\n\n3.4.2 Maps with leaflet\nThe leaflet() package allows you to make basic interactive maps using just a couple lines of code. Note that unlike ggplot2, the leaflet package uses pipe operators (%>%) and not the additive operator (+).\nThe addTiles() function without arguments will add base tiles to your map from OpenStreetMap. addMarkers() will add a marker at each location specified by the latitude and longitude arguments. Note that the ~ symbol is used here to model the coordinates to the map (similar to facet_wrap() in ggplot).\n\nleaflet(locations) %>%\n addTiles() %>%\n addMarkers(\n lng = ~ longitude,\n lat = ~ latitude,\n popup = ~ restore_loc\n )\n\n\n\n\n\n\nYou can also use leaflet to import Web Map Service (WMS) tiles. Here is an example that utilizes the General Bathymetric Map of the Oceans (GEBCO) WMS tiles. In this example, we also demonstrate how to create a more simple circle marker, the look of which is explicitly set using a series of style-related arguments.\n\nleaflet(locations) %>%\n # addWMSTiles(\n # \"https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=-90,-180,90,360&crs=EPSG:4326&format=image/jpeg&layers=gebco_latest&width=1200&height=600&version=1.3.0\",\n # layers = 'GEBCO_LATEST',\n # attribution = \"Imagery reproduced from the GEBCO_2022 Grid, WMS 1.3.0 GetMap, www.gebco.net\"\n # ) %>%\n addWMSTiles(\"https://basemap.nationalmap.gov/arcgis/services/USGSTopo/MapServer/WmsServer\",\n layers = \"0\") %>% \n addCircleMarkers(\n lng = ~ Longitude,\n lat = ~ Latitude,\n popup = ~ Location,\n radius = 5,\n # set fill properties\n fillColor = \"salmon\",\n fillOpacity = 1,\n # set stroke properties\n stroke = T,\n weight = 0.5,\n color = \"white\",\n opacity = 1\n )\n\n\nLeaflet has a ton of functionality that can enable you to create some beautiful, functional maps with relative ease. Here is an example of some we created as part of the State of Alaskan Salmon and People (SASAP) project, created using the same tools we showed you here. This map hopefully gives you an idea of how powerful the combination of R Markdown and GitHub Pages can be." }, { "objectID": "session_03.html#publish-the-data-visualization-lesson-to-your-webpage", diff --git a/public/2023-08-delta/session_03.html b/public/2023-08-delta/session_03.html index 92f4029d..f519f8d8 100644 --- a/public/2023-08-delta/session_03.html +++ b/public/2023-08-delta/session_03.html @@ -369,7 +369,7 @@

delta_visits <- read_csv("https://portal.edirepository.org/nis/dataviewer?packageid=edi.587.1&entityid=cda8c1384af0089b506d51ad8507641f") %>% 
-    clean_names()
+ janitor::clean_names()
  1. Learn about the data. For this session we are going to be working with data on Socioecological Monitoring on the Sacramento-San Joaquin Delta. Check out the documentation.

  2. @@ -533,7 +533,7 @@

    daily_visits_loc %>%
         separate(date, c("year", "month", "day"), sep = "-") %>%
    @@ -620,9 +620,9 @@ 

    <

    3.3.3.1 Setting ggplot themes

    We have successfully plotted our data. But, this is clearly not a nice plot. Let’s work on making this plot look a bit nicer. We are going to”

      -
    • Include a built in theme using theme_bw()
    • -
    • Flip the x and y axis to better read the graph
    • Add a title, subtitle and adjust labels using labs()
    • +
    • Flip the x and y axis to better read the graph
    • +
    • Include a built in theme using theme_bw()

    There are a wide variety of built in themes in ggplot that help quickly set the look of the plot. Use the RStudio auto-complete theme_ <TAB> to view a list of theme functions.

    @@ -630,13 +630,13 @@

    aes(x = restore_loc, y = daily_visits, fill = visitor_type))+ geom_col()+ - theme_bw()+ - coord_flip()+ - labs(x = "Restoration Location", - y = "Number of Visits", - fill = "Type of Visitor", - title = "Total Number of Visits to Delta Restoration Areas by visitor type", - subtitle = "Sum of all visits during study period")

    + labs(x = "Restoration Location", + y = "Number of Visits", + fill = "Type of Visitor", + title = "Total Number of Visits to Delta Restoration Areas by visitor type", + subtitle = "Sum of all visits during July 2017 and March 2018")+ + coord_flip()+ + theme_bw()

    @@ -655,14 +655,15 @@

    aes(x = restore_loc, y = daily_visits, fill = visitor_type))+ geom_col()+ - theme_bw()+ - coord_flip()+ - labs(x = "Restoration Location", - y = "Number of Visits", - fill = "Type of Visitor", - title = "Total Number of Visits to Delta Restoration Areas by visitor type", - subtitle = "Sum of all visits during study period")+ - theme(legend.position = "bottom") + labs(x = "Restoration Location", + y = "Number of Visits", + fill = "Type of Visitor", + title = "Total Number of Visits to Delta Restoration Areas by visitor type", + subtitle = "Sum of all visits during study period")+ + coord_flip()+ + theme_bw()+ + theme(legend.position = "bottom", + axis.ticks.x = element_blank())

    @@ -670,7 +671,7 @@

    -
    my_theme <- theme_bw(base_size = 14) +
    +
    my_theme <- theme_bw(base_size = 16) +
         theme(legend.position = "bottom",
               axis.ticks.x = element_blank())
    @@ -680,12 +681,12 @@

    aes(x = restore_loc, y = daily_visits, fill = visitor_type))+ geom_col()+ - coord_flip()+ - labs(x = "Restoration Location", - y = "Number of Visits", - fill = "Type of Visitor", - title = "Total Number of Visits to Delta Restoration Areas by visitor type", - subtitle = "Sum of all visits during study period")+ + labs(x = "Restoration Location", + y = "Number of Visits", + fill = "Type of Visitor", + title = "Total Number of Visits to Delta Restoration Areas by visitor type", + subtitle = "Sum of all visits during study period")+ + coord_flip()+ my_theme

    @@ -731,15 +732,14 @@

    aes(x = restore_loc, y = daily_visits, fill = visitor_type))+ geom_col()+ - theme_bw()+ - coord_flip()+ - scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+ - labs(x = "Restoration Location", - y = "Number of Visits", - fill = "Type of Visitor", - title = "Total Number of Visits to Delta Restoration Areas by visitor type", - subtitle = "Sum of all visits during study period")+ - my_theme

    + labs(x = "Restoration Location", + y = "Number of Visits", + fill = "Type of Visitor", + title = "Total Number of Visits to Delta Restoration Areas by visitor type", + subtitle = "Sum of all visits during study period")+ + coord_flip()+ + scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+ + my_theme

    @@ -748,7 +748,7 @@

    3.3.3.2 Reordering things

    ggplot() loves putting things in alphabetical order. But more frequent than not, that’s not the order you actually want things to be plotted. One way to do this is to use the fct_reorder() function from the forcats package. forcats provide tools for working with categorical variables. In this case, we want to reorder or categorical variable of “Restoration Location” base on the total number of visits.

    -

    The fist thing we need to do is to add a column to our data with the total number of visits by location.

    +

    The fist thing we need to do is to add a column to our data with the total number of visits by location. This will be our “sorting” variable.

    daily_visits_totals <- daily_visits_loc %>% 
         group_by(restore_loc) %>%
    @@ -774,15 +774,14 @@ 

    aes(x = fct_reorder(restore_loc, n), y = daily_visits, fill = visitor_type))+ geom_col()+ - theme_bw()+ - coord_flip()+ - scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+ - labs(x = "Restoration Location", - y = "Number of Visits", - fill = "Type of Visitor", - title = "Total Number of Visits to Delta Restoration Areas by visitor type", - subtitle = "Sum of all visits during study period")+ - my_theme

    + labs(x = "Restoration Location", + y = "Number of Visits", + fill = "Type of Visitor", + title = "Total Number of Visits to Delta Restoration Areas by visitor type", + subtitle = "Sum of all visits during study period")+ + coord_flip()+ + scale_y_continuous(breaks = seq(0,120, 20), expand = c(0,0))+ + my_theme

    @@ -810,8 +809,8 @@

    -

    ?? Add somthing about reordering the legend??

    3.3.3.3 Colors

    @@ -819,51 +818,42 @@

    3.3.3.4 Saving plots

    -

    UDPDATE CODE

    Saving plots using ggplot is easy! The ggsave() function will save either the last plot you created, or any plot that you have saved to a variable. You can specify what output format you want, size, resolution, etc. See ?ggsave() for documentation.

    -
    ggsave("figures/nyears_data_kus.jpg", width = 8, height = 6, units = "in")
    -
    -

    We can also save our facet plot showing annual delta_visitsments by region calling the plot’s object.

    -
    -
    ggsave(annual_region_plot, "figures/annual_esc_region.png", width = 12, height = 8, units = "in")
    +
    ggsave("figures/visit_restore_site_delta.jpg", width = 8, height = 6, units = "in")

    3.3.3.5 Creating multiple plots

    -

    UPDATE TEXT

    -

    We know that in the graph we just plotted, each bar includes visit for multiple days. Let’s leverage the power of ggplot to plot more aspects of our data in one plot.

    -

    We are going to plot visits by species over time, from 2000 to 2016, for each region.

    An easy way to plot another aspect of your data is using the function facet_wrap(). This function takes a mapping to a variable using the syntax ~{variable_name}. The ~ (tilde) is a model operator which tells facet_wrap() to model each unique value within variable_name to a facet in the plot.

    The default behavior of facet wrap is to put all facets on the same x and y scale. You can use the scales argument to specify whether to allow different scales between facet plots (e.g scales = "free_y" to free the y axis scale). You can also specify the number of columns using the ncol = argument or number of rows using nrow =.

    -

    ADD TEXT ABOUT SCALES

    -
    ggplot(data = daily_visits_totals,
    -       aes(x = visitor_type, y = daily_visits,
    -           fill = visitor_type))+
    -    geom_col()+
    -    theme_bw()+
    -    facet_wrap(~restore_loc,
    -               scales = "free_y",
    -               ncol = 5,
    -               nrow = 2)+
    -    labs(x = "Type of visitor",
    -         y = "Number of Visits",
    -         title = "Total Number of Visits to Delta Restoration Areas",
    -         subtitle = "Sum of all visits during study period")+
    -    theme_bw()+
    -    theme(legend.position = "bottom",
    -          axis.ticks.x = element_blank(),
    -          axis.text.x = element_blank())
    +
    ggplot(data = daily_visits_totals,
    +       aes(x = visitor_type, y = daily_visits,
    +           fill = visitor_type))+
    +    geom_col()+
    +    theme_bw()+
    +    facet_wrap(~restore_loc,
    +               scales = "free_y",
    +               ncol = 5,
    +               nrow = 2)+
    +    labs(x = "Type of visitor",
    +         y = "Number of Visits",
    +         title = "Total Number of Visits to Delta Restoration Areas",
    +         subtitle = "Sum of all visits during study period")+
    +    theme_bw()+
    +    theme(legend.position = "bottom",
    +          axis.ticks.x = element_blank(),
    +          axis.text.x = element_blank())
    -

    +

    -
    -
    -

    3.3.3.6 Smarter tick labels using scales

    -

    Fixing tick labels in ggplot can be super annoying. The y-axis labels in the plot above don’t look great. We could manually fix them, but it would likely be tedious and error prone.

    -

    The scales package provides some nice helper functions to easily rescale and relabel your plots. Here, we use scale_y_continuous() from ggplot2, with the argument labels, which is assigned to the function name comma, from the scales package. This will format all of the labels on the y-axis of our plot with comma-formatted numbers.

    +
    @@ -874,23 +864,37 @@

    Now that we know how to make great static visualizations, let’s introduce two other packages that allow us to display our data in interactive ways. These packages really shine when used with GitHub Pages, so at the end of this lesson we will publish our figures to the website we created earlier.

    First let’s show an interactive table of unique sampling locations using DT. Write a data.frame containing unique sampling locations with no missing values using two new functions from dplyr and tidyr: distinct() and drop_na().

    -
    locations <- visits_long %>%
    -    distinct(restore_loc, .keep_all = T) %>%
    -    select(restore_loc, latitude, longitude)
    -
    -

    And display it as an interactive table using datatable() from the DT package.

    +
    locations <- visits_long %>%
    +    distinct(restore_loc, .keep_all = T) %>%
    +    select(restore_loc, latitude, longitude)
    +
    +head(locations)
    +
    +
    # A tibble: 6 × 3
    +  restore_loc     latitude longitude
    +  <chr>              <dbl>     <dbl>
    +1 Decker Island       38.1     -122.
    +2 SW Suisun Marsh     38.2     -122.
    +3 Grizzly Bay         38.1     -122.
    +4 Prospect            38.2     -122.
    +5 SJ River            38.1     -122.
    +6 Wildlands           38.3     -122.
    +
    + +

    The dplyr::distinct() function comes pretty handy when you want to filter unique values in a column. In this case we use the .keep_all = T argument to keep all the columns of our data frame so we can have the lat and long of each of the locations. If we don’t add this argument, we would end up with a data frame with only one column: restore_loc and 10 rows, one for each of the unique locations.

    +

    Now we can display this table as an interactive table using datatable() from the DT package.

    datatable(locations)
    -
    - +
    +

    3.4.2 Maps with leaflet

    -

    Similar to ggplot2, you can make a basic leaflet map using just a couple lines of code. Note that unlike ggplot2, the leaflet package uses pipe operators (%>%) and not the additive operator (+).

    +

    The leaflet() package allows you to make basic interactive maps using just a couple lines of code. Note that unlike ggplot2, the leaflet package uses pipe operators (%>%) and not the additive operator (+).

    The addTiles() function without arguments will add base tiles to your map from OpenStreetMap. addMarkers() will add a marker at each location specified by the latitude and longitude arguments. Note that the ~ symbol is used here to model the coordinates to the map (similar to facet_wrap() in ggplot).

    leaflet(locations) %>%
    @@ -901,33 +905,35 @@ 

    popup = ~ restore_loc )

    -
    - +
    +


    You can also use leaflet to import Web Map Service (WMS) tiles. Here is an example that utilizes the General Bathymetric Map of the Oceans (GEBCO) WMS tiles. In this example, we also demonstrate how to create a more simple circle marker, the look of which is explicitly set using a series of style-related arguments.

    leaflet(locations) %>%
    -    addWMSTiles(
    -        "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=-90,-180,90,360&crs=EPSG:4326&format=image/jpeg&layers=gebco_latest&width=1200&height=600&version=1.3.0",
    -        layers = 'GEBCO_LATEST',
    -        attribution = "Imagery reproduced from the GEBCO_2022 Grid, WMS 1.3.0 GetMap, www.gebco.net"
    -    ) %>%
    -    addCircleMarkers(
    -        lng = ~ Longitude,
    -        lat = ~ Latitude,
    -        popup = ~ Location,
    -        radius = 5,
    -        # set fill properties
    -        fillColor = "salmon",
    -        fillOpacity = 1,
    -        # set stroke properties
    -        stroke = T,
    -        weight = 0.5,
    -        color = "white",
    -        opacity = 1
    -    )
    + # addWMSTiles( + # "https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?request=getmap&service=wms&BBOX=-90,-180,90,360&crs=EPSG:4326&format=image/jpeg&layers=gebco_latest&width=1200&height=600&version=1.3.0", + # layers = 'GEBCO_LATEST', + # attribution = "Imagery reproduced from the GEBCO_2022 Grid, WMS 1.3.0 GetMap, www.gebco.net" + # ) %>% + addWMSTiles("https://basemap.nationalmap.gov/arcgis/services/USGSTopo/MapServer/WmsServer", + layers = "0") %>% + addCircleMarkers( + lng = ~ Longitude, + lat = ~ Latitude, + popup = ~ Location, + radius = 5, + # set fill properties + fillColor = "salmon", + fillOpacity = 1, + # set stroke properties + stroke = T, + weight = 0.5, + color = "white", + opacity = 1 + )


    Leaflet has a ton of functionality that can enable you to create some beautiful, functional maps with relative ease. Here is an example of some we created as part of the State of Alaskan Salmon and People (SASAP) project, created using the same tools we showed you here. This map hopefully gives you an idea of how powerful the combination of R Markdown and GitHub Pages can be.

    diff --git a/public/2023-08-delta/session_03_files/figure-html/theme_bw_plot-1.png b/public/2023-08-delta/session_03_files/figure-html/theme_bw_plot-1.png index b2dae453..1d17cb84 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/theme_bw_plot-1.png and b/public/2023-08-delta/session_03_files/figure-html/theme_bw_plot-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-11-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-11-1.png index a6f4d880..5f87f05a 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-11-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-12-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-12-1.png index f1624363..a8825855 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-12-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-12-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-14-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-14-1.png new file mode 100644 index 00000000..510778af Binary files /dev/null and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-14-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-6-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-6-1.png index d0b66a1f..8edb046c 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-6-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-6-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-7-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-7-1.png index 120639d9..87e95987 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-7-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-7-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-8-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-8-1.png index 86c28a11..72c1ac85 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-8-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-8-1.png differ diff --git a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-9-1.png b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-9-1.png index a8b20011..e39fe79f 100644 Binary files a/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-9-1.png and b/public/2023-08-delta/session_03_files/figure-html/unnamed-chunk-9-1.png differ diff --git a/public/2023-08-delta/session_10.html b/public/2023-08-delta/session_10.html index 57e2f92f..c2a27b74 100644 --- a/public/2023-08-delta/session_10.html +++ b/public/2023-08-delta/session_10.html @@ -1002,8 +1002,8 @@

    m
    -
    - +
    +

    We can add labels, legends, and a color scale.

    @@ -1028,8 +1028,8 @@

    m
    -
    - +
    +

    We can also add the individual communities, with popup labels showing their population, on top of that!

    @@ -1065,8 +1065,8 @@

    m
    -
    - +
    +