2012-08-10 179 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