Friday, January 22, 2010

Fluctuation plot using ggplot2 in R

Found this nice way to visually summarize contingency tables using ggplot2 in R on Hadley Wickham's ggplot2 cheat sheet. Using the same data in my previous post on making scatterplots in small multiples, I'll demonstrate how to use ggfluctuation() to make a fluctuation plot. For a two-dimensional table, this simply puts different dimensions on the x and y axes, and the area of the rectangles is proportional to the density of observations in that cell of the table. Install ggplot2 as in the previous post, and run this code.
# Load ggplot2. Use install.packages("ggplot2") if you do not already have ggplot2 installed.
library(ggplot2)

# Load the diamonds dataset.
data(diamonds)

# Look at a few rows of the relevant columns
diamonds[1:20,2:3]

# Display the contingency table
table(diamonds$cut,diamonds$color)

# Make the fluctuation plot
ggfluctuation(table(diamonds$cut,diamonds$color))


The table looks like this:

             D    E    F    G    H    I    J
Fair       163  224  312  314  303  175  119
Good       662  933  909  871  702  522  307
Very Good 1513 2400 2164 2299 1824 1204  678
Premium   1603 2337 2331 2924 2360 1428  808
Ideal     2834 3903 3826 4884 3115 2093  896

And the plot:


As a side note, I'm experimenting with a new way to post R code. If you hover your mouse over the code above you can click this button to copy all the text to your clipboard.

4 comments:

  1. Very nice. I particularly liked being able to copy the code that easily.

    ReplyDelete
  2. Thank you for the very nicely done tutorial. Just out of curiosity, how did you create the code box? Is it a plug-in of some sort?

    ReplyDelete
  3. Rob, used this method here (http://blog.cartercole.com/2009/10/awesome-syntax-highlighting-made-easy.html) using the R brush here (http://www.demitri.com/code/)

    ReplyDelete
  4. This is very nice type of display that I agree is good for summarising tabular data in graphical form.

    One of the strengths of R is that it is also possible to create this type of display using lattice graphics.

    For this example:

    library(lattice)
    library(ggplot2)

    diamond.df = data.frame(table(diamonds$cut,diamonds$color))

    levelplot(Freq ~ Var2*Var1, data = diamond.df, ylab = "Cut", xlab = "Colour",
    shrink = c(0.5, 1), aspect = "iso", col.regions = gray(seq(0.25, 0.75, 0.01))
    )

    should do the trick.

    ReplyDelete

Creative Commons License
Getting Genetics Done by Stephen Turner is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.