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.
## [1] "/__w/Plotting_In_R/Plotting_In_R/extdata"
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()
plot+facet_grid(Sex~Smokes)
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
geom_smooth()
plot
## `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?
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
geom_smooth(method="lm",se=F)
plot
## `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?
###Boxplots and Violin plots
plot <- ggplot(data=patients_clean,
mapping=aes(x=Smokes,y=BMI,colour=Sex))+
geom_boxplot()+
facet_wrap(~Age)
plot
HINT - Discrete values such as in factors are used for categorical data.
plot <- ggplot(data=patients_clean,
mapping=aes(x=Sex,y=BMI,colour=factor(Age)))+
geom_boxplot()+
facet_wrap(~Smokes)
plot
plot <- ggplot(data=patients_clean,
mapping=aes(x=Sex,y=BMI,colour=factor(Age)))+
geom_violin()+
facet_wrap(~Smokes)
plot
###Histogram and Density plots
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
HINT: alpha can be used to control transparancy.