2009-09-08 43 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

相关问题