# 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.
Very nice. I particularly liked being able to copy the code that easily.
ReplyDeleteThank 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?
ReplyDeleteRob, 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/)
ReplyDeleteThis is very nice type of display that I agree is good for summarising tabular data in graphical form.
ReplyDeleteOne 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.