These exercises cover the Geoms and Aesthetics of ggplot2 for Plotting in R.
These first few exercises will run through some of the simple principles of creating a ggplot2 object, assigning aesthetics mappings and geoms.
Exercise 1 - Scatterplots
Read in the cleaned patients dataset as we saw in ggplot2 course earlier (“patients_clean_ggplot2.txt”)
Using the patient data set generate a scatter plot of BMI versus Weight.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## i This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## i Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## i This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## i Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
Exercise 2 - Boxplots, Violin and Jitter plots
## Warning: Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Warning: Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Warning: Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Warning: Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
## Groups with fewer than two datapoints have been dropped.
## i Set `drop = FALSE` to consider such groups for position adjustment purposes.
Exercise 3 - Histogram and Density plots
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
HINT: alpha can be used to control transparency.
Exercise 4 - GGplot object
## ID Name Race Age Sex Smokes Height Weight
## 1 Patient-001 Demetrius White 44 Male Smoker 176.4791 77.07070
## 2 Patient-002 Rosario White 43 Male Smoker 174.7485 83.26835
## 3 Patient-003 Julio Black 44 Male Non-Smoker 173.9449 85.63231
## 4 Patient-004 Lupe White 44 Male Non-Smoker 180.0289 88.72032
## 5 Patient-005 Lavern Cat 43 Male Non-Smoker 178.6365 97.05405
## 6 Patient-006 Bernie Native 43 Female Non-Smoker 159.7778 68.31247
## Birth State Pet Grade_Level Died Count Date Grade
## 1 1972-02-11 New York Cat 1 TRUE -0.7440011 2015-03-04 1
## 2 1972-07-22 Florida Cat 1 TRUE -1.0714531 2015-03-04 1
## 3 1971-11-23 Connecticut Dog 2 FALSE -0.8215636 2015-03-04 2
## 4 1971-10-03 Massachusetts Dog 1 TRUE 0.4976671 2015-03-04 1
## 5 1972-11-23 Kansas Cat 3 FALSE -1.2099217 2015-03-04 3
## 6 1972-07-30 Illinois Cat 2 FALSE 0.5241618 2015-03-04 2
## BMI Overweight
## 1 24.74586 FALSE
## 2 27.26799 TRUE
## 3 28.30182 TRUE
## 4 27.37403 TRUE
## 5 30.41397 TRUE
## 6 26.75882 TRUE
## Aesthetic mapping:
## * `x` -> `BMI`