Plotting with ggplot

The Blank Canvas

\(~\)

ggplot2 is a library that is built to follow the Grammar of Graphics blah. What this means for us is that it is a little easier to produce nice graphs by structuring grahpics production as a layered process. We will start with a blank canvas and add layers to this. To start, we will make a plot of the iris dataset with a histogram of the sepal length column.

ggplot(iris, aes(Sepal.Length, color = Species, fill = Species)) +
    geom_histogram(alpha = 0.7) +
    labs(title = "Sepal Length and Species Histogram", x = "Sepal Length") +
  theme_minimal()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

We can also produce a boxplot.

library(RColorBrewer)
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
    geom_boxplot( alpha = 0) +
    geom_jitter(alpha = 0.3, color = "tomato") +
    labs(title = "Sepal Length and Species Boxplots", xlab = "Species", ylab = "Sepal Length") +
    theme_light()