Â
These exercise cover the scales, statistics and themes of ggplot2 for Plotting in R.
In these exercises we look at adjusting the scales and themes of our plots.
library(ggplot2)
<- read.delim("./data/patients_clean_ggplot2.txt",sep="\t")
patients_clean
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight))+geom_point()
plot
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight))+geom_point()
+scale_x_continuous(breaks=c(20,30,40),label=c(20,30,40),limits=c(20,40))+
plotscale_y_continuous(breaks=seq(60,100,by=5),label=seq(60,100,by=5),
name="Weight (kilos)")
<- ggplot(data=patients_clean,
plot mapping=aes(x=factor(Age),y=BMI))+geom_violin(aes(fill=factor(Age)))+
scale_fill_brewer(palette="Blues", na.value="black")
library(ggplot2)
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
scale_colour_gradient2(low="blue",high="yellow",mid="grey",midpoint=mean(patients_clean$Height))
plot
library(ggplot2)
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
scale_colour_gradient2(low="blue",high="yellow",
mid="grey",midpoint=mean(patients_clean$Height),
limits=c(min(patients_clean$Height),180),
na.value=NA)
plot
## Warning: Removed 14 rows containing missing values (geom_point).
library(ggplot2)
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=Height))+geom_point()+
scale_colour_gradient2(low="blue",high="yellow",
mid="grey",midpoint=mean(patients_clean$Height),
breaks=c(min(patients_clean$Height),
median(patients_clean$Height),
quantile(patients_clean$Height)[4]),
labels=c(signif(min(patients_clean$Height),3),
signif(median(patients_clean$Height),3),
signif(quantile(patients_clean$Height)[4],3)))
plot
library(ggplot2)
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=Height,alpha=Count,size=Overweight))+geom_point()
plot
## Warning: Using size for a discrete variable is not advised.
library(ggplot2)
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
plot
## `geom_smooth()` using formula 'y ~ x'
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
<- plot+theme(legend.title=element_blank(),legend.background=element_rect(fill="white"),legend.key=element_rect(fill="white"),legend.position="bottom")
plot
plot
## `geom_smooth()` using formula 'y ~ x'
<- ggplot(data=patients_clean,
plot mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
<- plot+theme(legend.title=element_blank(),legend.background=element_rect(fill="white"),legend.key=element_rect(fill="white"),legend.position="bottom")
plot
<- plot+ggtitle("BMI vs Weight")+theme(panel.grid.minor=element_blank())
plot
plot
## `geom_smooth()` using formula 'y ~ x'
ggsave(plot,file="BMIvsWeight.png",units = "in",height = 7,width = 7)
## `geom_smooth()` using formula 'y ~ x'