2009-09-08 59 views
5

我想將x軸標籤分成兩行。我也想標籤旋轉45度。我怎樣才能做到這一點?是否有可能在基礎圖形中將軸標籤分爲兩行?

我有什麼至今:

N <- 10 
dnow <- data.frame(x=1:N, y=runif(N), labels=paste("This is observation ",1:N)) 
with(dnow, plot(x,y, xaxt="n", xlab="")) 
atn <- seq(1,N,3) 
axis(1, at=atn, labels=labels[atn]) 
+0

你是什麼意思2行?你的意思是你想要「這是\ n觀察......」嗎? – 2009-09-08 17:52:13

+0

@chis_dubois這是答案的第一部分!謝謝! – 2009-09-08 18:13:43

回答

12

這裏有一個可能性,與ggplot2包。

N <- 10 
labs <- factor(1:N,labels=paste("This is \n observation",1:N)) 
dnow <- data.frame(x=1:N, y=runif(N), labels=labs) 
qplot(labels,y,data=dnow) + 
     opts(axis.text.x=theme_text(angle=-45,hjust=0)) 

alt text http://i28.tinypic.com/k024p3.png

我期待着看到基礎包的例子呢!

+0

@chris - 我想接受你的問題(這些天所有的酷孩子都使用ggplot2)。但是,你能否事先修正x軸(ggplot按字母順序排列字符向量,所以你必須將它轉換爲一個因子)。謝謝! – 2009-09-10 01:40:30

+0

好的建議。我把它變成了一個因素,但右側仍然被切斷。我會盡力解決這個問題。 – 2009-09-10 02:12:07

+0

這也適用於數據標籤 - 如果您將\ n放入其中,它會斷線! – Andrew 2013-04-10 23:26:57

4

這是我使用基本圖形炮製了(之前我ggplot2天):

## data 
N <- 10 
dnow <- data.frame(x=1:N, y=runif(N), labels=paste("This is \nobservation ",1:N)) 
## make margins wide 
par(mfrow=c(1,1), mar=c(10,10,6,4)) 
## plot without axix labels or ticks 
with(dnow, plot(x,y, xaxt="n", xlab="")) 
## the positions we ant to plot 
atn <- seq(1,N,3) 
## the label for these positions 
lab <- dnow$labels[atn] 
## plot the axis, but do not plot labels 
axis(1, at=atn, labels=FALSE) 
## plot labels 
text(atn, ## x position 
    par("usr")[3]-.05, ## position of the low axis 
    srt=45, ## angle 
    labels=lab, ##labels 
    xpd=TRUE, ## allows plotting outside the region 
    pos=2) 
## par("usr")[3] 
+0

我覺得這也是一個重要的貢獻。用'ggplot2'可能這不是必需的。但也有好處。 Paul Murrel在R Graphics的書中也給出了一個更好更優雅的「網格」解決方案。 – Sam 2011-02-18 00:14:54

相關問題