class: center, middle, inverse, title-slide # Data Visualization ## Using ggplot2 ### Haley Jeppson and Sam Tyner --- class: center, middle # POLISHING PLOTS --- class: inverse, center, middle # Visual Appearance --- ## Built-In Themes ![](5-PolishingPlots_files/figure-html/unnamed-chunk-2-1.png)<!-- --> --- ## Other Themes: ggthemes a ggplot2 extension ![](5-PolishingPlots_files/figure-html/unnamed-chunk-3-1.png)<!-- --> --- ## Setting Themes You can globally set a theme with the `theme_set()` function: ```r theme_set(theme_bw()) ggplot(mtcars, aes(x = wt, y = mpg, colour = gear)) + geom_point() ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-4-1.png)<!-- --> --- ## Elements in a theme The function `theme()` is used to control non-data parts of the graph including: - **Line elements**: axis lines, minor and major grid lines, plot panel border, axis ticks background color, etc. - **Text elements**: plot title, axis titles, legend title and text, axis tick mark labels, etc. - **Rectangle elements**: plot background, panel background, legend background, etc. There is a specific function to modify each of these three elements : - `element_line()` to modify the line elements of the theme - `element_text()` to modify the text elements - `element_rect()` to change the appearance of the rectangle elements - `element_blank()` to draw nothing and assign no space **Note**: `rel()` is used to specify sizes relative to the parent, `margins()` is used to specify the margins of elements. --- ## Modifying a plot ```r p1 <- ggplot(mpg) + geom_bar(aes(x = class, colour = manufacturer, fill = manufacturer) ) p2 <- p1 + theme_classic() + theme( ## modify plot background plot.background = element_rect(fill = "lightskyblue1",colour = "pink",size = 0.5, linetype = "longdash") ) ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-6-1.png)<!-- --> --- ## Plot Legends ```r p3 <- p2 + theme( ### move and modify legend legend.title = element_blank(), legend.position = "top", legend.key = element_rect(fill = "lightskyblue1", color = "lightskyblue1"), legend.background = element_rect( fill = "lightskyblue1",color = "pink", size = 0.5,linetype = "longdash") ) ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-8-1.png)<!-- --> --- ## Modifying Axes ```r p4 <- p3 + theme( ### remove axis ticks axis.ticks=element_blank(), ### modify axis lines axis.line.y = element_line(colour = "pink", size = 1, linetype = "dashed"), axis.line.x = element_line(colour = "pink", size = 1.2, linetype = "dashed")) ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-10-1.png)<!-- --> --- ## Plot Labels Can be modified in several ways: - `labs()`, `xlab()`, `ylab()`, `ggtitle()` - You can also set axis and legend labels in the individual scales (using the first argument, the name) . ```r p5 <- p4 + labs(x = "Class of car", y = "", title = "Cars by class and manufacturer", subtitle = "With a custom theme!!") ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-12-1.png)<!-- --> --- ## Zooming ```r p <- ggplot(mtcars, aes(x = wt, y = mpg, colour = gear)) + geom_point() p_zoom_in <- p + xlim(2, 4) + ylim(10, 25) p_zoom_out <- p + xlim(0,7) + ylim(0, 45) ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-14-1.png)<!-- --> --- class: inverse, center, middle # Interactive graphics --- ## Plotly ```r p <- ggplot(mtcars, aes(x = wt, y = mpg, colour = gear)) + geom_point() + scale_color_locuszoom() library(plotly) ggplotly(p) ```
--- class: inverse, center, middle # Saving graphics --- ## Saving your Work We can save the results of a plot to a file (as an image) using the `ggsave()` function: ```r p1 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = gear)) + geom_point() ggsave("mpg_by_wt.pdf", plot = p1) ``` --- ## Your Turn 1. Create a scatterplot of y versus x from the `diamonds` data, colored by clarity 2. Use the black and white theme 3. Include an informative title 4. Move the legend to the bottom 5. Save your plot to a pdf file and open it in a pdf viewer. ```r ggplot(data = diamonds, aes(x = x, y = y)) ``` ![](5-PolishingPlots_files/figure-html/unnamed-chunk-17-1.png)<!-- -->