Skip to content

Instantly share code, notes, and snippets.

@johnstantongeddes
Last active September 25, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johnstantongeddes/9d1f15209c28e435dcb5 to your computer and use it in GitHub Desktop.
Save johnstantongeddes/9d1f15209c28e435dcb5 to your computer and use it in GitHub Desktop.
ggplot2 examples
---
title: "GrammaR of graphics using `ggplot2` in R"
author: "John Stanton-Geddes"
date: "December 8, 2014"
output: html_document
---
The `ggplot2` package, deveoped by Hadley Wickham, is the most downloaded R package of [all time](http://www.rdocumentation.org/) and one of the standards for publication-ready figures. Fron the [ggplot website](http://ggplot2.org/)
> ggplot2 is a plotting system for R, based on the grammar of graphics, which tries to take the good parts of base and lattice graphics and none of the bad parts. It takes care of many of the fiddly details that make plotting a hassle (like drawing legends) as well as providing a powerful model of graphics that makes it easy to produce complex multi-layered graphics.)
That said, the syntax for generating plots is distinct from that of base R and can take some practice, constant checking of the [documentation](http://docs.ggplot2.org/current/) and searching on [stack overflow](http://stackoverflow.com/questions/tagged/ggplot2) to become familiar with.
Here, I show a few examples.
## Bar chart
From the `ggplot` documentation.
```{r}
library(ggplot2)
library(gridExtra)
library(plyr)
summary(diamonds)
# define plot
c <- ggplot(diamonds, aes(clarity))
# by default, uses stat="bin", which gives the count in each category
c + geom_bar()
```
## Histograms
```{r}
h <- ggplot(diamonds, aes(carat))
h + geom_histogram()
h + geom_histogram(binwidth = .1)
```
## Faceting
```{r}
# stacked barplot
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
# facets
ggplot(diamonds, aes(clarity)) + geom_bar() +
facet_wrap(~ cut)
```
## Boxplots
```{r, echo=FALSE}
d <- ggplot(diamonds, aes(x = color, y = log(price))) + geom_boxplot()
d
```
## Line plots
```{r}
# summarise diamond price by color and carat
ddiamonds <- ddply(diamonds, .(color, carat), summarise, mprice = mean(price))
str(ddiamonds)
# plot
e <- ggplot(ddiamonds, aes(x = carat, y = log(mprice), group = color)) +
geom_line()
e
# color lines
e + geom_line(aes(colour = color))
```
Lines of the actual data are nice, but smooth lines are better.
```{r}
# smooth
e + geom_smooth(stat = "smooth")
e + stat_smooth(method = "loess")
# lm
e + stat_smooth(method = "lm")
e + stat_smooth(method = "lm", formula = y ~ (poly(x, 3)))
```
Plot multiple time series on a single graph.
```{r}
y2005 <- runif(300, 20, 120)
y2010 <- y2005 * runif(300, -1.05, 1.5)
group <- rep(LETTERS[1:3], each = 100)
df <- data.frame(id = seq_along(group), group, y2005, y2010)
library(reshape2) # for melt
dfm <- melt(df, id.var = c("id", "group"))
ggplot(dfm, aes(variable, value, group = id, colour = group)) +
geom_path(alpha = 0.5)
# note the alpha parameter that specifies line intensity
```
Here's an example of using ggplot in a Shiny interactive webapp: [https://johnsg.shinyapps.io/ApRxN-shinyapp/]
## qplot
If you're already familiar with base R graphic syntax, you can use `qplot` for most ggplots. For example:
```{r}
qplot(color, log(price), geom = "boxplot", data = diamonds)
```
For full functionality, I recommend just getting used to the `ggplot` syntax.
## Arrange plots
Even after faceting plots, sometimes you want to group multiple plots into a single figure. In base R, you could do this using combination of `par(mfrow = c(x,x))`. For ggplots, you use the `grid.arrange()` function from the [gridExtra](http://cran.r-project.org/web/packages/gridExtra/index.html) package.
```{r}
e <- ggplot(diamonds, aes(x = color)) + geom_bar()
grid.arrange(d, e, ncol = 1)
```
Note that the [grid package](http://www.inside-r.org/r-doc/grid/viewport) provides a more extensible way to do this using `viewports`, though not quite as simply as `grid.arrange`.
## Other random things
Violin plot - show the probability desnity at different values.
```{r}
v <- ggplot(mtcars, aes(factor(cyl), mpg))
v + geom_violin()
# gitter
v + geom_violin() + geom_jitter(height = 0)
```
Rug plot
```{r}
r <- ggplot(mtcars, aes(x=wt, y=mpg))
r + geom_point()
r + geom_point() + geom_rug()
```
## ggvis
The cutting-edge of ggplot is a new package called [ggvis](http://ggvis.rstudio.com/) that allows for interactive plots. Here are a few examples:
```{r}
library(ggvis)
library(dplyr)
# points
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points()
# smooth lines
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_smooths()
# grouping
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points(fill = ~factor(cyl))
```
Interactive examples
```{r}
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_smooths(span = input_slider(0.5, 1, value = 1)) %>%
layer_points(size := input_slider(100, 1000, value = 100))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment