Â
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)
patients_clean <- read.delim("./data/patients_clean_ggplot2.txt",sep="\t")
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))+geom_point()
plot+scale_x_continuous(breaks=c(20,30,40),label=c(20,30,40),limits=c(20,40))+
scale_y_continuous(breaks=seq(60,100,by=5),label=seq(60,100,by=5),
name="Weight (kilos)")
plot <- ggplot(data=patients_clean,
mapping=aes(x=factor(Age),y=BMI))+geom_violin(aes(fill=factor(Age)))+
scale_fill_brewer(palette="Blues", na.value="black")
library(ggplot2)
plot <- ggplot(data=patients_clean,
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)
plot <- ggplot(data=patients_clean,
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 or values outside the scale range
## (`geom_point()`).
library(ggplot2)
plot <- ggplot(data=patients_clean,
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)
plot <- ggplot(data=patients_clean,
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)
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
plot
## `geom_smooth()` using formula = 'y ~ x'
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
plot <- plot+theme(legend.title=element_blank(),legend.background=element_rect(fill="white"),legend.key=element_rect(fill="white"),legend.position="bottom")
plot
## `geom_smooth()` using formula = 'y ~ x'
plot <- ggplot(data=patients_clean,
mapping=aes(x=BMI,y=Weight,colour=factor(Age)))+geom_point()+stat_smooth(method="lm",se=F)
plot <- 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
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'