class: center, middle, inverse, title-slide .title[ # Data Visualization with R Package ggplot2 ] .author[ ###
Ying-Ju Tessa Chen, PhD
Associate Professor
Department of Mathematics
University of Dayton
@ying-ju
ying-ju
ychen4@udayton.edu
] --- ## Learning Objectives .small[ In this session, we will introduce how to visualize data using .red[ggplot2]. This lecture is based on [UC Business Analytics R Programming Guide]( https://uc-r.github.io/ggplot_intro). While we can use the built-in functions in the base package in R to create figures, the package .red[ggplot2] creates advanced graphs with simple and flexible commands. ] --- ## Load packages and read the Fuel Economy Data .small[First, we load the necessary packages, check conflict functions, and get a glimpse of the dataset **mpg** from the R package .red[ggplot2]. ``` r library(tidyverse) library(conflicted) conflict_prefer("lag", "dplyr") conflict_prefer("filter", "dplyr") glimpse(mpg) ``` ``` ## Rows: 234 ## Columns: 11 ## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "… ## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "… ## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.… ## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200… ## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, … ## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto… ## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4… ## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1… ## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2… ## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p… ## $ class <chr> "compact", "compact", "compact", "compact", "compact", "c… ``` ] --- ## Understand Our Data .small[ Now we need to understand the data and each variable in the data. This dataset contains 38 popular models of cars from 1999 to 2008. ([Fuel Economy Data](https://ggplot2.tidyverse.org/reference/mpg.html)). - manufacturer: car manufacturer - model: model name - displ: engine displacement, in liters - year: year of manufacturing (1999-2008) - cyl: number of cylinders - trans: type of transmission - drv: drive type (f, r, 4, f=front wheel, r=rear wheel, 4=4 wheel) - cty: city mileage miles per gallon - hwy: highway mileage miles per gallon - fl: fuel type (diesel, petrol, electric, etc.) - class: vehicle class 7 types (compact, SUV, minivan etc.) ] --- ## Grammar of Graphics .small[ The basic idea of creating plots using the R package .red[ggplot2] is to specify each component of the following and combine them with .blue[+]. The .blue[ggplot()] function plays an important role in data visualization as it is very flexible for plotting many different types of graphic displays. The logic when using .blue[ggplot()] function is: .orange[ggplot(data, mapping) + geom_function()]. ] --- ## The Basics We create a canvas first and then we add variables of interest on the plot. Then, we can obtain a scatter plot to study the relationship between engine displacement and highway mileage per gallon. .pull-left-three[ ``` r # create canvas mpg |> ggplot() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/ggplot_basics1-1.png" width="90%" style="display: block; margin: auto;" /> ] .center-column-three[ ``` r # variables of interest mapped mpg |> ggplot(aes(x = displ, y = hwy)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/ggplot_basics2-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right-three[ ``` r # data plotted mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/ggplot_basics3-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ## Aesthetic Mappings .pull-left[ .small[ The aesthetic mappings allow to select variables to be plotted and use data properties to influence visual characteristics such as color, size, shape, position, etc. As a result, each visual characteristic can encode a different part of the data and be utilized to communicate information. All aesthetics for a plot are specified in the .blue[aes()] function call. For example, we can add a mapping from the class of the cars to a color characteristic: _ ``` r mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_color1-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ### Aesthetic Mappings (Continued) .pull-left[.footnotesize[ **Note:** 1. We should note that in the above code chunk, "class" is a variable in the data and therefore, the commend specifies a categorical variable is used as the third variable in the figure. 2. Using the .blue[aes()] function will cause the visual channel to be based on the data specified in the argument. For example, using .blue[aes(color = "blue")] won’t cause the geometry’s color to be “blue”, but will instead cause the visual channel to be mapped from the vector c("blue") — as if we only had a single type of engine that happened to be called “red”. If we wish to apply an aesthetic property to an entire geometry, you can set that property as an argument to the geom method, outside of the .blue[aes()] call: ] ] .pull-right[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(color = "blue") + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_color2-1.png" width="65%" style="display: block; margin: auto;" /> ] --- ## Specifying Geometric Shapes .small[Building on these basics, we can use .red[ggplot2] to create almost any kind of plot we may want. These plots are declared using functions that follow from the Grammar of Graphics. .red[ggplot2] supports a number of different types of geometric objects, including: .pull-left[ - .blue[geom_bar()]: bar charts - .blue[geom_boxplot()]: boxplots - .blue[geom_histogram()]: histograms - .blue[geom_line()]: lines ] .pull-right[ - .blue[geom_map()]: polygons in the shape of a map. - .blue[geom_point()]: individual points - .blue[geom_polygon()]: arbitrary shapes - .blue[geom_smooth()]: smoothed lines ] Each of these geometries will make use of the aesthetic mappings provided, albeit the visual qualities to which the data will be mapped will differ. For example, we can map data to the shape of a geom_point (e.g., if they should be circles or squares), or we can map data to the line-type of a geom_line (e.g., if it is solid or dotted), but not vice versa. Almost all geoms require an x and y mapping at the bare minimum.] --- ### Specifying Geometric Shape (Continued) .small[- x and y mapping needed when creating a scatterplot] .pull-left[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom11-1.png" width="70%" style="display: block; margin: auto;" /> ] .pull-right[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_smooth() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom12-1.png" width="70%" style="display: block; margin: auto;" /> ] --- ### Specifying Geometric Shape (Continued) .small[ - no y mapping needed when creating a bar chart] .pull-left[ ``` r mpg |> ggplot(aes(x = class)) + geom_bar() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom13-1.png" width="75%" style="display: block; margin: auto;" /> ] .pull-right[ ``` r mpg |> ggplot(aes(x = hwy)) + geom_histogram() ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom14-1.png" width="75%" style="display: block; margin: auto;" /> ] --- ### Specifying Geometric Shapes (Continued) .small[ What makes this really powerful is that you can add multiple geometries to a plot, thus allowing you to create complex graphics showing multiple aspects of your data. .pull-left[ ``` r # plot with both points and smoothed line mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + geom_smooth() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom2-1.png" width="75%" style="display: block; margin: auto;" /> ] --- ### Specifying Geometric Shapes (Continued) .small[Since the aesthetics for each geom can be different, we could show multiple lines on the same plot (or with different colors, styles, etc). For example, we can plot both points and a smoothed line for the same x and y variable but specify unique colors within each geom: .pull-left[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(color = "blue") + geom_smooth(color = "red") + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom3-1.png" width="75%" style="display: block; margin: auto;" /> ] --- ### Specifying Geometric Shapes (Continued) .small[ .pull-left[ It is also possible to give each geom a different data argument, so that we can show multiple data sets in the same plot. If we specify an aesthetic within .blue[ggplot()], it will be passed on to each geom that follows. ``` r # color aesthetic passed to each geom layer mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + geom_smooth(se = FALSE) + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom41_show-1.png" width="60%" style="display: block; margin: auto;" /> ] .pull-right[ Or we can specify certain aes within each geom, which allows us to only show certain characteristics for that specific layer (i.e. geom_point). ``` r # color aesthetic specified for only the geom_point layer mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + geom_smooth(se = FALSE) + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_geom42_show-1.png" width="60%" style="display: block; margin: auto;" /> ] ] --- ## Statistical Transformations .small[The following bar chart shows the frequency distribution of vehicle class. We can find that y axis was defined as the count of elements that have the particular type. This count is not part of the data set, but is instead a statistical transformation that the geom_bar automatically applies to the data. In particular, it applies the stat_count transformation. .pull-left[ ``` r mpg |> ggplot(aes(x = class)) + geom_bar() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_bar-1.png" width="75%" style="display: block; margin: auto;" /> ] --- ### Statistical Transformations (Continued) .small[ .pull-left[ .red[ggplot2] supports many different statistical transformations. For example, the “identity” transformation will leave the data “as is”. We can specify which statistical transformation a geom uses by passing it as the stat argument. For example, consider our data already had the count as a variable: ``` r (class_count <- mpg |> count(class)) ``` ``` ## # A tibble: 7 × 2 ## class n ## <chr> <int> ## 1 2seater 5 ## 2 compact 47 ## 3 midsize 41 ## 4 minivan 11 ## 5 pickup 33 ## 6 subcompact 35 ## 7 suv 62 ``` ] .pull-right[ We can use stat = "identity" within geom_bar to plot our bar height values to this variable. Also, note that we now include n for our y variable: ``` r class_count |> ggplot(aes(x = class, y = n)) + geom_bar(stat = "identity") + theme(axis.text.x = element_text(angle=30, hjust=1), text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_count1_show-1.png" width="68%" style="display: block; margin: auto;" /> ] ] --- ### Statistical Transformations (Continued) .small[We can also call .blue[stat_] functions directly to add additional layers. For example, here we create a scatter plot of highway miles for each displacement value and then use .blue[stat_summary()] to plot the mean highway miles at each displacement value. .pull-left[ ``` r mpg |> ggplot(aes(displ, hwy)) + geom_point(color = "grey") + stat_summary(fun.y = "mean", geom = "line", size = 1, linetype = "dashed") + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_summary-1.png" width="75%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ## Position Adjustments .small[In addition to a default statistical transformation, each geom also has a default position adjustment which specifies a set of “rules” as to how different components should be positioned relative to each other. This position is noticeable in .blue[geom_bar()] if we map a different variable to the color visual characteristic. .pull-left[ ``` r # bar chart of class, colored by drive (front, rear, 4-wheel) mpg |> ggplot(aes(x = class, fill = drv)) + geom_bar() + theme(axis.text.x = element_text(angle=30, hjust=1), text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_position1-1.png" width="70%" style="display: block; margin: auto;" /> ] --- ### Position Adjustments (Continued) .small[The .blue[geom_bar()] by default uses a position adjustment of "stack", which makes each rectangle's height proportional to its value and stacks them on top of each other. We can use the position argument to specify what position adjustment rules to follow:] .pull-left[ ``` r # position = "dodge": values next to each other mpg |> ggplot(aes(x = class, fill = drv)) + geom_bar(position = "dodge") + theme(axis.text.x = element_text(angle=30, hjust=1), text = element_text(size = 20)) ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_position21-1.png" width="75%" style="display: block; margin: auto;" /> ] --- ### Position Adjustments (Continued) .pull-left[ ``` r # position = "fill": percentage chart mpg |> ggplot(aes(x = class, fill = drv)) + geom_bar(position = "fill") + theme(axis.text.x = element_text(angle=30, hjust=1), text = element_text(size = 20)) ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_position22-1.png" width="75%" style="display: block; margin: auto;" /> ] **Note:** We may need to check the documentation for each particular geom to learn more about its positioning adjustments. --- ## Managing Scales .small[Whenever we specify an aesthetic mapping, .blue[ggplot()] uses a particular **scale** to determine the range of values that the data should map to. It automatically adds a scale for each mapping to the plot. .pull-left[ ``` r # color the data by engine type mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_scale1-1.png" width="75%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ### Managing Scales (Continued) .small[However, the scale used in the figure could be changed if needed. Each scale can be represented by a function with the following name: **scale_**, followed by the name of the aesthetic property, followed by an _ and the name of the scale. A continuous scale will handle things like numeric data, whereas a discrete scale will handle things like colors. .pull-left[ ``` r # same as above, with explicit scales mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + scale_x_continuous() + scale_y_continuous() + scale_colour_discrete() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_scale2-1.png" width="75%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ### Managing Scales (Continued) .small[While the default scales will work fine, it is possible to explicitly add different scales to replace the defaults. For example, we can use a scale to change the direction of an axis: .pull-left[ ``` r # milage relationship, ordered in reverse mpg |> ggplot(aes(x = cty, y = hwy)) + geom_point() + scale_x_reverse() + scale_y_reverse() + theme(text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_reverse-1.png" width="80%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ### Managing Scales (Continued) .small[ Similarly, we can use .blue[scale_x_log10()] and .blue[scale_x_sqrt()] to transform the scale. We can use scales to format the axes as well. .pull-left[ ``` r mpg |> ggplot(aes(x = class, fill = drv)) + geom_bar(width=0.75, position = "fill") + scale_y_continuous(breaks = seq(0, 1, by = .2), labels = scales::percent) + labs(y = "Percent") + theme(axis.text.x = element_text(angle=30, hjust=1), text = element_text(size = 20)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/visual_scale3_show-1.png" width="80%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ## Use Pre-Defined Palettees .small[ A common parameter to change is which set of colors to use in a plot. While you can use the default coloring, a more common option is to leverage the pre-defined palettes from [colorbrewer.org](https://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3). These color sets have been carefully designed to look good and to be viewable to people with certain forms of color blindness. We can leverage color brewer palettes by specifying the .blue[scale_color_brewer()], passing the palette as an argument. .pull-left[ - default color brewer ``` r mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + scale_color_brewer() + theme(text = element_text(size = 18)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/color_brewer1-1.png" width="66%" style="display: block; margin: auto;" /> ] <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ### Use Pre-Defined Palettees (Continued) .pull-left[.small[ - specifying color palette ``` r mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + scale_color_brewer(palette = "Set3") + theme(text = element_text(size = 16)) ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/color_brewer2-1.png" width="72%" style="display: block; margin: auto;" /> ] <br> <br> <br> <img src="data:image/png;base64,#../figs/self_study.png" width="6%" style="display: block; margin: auto 0 auto auto;" /> --- ## Coordinate Systems .small[ Similar to scales, coordinate systems are specified with functions that all start with **coord_** and are added as a layer. There are a number of different possible coordinate systems to use, including: - .blue[coord_cartesian()]: the default Cartesian coordinate system, where you specify x and y values - .blue[coord_flip()]: a cartesian system with the x and y flipped - .blue[coord_fixed()]: a cartesian system with a “fixed” aspect ratio - .blue[coord_polar()]: a plot using polar coordinates - .blue[coord_quickmap()]: a coordinate system that approximates a good aspect ratio for maps. ] .footnotesize[Some useful functions: - Add text labels: .blue[geom_text()] - Change fill color using colour schemes from [ColorBrewer](http://colorbrewer2.org): .blue[scale_color_brewer()] - Apply .blue[theme_void()] to remove axes, background, etc.] --- ### Coordinate Systems (Continued) .small[ .pull-left[ - zoom in with coord_cartesian ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + coord_cartesian(xlim = c(0, 5)) + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/coord1-1.png" width="70%" style="display: block; margin: auto;" /> ] .pull-right[ - flip x and y axis with coord_flip ``` r mpg |> ggplot(aes(x = class)) + geom_bar() + coord_flip() + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/coord2-1.png" width="70%" style="display: block; margin: auto;" /> ] ] --- ### Coordinate Systems (Continued) .small[We can use .blue[geom_bar() + coord_polar()] to create a pie chart.] .pull-left[ .small[ ``` r class_count$percent <- round(class_count$n/sum(class_count$n)*100,2) # Create a basic bar pie <- class_count |> ggplot(aes(x = "", y = percent, fill = class)) + geom_bar(stat = "identity", width = 1, color = "white") # Convert to pie (polar coordinates) and add labels pie <- pie + coord_polar("y", start = 0) + geom_text(aes(label = paste0(percent, "%")), fontface = "bold", color = "black", position = position_stack(vjust = 0.5)) # Add color scale (hex colors) pie <- pie + scale_fill_brewer(palette = "Oranges") # Remove labels and change font size pie <- pie + theme_void() + theme(text = element_text(size = 20)) pie ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/pie_show-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ### Coordinate Systems (Continued) .small[ We can create a donut chart by having a hole inside of a pie chart. The only difference between the pie chart code is that we set: x = 2 and xlim = c(0.5, 2.5) to create the hole inside the pie chart. Additionally, the argument width in the function .blue[geom_bar()] is no longer needed. .pull-left[ ``` r # Create a basic bar donut <- class_count |> ggplot(aes(x = 2, y = percent, fill = class)) + geom_bar(stat = "identity", color = "white") # Convert to pie (polar coordinates) and add labels donut <- donut + coord_polar("y", start = 0) + geom_text(aes(label = paste0(percent, "%")), fontface = "bold", color = "black", position = position_stack(vjust = 0.5)) # Add color scale (hex colors) donut <- donut + scale_fill_brewer(palette = "Oranges") # Remove labels and change font size donut <- donut + theme_void() + theme(text = element_text(size = 20)) + xlim(0.5, 2.5) donut ``` ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/donut_show-1.png" width="72%" style="display: block; margin: auto auto auto 0;" /> <img src="data:image/png;base64,#../figs/self_study.png" width="12%" style="display: block; margin: auto 0 auto auto;" /> ] --- ## Interative Chord Diagram .small[ .pull-left[ ``` r if(require(chorddiag)==FALSE) devtools::install_github("mattflor/chorddiag") library(chorddiag) mat <- table(mpg$drv, mpg$class) dimnames(mat) <- list(Drive = rownames(mat), Type = colnames(mat)) colorcount <- ncol(mat) getPalette <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(9, "Set1")) mat |> chorddiag(type = "bipartite", width = 500, height = 430, showTicks = FALSE, groupnameFontsize = 10, groupnamePadding = 8, groupThickness = 0.05, categorynamePadding = 50, chordedgeColor = "gray90", categoryNames = c("Type of Drive Train", "Type of Car"), tooltipUnit = "car(s)", groupColors = getPalette(colorcount)) ``` ] .pull-right[
<img src="data:image/png;base64,#../figs/self_study.png" width="12%" style="display: block; margin: auto 0 auto auto;" /> ] ] --- ## Facets .pull-left[.small[ If we want to divide the information into multiple subplots, facets are ways to go. It allows us to view a separate plot for each case in a categorical variable. We can construct a plot with multiple facets by using the .blue[facet_wrap()]. This will produce a “row” of subplots, one for each categorical variable (the number of rows can be specified with an additional argument).] .footnotesize[ **Note:** 1. We can .blue[facet_grid()] to facet the data by more than one categorical variable. 2. We use a tilde (~) in our facet functions. With .blue[facet_grid()] the variable to the left of the tilde will be represented in the rows and the variable to the right will be represented across the columns. ] ] .pull-right[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + facet_grid(~ class) + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/facets1-1.png" width="80%" style="display: block; margin: auto;" /> ] --- ### Facets (Continued) .small[ ``` r mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point() + facet_grid(year ~ cyl) + theme(text = element_text(size = 20)) ``` <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/facets2-1.png" width="40%" style="display: block; margin: auto;" /> ] --- ## Labels & Annotations .small[ Textual annotations and labels (on the plot, axes, geometry, and legend) are crucial for understanding and presenting information. - .blue[labs()]: assign title, subtitle, caption, x & y labels ] .pull-left-2[.small[ We can add titles and axis labels to a chart using the labs() function (not labels, which is a different R function!). ``` r mpg |> ggplot(aes(x = displ, y = hwy, color = class)) + geom_point() + labs(title = "Fuel Efficiency by Engine Power", subtitle = "Fuel economy data from 1999 and 2008 for cars", x = "Engine Displacement (liters)", y = "Fuel Efficiency (miles per gallon)", color = "Car Type") + theme(text = element_text(size = 20)) ``` ] ] .pull-right-2[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/labels2-1.png" width="85%" style="display: block; margin: auto;" /> ] --- ### Labels & Annotations (Continued) .small[It is also possible to add labels into the plot itself (e.g., to label each point or line) by adding a new geom_text or geom_label to the plot; effectively, we are plotting an extra set of data which happen to be the variable names. .pull-left[ ``` r # table of each car that has best efficiency of its type best_in_class <- mpg |> group_by(class) |> filter(row_number(desc(hwy)) == 1) mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + geom_label(data = best_in_class, aes(label = model), alpha = 0.5) + theme(text = element_text(size = 20)) ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/label_points2-1.png" width="80%" style="display: block; margin: auto;" /> <img src="data:image/png;base64,#../figs/self_study.png" width="12%" style="display: block; margin: auto 0 auto auto;" /> ] ] --- ### Labels & Annotations (Continued) .small[However, we can find that two labels overlap one-another in the top left part of the plot on the previous slide. We can use the .blue[geom_text_repel()] from the .red[ggrepel] package to help position labels.] .pull-left[ ``` r library(ggrepel) mpg |> ggplot(aes(x = displ, y = hwy)) + geom_point(aes(color = class)) + geom_text_repel(data = best_in_class, aes(label = model)) + theme(text = element_text(size = 20)) ``` ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/ggrepel2-1.png" width="80%" style="display: block; margin: auto 0 auto auto;" /> <img src="data:image/png;base64,#../figs/self_study.png" width="12%" style="display: block; margin: auto 0 auto auto;" /> ] --- ## Themes .small[Whenever we want to customize titles, labels, fonts, background, grid lines, and legends, we can use themes. ] .pull-left[ ``` r mpg |> ggplot(aes(x=displ, y=hwy)) + geom_point() + labs(title = "Fuel Efficiency by Engine Power", x = "Engine Displacement (Liters)", y = "Fuel Efficiency (Miles per gallon)") + theme(axis.text.x = element_text(size = 18), axis.text.y = element_text(size = 18), axis.title.x = element_text(size = 18), axis.title.y = element_text(size = 18)) ``` .footnotesize[ **Note:** 1. We only list some key components here. 2. See [Modify Components of A Theme](https://ggplot2.tidyverse.org/reference/theme.html) and [Complete Themes](https://ggplot2.tidyverse.org/reference/ggtheme.html) for more details about the use of theme. ] ] .pull-right[ <img src="data:image/png;base64,#ggplot2_functions_files/figure-html/theme2-1.png" width="80%" style="display: block; margin: auto;" /> ] --- # Summary of Main Points By now, you should know how to use .blue[ggplot()] function to create basic graphical displays. In addition, we learned .small[ - Aesthetic Mappings - Specifying Geometric Shapes - Statistical Transformations - Position Adjustments - Managing Scales - Using Pre-Defined Palettes - Coordinate Systems - Facets - Labels & Annotations - Themes ] --- ## In-Class Activity You can download the worksheet [here](https://bitbucket.org/tessachen/tessachen.bitbucket.io/src/main/worksheets/ggplot2_activity.docx). <br> ## Thank you! .pull-left-2[ Please do not hesitate to contact me (Tessa Chen) at <a href="mailto:ychen4@udayton.edu"><i class="fa fa-paper-planee fa-fw"></i? : ychen4@udayton.edu</a> for questions or further discussions. ] .pull-right-2[ <img src="data:image/png;base64,#../figs/Tessa_grey_G.gif" width="50%" style="display: block; margin: auto;" /> ]