class: center, middle, inverse, title-slide # Advanced
ggplot2
### Haley Jeppson and Sam Tyner --- class: inverse # Visual Appearance --- class: primary # Built-In Themes <!-- --> --- class: primary # Other Themes `ggthemes`: a `ggplot2` extension <!-- --> --- class: primary # 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() ``` <!-- --> --- class: primary # 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. .small[ 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.] --- class: primary # 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") ) ``` <!-- --> --- class: primary # 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") ) ``` <!-- --> --- class: primary # 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")) ``` <!-- --> --- class: primary # 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!!") ``` <!-- --> --- class: primary # 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) ``` <!-- --> --- class: inverse # Interactive graphics --- class: primary # Plotly ```r p <- ggplot(mtcars, aes(x = wt, y = mpg, colour = gear)) + geom_point() + scale_color_locuszoom() library(plotly) ggplotly(p) ```
--- class: inverse # Saving graphics --- class: primary # 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)) ``` <!-- -->