How to rotate the x-axis labels 90 degrees in levelplot

Atticus29 picture Atticus29 · Aug 11, 2012 · Viewed 14.2k times · Source

I'm trying to visualize a correlation map, and I think that I've gotten it to work, except for the jumbling of the x-axis labels. Any suggestions for how to rotate them? Some of the parameters for many of the Base R plots don't seem available for levelplots. It's possible that I don't know what I'm talking about.

This is the data I used:

cor2<-structure(c(1, -0.0389681095266643, -0.0389681095266643, 0.178476154216706, 
-0.178476154216706, -0.168494117498168, -0.0531355593389041, 
-0.0627159607856109, -0.11922152176561, 0.00887070126495505, 
-0.0721998017906867, -0.140654273521441, 0.0257953297166468, 
-0.0504004052031319, -0.057270788080051, -0.0918121581058645, 
-0.0389681095266643, 1, 1, -0.207885482210833, 0.207885482210833, 
0.221906241587463, 0.285338260198777, 0.370743678038144, 0.219208701879567, 
0.266580102221828, 0.193378655575033, 0.23922965323291, 0.164352387360997, 
0.229607796224011, 0.184412911672718, -0.250294366329347, -0.0389681095266643, 
1, 1, -0.207885482210833, 0.207885482210833, 0.221906241587463, 
0.285338260198777, 0.370743678038144, 0.219208701879567, 0.266580102221828, 
0.193378655575033, 0.23922965323291, 0.164352387360997, 0.229607796224011, 
0.184412911672718, -0.250294366329347, 0.178476154216706, -0.207885482210833, 
-0.207885482210833, 1, -1, -0.836264694082863, 0.0968492347163521, 
-0.529206721168992, -0.368511719318385, -0.370488391611307, -0.446552321746797, 
-0.0946404317035884, -0.166601542941319, -0.371982811624152, 
-0.0551151773556682, -0.0252777576827408, -0.178476154216706, 
0.207885482210833, 0.207885482210833, -1, 1, 0.836264694082863, 
-0.0968492347163521, 0.529206721168992, 0.368511719318385, 0.370488391611307, 
0.446552321746797, 0.0946404317035884, 0.166601542941319, 0.371982811624152, 
0.0551151773556682, 0.0252777576827408, -0.168494117498168, 0.221906241587463, 
0.221906241587463, -0.836264694082863, 0.836264694082863, 1, 
-0.0484257163697656, 0.493261440648374, 0.421352971692481, 0.335283617372406, 
0.42535494057661, 0.0277745485055271, 0.0990554932499753, 0.268268745412774, 
0.106129159123147, 0.0645326205432567, -0.0531355593389041, 0.285338260198777, 
0.285338260198777, 0.0968492347163521, -0.0968492347163521, -0.0484257163697656, 
1, -0.525318257575221, -0.160726744335619, -0.530266513726872, 
-0.523896249354085, 0.0821688285842216, 0.120061871987285, -0.0462809753885581, 
0.0894673511307366, -0.157709074647179, -0.0627159607856109, 
0.370743678038144, 0.370743678038144, -0.529206721168992, 0.529206721168992, 
0.493261440648374, -0.525318257575221, 1, 0.450831181390043, 
0.894070902852474, 0.937461049803717, 0.120717838386174, 0.0922093724569894, 
0.421886104540215, 0.0686035045695465, -0.0259181463830899, -0.11922152176561, 
0.219208701879567, 0.219208701879567, -0.368511719318385, 0.368511719318385, 
0.421352971692481, -0.160726744335619, 0.450831181390043, 1, 
0.194931612275736, 0.475810998910999, 0.0674296214446775, 0.0665009602984884, 
0.181289860729344, -0.0399243218645222, -0.0918283721705961, 
0.00887070126495505, 0.266580102221828, 0.266580102221828, -0.370488391611307, 
0.370488391611307, 0.335283617372406, -0.530266513726872, 0.894070902852474, 
0.194931612275736, 1, 0.955425650934062, 0.0881868869011078, 
0.139156258136006, 0.310078412850343, 0.137566770975812, -0.0893525009590147, 
-0.0721998017906867, 0.193378655575033, 0.193378655575033, -0.446552321746797, 
0.446552321746797, 0.42535494057661, -0.523896249354085, 0.937461049803717, 
0.475810998910999, 0.955425650934062, 1, 0.0988326951129949, 
0.147058790355866, 0.336760945050752, 0.110616903520016, -0.107521647802021, 
-0.140654273521441, 0.23922965323291, 0.23922965323291, -0.0946404317035884, 
0.0946404317035884, 0.0277745485055271, 0.0821688285842216, 0.120717838386174, 
0.0674296214446775, 0.0881868869011078, 0.0988326951129949, 1, 
0.746502805562635, 0.144029358291222, 0.496146773442592, -0.464865772986093, 
0.0257953297166468, 0.164352387360997, 0.164352387360997, -0.166601542941319, 
0.166601542941319, 0.0990554932499753, 0.120061871987285, 0.0922093724569894, 
0.0665009602984884, 0.139156258136006, 0.147058790355866, 0.746502805562635, 
1, 0.068339761274059, 0.546006730280353, -0.392180163611345, 
-0.0504004052031319, 0.229607796224011, 0.229607796224011, -0.371982811624152, 
0.371982811624152, 0.268268745412774, -0.0462809753885581, 0.421886104540215, 
0.181289860729344, 0.310078412850343, 0.336760945050752, 0.144029358291222, 
0.068339761274059, 1, -0.118872875719514, 0.0250634712618911, 
-0.057270788080051, 0.184412911672718, 0.184412911672718, -0.0551151773556682, 
0.0551151773556682, 0.106129159123147, 0.0894673511307366, 0.0686035045695465, 
-0.0399243218645222, 0.137566770975812, 0.110616903520016, 0.496146773442592, 
0.546006730280353, -0.118872875719514, 1, -0.0649756207052158, 
-0.0918121581058645, -0.250294366329347, -0.250294366329347, 
-0.0252777576827408, 0.0252777576827408, 0.0645326205432567, 
-0.157709074647179, -0.0259181463830899, -0.0918283721705961, 
-0.0893525009590147, -0.107521647802021, -0.464865772986093, 
-0.392180163611345, 0.0250634712618911, -0.0649756207052158, 
1), .Dim = c(16L, 16L), .Dimnames = list(c("ID", "QnWeight_initial", 
"QnWeight_initial_mg", "Days_till_1st_Wrkr", "Days_before_max_Wrkr_Eclosion", 
"Wrkr_Eclosion_Bin", "QnMass_At_Wrkr_Eclosion", "ColonyMass_At_Wrkr_Eclosion", 
"Adult_Wrkrs_At_Wrkr_Eclosion", "Mature_Brood_At_Wrkr_Eclosion", 
"Sum_wrkrsPlusBrood_At_Wrkr_Eclosion", "QnMass_2wksLater", "QnMass_4wksLater", 
"ColonyMass_4wksLater", "QnMass_2mnthsLater", "ColonyMass_2mnthsLater"
), c("ID", "QnWeight_initial", "QnWeight_initial_mg", "Days_till_1st_Wrkr", 
"Days_before_max_Wrkr_Eclosion", "Wrkr_Eclosion_Bin", "QnMass_At_Wrkr_Eclosion", 
"ColonyMass_At_Wrkr_Eclosion", "Adult_Wrkrs_At_Wrkr_Eclosion", 
"Mature_Brood_At_Wrkr_Eclosion", "Sum_wrkrsPlusBrood_At_Wrkr_Eclosion", 
"QnMass_2wksLater", "QnMass_4wksLater", "ColonyMass_4wksLater", 
"QnMass_2mnthsLater", "ColonyMass_2mnthsLater")))

My code:

rgb.palette <- colorRampPalette(c("blue", "yellow"), space = "rgb")
levelplot(cor2, aspect="iso", main="Correlation Matrix", xlab="", xaxt='n', ylab="", col.regions=rgb.palette(120), cuts=100, at=seq(0,1,0.01)) #, names.arg=paste(cor2[,1]),las=3

#this is what I tried, and it didn't work. I couldn't get names(cor2) to work, but I guess that this is a separate question.
labs<- (cor2[,0])
text(cex=1, x=x-0.25, y=-1.5, labs, xpd=TRUE, srt=90)

##want something like this, but can't find an analogous way to remove axisnames and add text
#x <- barplot(table(mtcars$cyl),axisnames=FALSE)#,xaxt="n" 
#labs <- paste(names(table(mtcars$cyl)), "cylinders")
#text(cex=1, x=x-0.25, y=-1.5, labs, xpd=TRUE, srt=90)

Answer

plannapus picture plannapus · Aug 11, 2012

As far as the rotation of the x-axis labels go, you have to use parameter scales (see ?xyplot for more information on lattice parameters). You want to modify only its x component: scales=list(x=list(rot=90)). In context:

levelplot(cor2,
          aspect="iso", scales=list(x=list(rot=90)),
          main="Correlation Matrix", col.regions=rgb.palette(120),
          cuts=100, at=seq(0,1,0.01))