library(knitr) # opts_knit$set(out.format = "latex") knit_theme$set(knit_theme$get("greyscale0")) # options(replace.assign=FALSE,width=50) # opts_chunk$set(fig.path='figure/graphics-', # cache.path='cache/graphics-', # dev='pdf', fig.width=5, fig.height=5, # cache=FALSE) # knit_hooks$set(crop=hook_pdfcrop) suppressPackageStartupMessages(library(dplyr)) # figure referencing hack fig <- local({ i <- 0 ref <- list() list( cap=function(refName, text) { i <<- i + 1 ref[[refName]] <<- paste0("Figure ",i) paste("Figure ", i, ": ", text, sep="") }, ref=function(refName) { ref[[refName]] }) })
This section will hopefully help you get more comfortable with some of the dplyr
functionality for "wrangling" your data. We will do some data wrangling and the use that to create some plots. Make sure you load in the dplyr
package and the movies
data set.
suppressPackageStartupMessages(library(dplyr)) library(dplyr) data("movies",package = "ggplot2movies")
(1) We want to look at how film budgets have changed over time for both the Comedy and non Comedy films. To start with, since there are missing values NA
for films where we didn't know the budget, there could be complications for calculating averages and plots. We can use the is.na()
function to find such values. Use the filter()
function to remove them, try using the %>%
operator notation if you like, movies %>% filter(!is.na(...))
```r movies %>% filter(!is.na(budget)) ```
(1) We want to look at comedy and non comedy films in each year, this is some sort of grouping structure which suggests use of the group_by()
function. Create this grouping structure on the filtered movies data
```r movies %>% filter(!is.na(budget)) %>% group_by(year,Comedy) ```
(1) Use summarise()
to calculate the average budget in each year for both comedy and non comedy films. When you do name the new column of averages b
and store the final output data as a variable called plotdat
```r plotdat = movies %>% filter(!is.na(budget)) %>% group_by(year,Comedy) %>% summarise(b = mean(budget)) ```
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.