|
| 1 | +# Automate Plotting |
| 2 | + |
| 3 | +## Challenge 1 |
| 4 | + |
| 5 | +How can you create the character vector of column names? |
| 6 | + |
| 7 | +**Answer** In the context of this challenge |
| 8 | + |
| 9 | +```{r} |
| 10 | + |
| 11 | +names(airquality) |
| 12 | + |
| 13 | +``` |
| 14 | + |
| 15 | +## Challenge 2 |
| 16 | + |
| 17 | +How can make `ggplot2()` take strings as x and y variable names? (Hint: Type `?aes_string()`) |
| 18 | + |
| 19 | +**Answer** |
| 20 | + |
| 21 | +```{r} |
| 22 | +ggplot(aes_string(x = names(airquality)[1], y = names(airquality)[2])) |
| 23 | +``` |
| 24 | + |
| 25 | +This is soft deprecated, so the modern way going forward is: |
| 26 | + |
| 27 | +```{r} |
| 28 | +ggplot(airquality)+ |
| 29 | + geom_point(aes(x = .data[[names(airquality)[1]]], |
| 30 | + y = .data[[names(airquality)[2]]]))+ |
| 31 | + ... # other arguments |
| 32 | +``` |
| 33 | + |
| 34 | +The underlying logic works in the same way. We pass in strings to the aes() function. |
0 commit comments