Rob Weyant bio photo

Rob Weyant

Data Scientist at Powerley

Twitter LinkedIn Instagram Github Last.fm

Basic Syntax

library(ggplot2)
ggplot(mtcars,aes(factor(vs),mpg))+geom_boxplot()

plot of chunk unnamed-chunk-1

Additional Features

Add Colors

ggplot(mtcars,aes(factor(vs),mpg,fill=factor(vs)))+geom_boxplot()

plot of chunk unnamed-chunk-2

Multiple Groups

ggplot(mtcars,aes(factor(vs),mpg,fill=factor(am)))+geom_boxplot()

plot of chunk unnamed-chunk-3

Edit Width/Spacing

ggplot(mtcars,aes(factor(vs),mpg,fill=factor(am)))+
  geom_boxplot(width=0.4,                    # Controls Width of Boxes
               position=position_dodge(0.5), # Controls Space between Boxes
               size=1.2)                     # Controls Whisker Thickness

plot of chunk unnamed-chunk-4

Changing Themes

Modify Axes

standard_theme <- 
  theme(axis.text.x=element_text(size=14),    # Change x-axis value text-size
        axis.title.x=element_text(size=18),   # Change x-axis label text-size
        axis.text.y=element_text(size=14),    # Change y-axis value text-size
        axis.title.y=element_text(size=18),   # Change y-axis label text-size
        legend.text=element_text(size=14),    # Change legend value text-size
        legend.title=element_text(size=16))   # Change legend title text-size

ggplot(mtcars,aes(factor(vs),mpg,fill=factor(am)))+
  geom_boxplot(width=0.4,                    
               position=position_dodge(0.5), 
               size=1.2)+
  scale_x_discrete('VS')+
  scale_y_continuous('Miles Per Gallon (MPG)')+
  scale_fill_discrete('AM')+
  standard_theme

plot of chunk unnamed-chunk-5

More Resources