Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
r_hadleyverse [2016/11/02 21:12] xaviergb [ggplot2 - Challenge #1] |
r_hadleyverse [2016/11/02 22:46] (current) xaviergb [Workshop 3: Intro to ggplot2, tidyr & dplyr] |
||
---|---|---|---|
Line 15: | Line 15: | ||
Link to associated Prezi: [[http://prezi.com/daz9r0cj1si4/|Prezi]] | Link to associated Prezi: [[http://prezi.com/daz9r0cj1si4/|Prezi]] | ||
- | Download the R script for this lesson: [[http://qcbs.ca/wiki/_media/ggplot.r|Script]] | + | Download the R script for this lesson: {{:workshop3.r|Script}} |
===== 1. Plotting in R using the Grammar of Graphics (ggplot2) ===== | ===== 1. Plotting in R using the Grammar of Graphics (ggplot2) ===== | ||
Line 97: | Line 97: | ||
===== ggplot2 - Challenge #1 ===== | ===== ggplot2 - Challenge #1 ===== | ||
- | Using the ''qplot()'' function, build a basic scatter plot with a title and axis labels from one of the ''CO2'' or ''BOD'' data sets in R. You can load these and explore their contents as follows: | + | //Using the ''qplot()'' function, build a basic scatter plot with a title and axis labels from one of the ''CO2'' or ''BOD'' data sets in R. You can load these and explore their contents as follows:// |
<code rsplus | > | <code rsplus | > | ||
Line 242: | Line 242: | ||
print(linear.smooth.plot) | print(linear.smooth.plot) | ||
</code> | </code> | ||
+ | |||
{{ggplot2_6.png?400 |}} | {{ggplot2_6.png?400 |}} | ||
Line 248: | Line 249: | ||
- | ===== BONUS===== | + | ===== BONUS ===== |
You can even use emojis as your geoms!!! You need the emojiGG package by David Lawrence Miller. | You can even use emojis as your geoms!!! You need the emojiGG package by David Lawrence Miller. | ||
Line 263: | Line 264: | ||
geom_emoji(emoji="1f337") | geom_emoji(emoji="1f337") | ||
</code> | </code> | ||
- | {{rplotbear.png?400 |}} | ||
- | | ||
+ | {{:rplotbear.png?400|}} | ||
+ | |||
+ | |||
+ | ---- | ||
Line 274: | Line 277: | ||
===== ggplot2 - Challenge # 2 ===== | ===== ggplot2 - Challenge # 2 ===== | ||
- | Produce a colourful plot with linear regression from built in data such as the ''CO2'' dataset or the ''msleep'' dataset: | + | //Produce a colourful plot with linear regression from built in data such as the ''CO2'' dataset or the ''msleep'' dataset:// |
<code rsplus | > | <code rsplus | > | ||
Line 299: | Line 302: | ||
++++ | ++++ | ||
+ | ---- | ||
===== 1.7 Adding multiple facets ===== | ===== 1.7 Adding multiple facets ===== | ||
Line 345: | Line 349: | ||
{{::ggplot2_11.png?800|}} | {{::ggplot2_11.png?800|}} | ||
+ | ---- | ||
===== ggplot2 - Challenge # 3 ===== | ===== ggplot2 - Challenge # 3 ===== | ||
- | Explore a new geom and other plot elements with your own data or built in data. Look [[http://docs.ggplot2.org/current/index.html|here]] for some inspiration and examples! | + | //Explore a new geom and other plot elements with your own data or built in data. Look [[http://docs.ggplot2.org/current/index.html|here]] or [[http://shinyapps.stat.ubc.ca/r-graph-catalog/|here]] for some inspiration and great examples!// |
<code rsplus | > | <code rsplus | > | ||
Line 367: | Line 372: | ||
++++ | ++++ | ||
+ | ---- | ||
===== 1.9 Saving plots ===== | ===== 1.9 Saving plots ===== | ||
Line 611: | Line 617: | ||
---- | ---- | ||
- | ===== TIDYR CHALLENGE ==== | + | ===== tidyr CHALLENGE ==== |
- | //Gather and spread the airquality to return the same format as the original data using month and day// | + | //Gather and spread the airquality to return the same format as the original data using month and day.// |
++++Solution| | ++++Solution| | ||
Line 725: | Line 731: | ||
**Change data from wide to long format** | **Change data from wide to long format** | ||
- | //(See back to Section 6.3)// | + | //(See back to Section 2.3)// |
<code rsplus | > | <code rsplus | > | ||
Line 954: | Line 960: | ||
---- | ---- | ||
- | **CHALLENGE:** | + | |
+ | ===== dplyr CHALLENGE ===== | ||
//Using the ''ChickWeight'' dataset, create a summary table which displays the weight gain of individual chicks in the study. Employ ''dplyr'' verbs and the ''%>%'' operator.// | //Using the ''ChickWeight'' dataset, create a summary table which displays the weight gain of individual chicks in the study. Employ ''dplyr'' verbs and the ''%>%'' operator.// | ||
Line 982: | Line 989: | ||
---- | ---- | ||
- | Note that we can group the data frame using more than one factor, using the general syntax as follows: ''group_by(group1, group2, ...)''. This approach allows us to carry out operations on ''group2'' while conserving information on ''group1'' within the summary table. ** HINT HINT ** | + | ==== Ninja Hint ==== |
+ | |||
+ | Note that we can group the data frame using more than one factor, using the general syntax as follows: ''group_by(group1, group2, ...)''. This approach allows us to carry out operations on ''group2'' while conserving information on ''group1'' within the summary table. | ||
---- | ---- | ||
- | **dplyr NINJA CHALLENGE:** | + | |
+ | ===== dplyr NINJA CHALLENGE ===== | ||
//Using the ''ChickWeight'' dataset, create a summary table which displays the average weight gain of individual chicks for each diet in the study. Employ ''dplyr'' verbs and the ''%>%'' operator.// | //Using the ''ChickWeight'' dataset, create a summary table which displays the average weight gain of individual chicks for each diet in the study. Employ ''dplyr'' verbs and the ''%>%'' operator.// | ||
Line 1023: | Line 1033: | ||
===== 4. Resources ===== | ===== 4. Resources ===== | ||
- | Here are some great resources for learning tidyr and dplyr that we used when compiling this workshop: | + | Here are some great resources for learning ggplot2, tidyr and dplyr that we used when compiling this workshop: |
- | //Reshape / Reshape2// | + | //ggplot2// |
- | * [[http://seananderson.ca/2013/10/19/reshape.html]] | + | * [[http://shinyapps.stat.ubc.ca/r-graph-catalog/|The R Graph Catalog]] |
- | * [[http://had.co.nz/reshape/introduction.pdf]] | + | * [[https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf|The RStudio ggplot2 Cheat Sheet]] |
- | * [[http://cran.r-project.org/web/packages/reshape2/reshape2.pdf]] | + | |
Lecture notes from Hadley's course Stat 405 (the other lessons are awesome too!) | Lecture notes from Hadley's course Stat 405 (the other lessons are awesome too!) | ||
Line 1034: | Line 1043: | ||
* [[http://stat405.had.co.nz/lectures/19-tables.pdf]] | * [[http://stat405.had.co.nz/lectures/19-tables.pdf]] | ||
- | //dplyr// | + | //dplyr and tidyr// |
- | * [[https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html]] | + | * [[https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf|The RStudio Data Wrangling Cheat Sheet]] |
- | * [[http://seananderson.ca/2014/09/13/dplyr-intro.html]] | + | * [[https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html|CRAN Intro to dplyr]] |
+ | * [[http://seananderson.ca/2014/09/13/dplyr-intro.html|Sean Anderson's Intro to dplyr and pipes]] | ||
+ | * [[https://rpubs.com/bradleyboehmke/data_wrangling|Bradley Boehmke's Intro to data wrangling]] | ||