These first few exercises will run through some of the simple principles of creating a ggplot2 object, assigning aesthetics mappings and geoms.
patients_clean <- read.delim("patients_clean_ggplot2.txt",sep="\t")
library(ggplot2)
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight))+geom_point()
plot
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()
plot
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
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
geom_smooth(method="lm",se=F)
plot
plot <- ggplot(data=patients_clean,
mapping=aes(x=Smokes,y=BMI))+geom_boxplot()
plot
plot <- ggplot(data=patients_clean,
mapping=aes(x=Smokes,y=BMI,colour=Sex))+geom_boxplot()
plot
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
plot <- ggplot(data=patients_clean,
mapping=aes(BMI))+
geom_histogram(fill="blue")
plot
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
HINT: alpha can be used to control transparancy.
plot <- ggplot(data=patients_clean,
mapping=aes(BMI))+ geom_density(aes(fill=Sex),alpha=0.5)
plot
plot <- ggplot(data=patients_clean,
mapping=aes(BMI))+ geom_density(aes(fill=Sex),alpha=0.5)
plot+facet_wrap(~Grade)