2012-08-10 305 views
10

我试图想象一个相关性地图,我认为我已经得到它的工作,除了x轴标签的混乱。有关如何旋转它们的建议?许多Base R图的一些参数似乎不适用于水平图。有可能我不知道我在说什么。如何在水平面上旋转x轴标签90度

这是我使用的数据:

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"))) 

我的代码:

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) 
+1

看起来你需要深入挖掘'lattice'(或者'grid')来做到这一点。对于初学者,你需要格子函数'ltext()'而不是基本图形'text()'。另外,对于需要'实验室< - colnames(cor2)'的标签。 – bdemarest 2012-08-11 00:10:17

回答

22

至于x轴标签的旋转去,你必须使用参数scales(见?xyplot有关lattice参数的更多信息)。您只想修改其x组件:scales=list(x=list(rot=90))。在上下文中:

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)) 
+0

工作,真棒。然而,我有一个问题:为什么你必须指定x scale属性/列表,并确实:'''labelrotation = c(0,90) scales = list(rot = labelrotation)'''not work,since rotation can是一个为x轴和y轴指定旋转的矢量吗? – 2014-05-15 22:44:22

+0

这是一个很好的问题(你应该问这是一个适当的问题,以获得更多的知名度),我不知道如何回答。从我读的文档'rot 轴标签旋转的角度(以度为单位)。可以是长度为2的矢量,分别控制左/右和右/顶轴。这使我认为它不应该是ax/y矢量(否则它将分别为“左和底”,而不是“左” /底部和右侧/顶部轴线分开“)。 – plannapus 2014-05-16 12:58:11