2011-11-11 14 views
5

enter image description here私は以下のようなコードを持っています。しかし、私はすべてのx軸ラベルを取得していないと私はpdfでこれをしようとすると、45度で表示されていません。私は新しいので、このオプションを修正するのを手伝ってください。私はあなたのコードを実行するとすべてのx軸ラベルが45度で表示されない

pdf(file="figure.pdf", height=3.5, width=5, onefile=TRUE) 
    Runtime <- c(579,0,581,610,830,828,592,651,596,596,591,581,587,594,604,606,447,434,445) 
    Runtime 
    g_range <- range(0,Runtime) 
    g_range 
    plot(Runtime, type="o", col="blue", ylim=g_range,axes=FALSE, ann=FALSE) 
    lab=c('2011-07-20','2011-08-03','2011-08-10','2011-08-17','2011-08-24','2011-08-25','2011-08-27','2011-08-31','2011-09-07','2011-09-10','2011-09-14','2011-09-21','2011-09-28','2011-10-05','2011-10-06','2011-10-07','2011-10-13','2011-10-19','2011-10-31') 
    box() 
    lab 
    axis(1, at=1:19, lab=F) 
    text(axTicks(1), par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8) 
    axis(2, las=1, at=500*0:g_range[2]) 
    title(main="Runtime", col.main="red", font.main=4) 
    title(xlab="Build", col.lab=rgb(0,0.5,0)) 
    title(ylab="MS", col.lab=rgb(0,0.5,0)) 
    legend(1, g_range[2], c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1); 
    dev.off() 
+1

'las'を使って垂直または水平にすることができます。傾いたラベルが必要な場合は、 'text'を使用して自分で書かなければなりません:http://128.97.141.26/stat/R/faq/angled_labels.htm – James

+0

@James err、OP *は*です。 'text()'呼び出しを 'srt'で参照してください。 –

+0

ちょうどコードをスキャンし、軸の呼び出しを見た。 – James

答えて

6

、私はないはあなたが表示された画像を得ますか。問題は、この行です:

text(axTicks(1), par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8) 

axTicks(1)として返します:

だから、
> axTicks(1) 
[1] 5 10 15 

何が起こっているのか、あなたの19枚のラベルがこれらのの位置にプロットされていることです。

あなたは、その後ティック(1:19)の位置にプロットしたい場合:

text(1:19, par("usr")[3] - 2, srt=45, adj=1, labels=lab, xpd=T, cex=0.8) 

が動作します。

コードに基づいた完全な例です。

Runtime <- c(579,0,581,610,830,828,592,651,596,596,591,581,587, 
      594,604,606,447,434,445) 
g_range <- range(0,Runtime) 
lab <- c('2011-07-20','2011-08-03','2011-08-10','2011-08-17','2011-08-24', 
     '2011-08-25','2011-08-27','2011-08-31','2011-09-07','2011-09-10', 
     '2011-09-14','2011-09-21','2011-09-28','2011-10-05','2011-10-06', 
     '2011-10-07','2011-10-13','2011-10-19','2011-10-31') 
## plot 
op <- par(mar = c(6,4,4,2) + 0.1) ## bigger bottom margin 
plot(Runtime, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE) 
box() 
axis(1, at=1:19, lab=FALSE) 
text(1:19, par("usr")[3] - 40, srt=45, adj=1.2, labels=lab, xpd=T, cex=0.7) 
axis(2, las=1, at=500*0:g_range[2]) 
title(main="Runtime", col.main="red", font.main=4) 
title(xlab="Build", col.lab=rgb(0,0.5,0), line = 4.5) 
title(ylab="MS", col.lab=rgb(0,0.5,0)) 
legend("topright", c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1) 
## reset par 
par(op) 

これは良好グリッドグラフィックが混在するとベースを可能にする、しかしgridBaseパッケージにおける機能を使用して処理されるかもしれません。私がそれが良いかもしれない理由は、プロットされたデータに関してyの適切な値を計算しようとするのではなく、yの座標を線の数として設定することができるということです。これは、画面上のビットうるさいことができ、私のR 2.13.2にgridBase例を任意のラベルを作成しないインストール再実行

## load gridBase 
require(gridBase) 

## do the base plot parts 
op <- par(mar = c(6,4,4,2) + 0.1) ## bigger bottom margin 
plot(1:19, Runtime, type="o", col="blue", ylim=g_range, axes=FALSE, ann=FALSE) 
box() 
axis(1, at=1:19, lab=FALSE) 
axis(2, las=1, at=500*0:g_range[2]) 
title(main="Runtime", col.main="red", font.main=4) 
title(xlab="Build", col.lab=rgb(0,0.5,0), line = 4.5) 
title(ylab="MS", col.lab=rgb(0,0.5,0)) 
legend("topright", c("AveElapsedTime"), cex=0.8, col=c("blue"), pch=21, lty=1) 
## at this point, DO NOT alter the dimensions of the plotting window 

## now do the grid business 
vps <- baseViewports() 
pushViewport(vps$inner, vps$figure, vps$plot) 
## this adds the text 
grid.text(lab, x = unit(1:19, "native"), y = unit(-1, "lines"), 
      just = "right", rot = 60, gp = gpar(cex = 0.7)) 
## this finishes off the viewport - you have to do this or things will go wrong: 
popViewport(3) 
## reset par 
par(op) 

注:ここでは

は一例です。デバイスを閉じてから、コードを再実行すると動作します。あなたがpdf()デバイスに描画している場合、これは問題ではないと思います。

+1

凡例の '' topright ''の+1。私は '文章'でも 'adj = 1.2'が好きです。もし私がさらに微調整していたら、xラベルをもっと遠くに(目盛ラベルで覆われて)移動して、マージンを広げて補色を作ります。 –

+0

@BenBolker良い点。 ** gridBase **を使用してこれを行うより良い方法があることを思い出したので、私は 'y'座標で手を出すことを断念しました。もう1つの例は、ユーザーの座標を踏まずにこれを使って示しています。 –

+0

Gavinさんありがとうございました..素晴らしいことに働いています..あなたのコメントのためにBen、Jamesに感謝します。 – Tamilan

関連する問題