Creating boxplots

In ggplot, we create boxplots using geom_boxplot(). Here, you will create a boxplot of the heights of female patients, partitioned by ethnicity. As in previous examples, we use the subset() command to include only females. We can create the subset either before we use ggplot, or within ggplot, as follows:

 H <- ggplot(subset(T, GENDER == "F"), aes(factor(ETH), HEIGHT)) 

You can create a basic boxplot yourself by entering the following syntax:

H + geom_boxplot() 

You should have got a basic boxplot of height, partitioned by ethnicity. For the remainder of this section, we will embellish our basic boxplot.

Note

Try the following boxplots for yourself:

H + geom_boxplot() + geom_jitter() 
H + geom_boxplot() + coord_flip() 
H + geom_boxplot(outlier.color = "red", outlier.size = 5) 
H + geom_boxplot(aes(fill = SMOKE))
H + geom_boxplot(fill= "#99CCFF" , color="#990000")  

Next we set our choice of fill color and outline color from the Hexadecimal Color Chart, using the following syntax:

 H + geom_boxplot(aes(fill = factor(ETH))) + scale_fill_manual(values = c("#CCCC99","#FFCCCC","#99CCFF"))

You will get this boxplot:

Creating boxplots
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset