1

I am looking to improve upon output I implemented in R based on Jeromy's answer here (thanks!). Mine is a 31x31 matrix with positive and negative values, and uses basically the same ggplot2 code:

library(ggplot2)
library(reshape)

z<-cor(insheet3,use="complete.obs",method="kendall")
zm<-melt(z)
ggplot(zm, aes(X1,X2, fill=value)) +  geom_tile() +
scale_fill_gradient2(low = "blue",  high = "dark violet")  

I need to change three things:

  1. Right now, the rows appear in reverse alphabetical order, which means no visible data trends. How can I influence the order of the rows and columns, such that either:

    A. (Preferred:) The columns are ordered by correlation value (negative to positive or vice versa), as they are in the ellipse package output on that same page; or

    B. The columns are manually ordered, so that I can group similar variables?

  2. Along the bottom X-axis, my variable names are overlapping dramatically and are unreadable. They need to remain long (i.e., OrthoPhos, Ammonia, Residential...), so how can I rotate their labels 90 degrees?

  3. Is there a way to remove the "X1" and "X2" labels along each axis?

Thank you!

Community
  • 1
  • 1
Alex G.
  • 21
  • 3
  • 3
    These problems are generally solved by changing the ordering of levels in factor variables. Data examples are needed if you want to see code. Or you coud simply do an SO search since this question has been addressed many times. – IRTFM Mar 29 '13 at 22:29
  • Check [**this**](http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/#changing-the-order-of-items) and [**this**](http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/#axis-labels-and-text-formatting) – Arun Mar 29 '13 at 22:38
  • And [**this**](http://trinkerrstuff.wordpress.com/2012/10/15/how-do-i-re-arrange-ordering-a-plot/) – Tyler Rinker Mar 29 '13 at 23:08
  • 1
    Thanks, @Arun & Tyler. This being the first time I've used ggplot2, I didn't know that all its "+" add-ons could be applied to all ggplot2's different types of plots (not just geom_tile). That solved the problem and opened up a new world, thanks! – Alex G. Jul 16 '13 at 20:03

2 Answers2

1

Following what I'll call an extensive/religious R journey into correlation matrix possibilities, I wanted to share what I'm finally going to use. Also, thanks to the previous answerers; I've found that there are many "right" answers to this.

Since my reviewers insisted I include numbers and not just colors, and that I stay away from more "confusing" and "busy" output like correlogram, I finally found "image" and based my final output on this example. Thanks @Marcinthebox.

Also to appease StackOverflow, here is a link to the image, rather than the image itself.

Because some of these specifications took a while to figure out and were critical to the final output, here's my code, shortened as much as I could.

#Subsetting to only the vectors I want to see in the correlation, as ordered
insheet<-subset(insheet1,
    select=c("Cond", "CL", "SO4", "TN", "TP", "OrthoPhos", "DO", ...., "Rural"))

#Defining "high" and "low" colors
library(colorspace)
mycolors<-diverge_hcl(8,  h = c(8, 240), c = 80, l = c(50,100), power = 1)

#Correlating them into a matrix
sheet<-cor(insheet,use="complete.obs")

#Making it!
image(x=seq(dim(sheet)[2]), y=seq(dim(sheet)[2]), z=sheet, ann=FALSE, 
    col=mycolors,  xlab="x column", ylab="y column", xaxt='n', yaxt='n')
text(expand.grid(x=seq(dim(sheet)[2]), y=seq(dim(sheet)[2])),
    labels=round(c(sheet),2), cex=0.5)
axis(1, 1:dim(insheet2)[2], colnames(insheet2), las=2)
axis(2, 1:dim(insheet2)[2], colnames(insheet2), las=2)
par(mar=c(5.5, 5.5, 2, 1)) #Moves margins over to allow for axis labels

I was also able to for-loop this to output multiple .wmf files, once errors were suppressed. Too bad I couldn't visualize significant p-values as well... another time. Thanks!

Community
  • 1
  • 1
Alex G.
  • 21
  • 3
0

I assume that you mean "clustering" for point 1.? For such tasks I prefer the heatmap.2() function from the gplots package, which offers various clustering options.

For point 2 and 3: The heatmap.2() function will also take care of the 90º rotation and the labels since it is using a data matrix as input instead of a data table.