2011-12-01 13 views
14

私はいくつかの迅速かつ簡単なsparklines Rでこの機能を使用していますが、y軸目盛ラベルの醜いオーバーラップを避けるために、フォントサイズを変更する方法を考えることができません。ここに私のコードは、(再現例えば、以下を参照)は次のとおりR YaleToolkit:スパークラインの目盛りラベルのフォントサイズを変更する方法は?

sparklines(gamma.df, sub=c(1:23),outer.margin = unit(c(2, 2, 2, 2), "cm"))

、得られたプロット:

enter image description here

Iが完全

とy軸を抑制することができるように思われます

sparklines(gamma.df, sub=c(1:23),yaxis=FALSE,outer.margin = unit(c(2, 2, 2, 2), "cm"))

しかし、私が本当に望むのは、 (そして、線の下に灰色の塗りつぶしを追加しますが、画面にポリゴンを描画しなければならないように見えます。別の質問に値するのはおそらく問題です...そして別のパッケージ)。

documentationgparは、関連するかもしれないことを示唆している:「グラフィックスパラメータのリストが必要とされているすべての場合において、有効なパラメータ名は、適切な呼び出しでgparに渡された時に有効になるのと同じです。」しかし、私の試みがどこにも出てこないような感覚を持たせるためには、ちょっとした助けが必要です(cex.axisとaxis()と同上)。 Rのgridのグラフィックスの専門家はいますか?より良い品質のスパークラインスタイルの出力を提供する完全に異なるアプローチ(ggplot2?)へのリンクも歓迎します。ここで

は私の問題を複製いくつかの例のデータとコードです:

x <- data.frame(V = rnorm(1000), W = rnorm(1000), X = rnorm(1000), Y = rnorm(1000), Z = rnorm(10)) 
sparklines(x,outer.margin = unit(c(2, 2, 2, 2), "cm")) 

そしてここでは、y軸の目盛りで醜い重複番号の結果の一例のデータプロットです:

enter image description here

更新非常に有用なplotコードからの@ geek-on-acidおよびTからのいくつかのコードufteフォーラム(下記参照)YaleToolkitパッケージを使用しない代替のスパークラインメソッドを思いついたと思います...再現可能な例(実際には2行ですが、私の教育のためにここに注釈が付けられています):

 x <- data.frame(V = rnorm(1000), W = rnorm(1000), X = rnorm(1000), Y = rnorm(1000), 
    Z = rnorm(1000)) # get a bit of data 

    par(mfrow=c(ncol(x),1), #sets number of rows in space to number of cols in data frame x 
    mar=c(1,0,0,0), #sets margin size for the figures 
    oma=c(4,5,4,4)) #sets outer margin 

    for (i in 1:ncol(x)){ # setup for statement to loops over all elements in a list or vector 
     plot(x[,i], #use col data, not rows from data frame x 
     col="grey",lwd=0.5, #make the line grey and thin 
     axes=F,ylab="",xlab="",main="",type="l"); #suppress axes lines, set as line plot 
     axis(2,yaxp=c(min(x[,i]),max(x[,i]),2), # y-axis: only show tickmarks for max and min values of col 
     cex.axis=1.1,las=1, # shrink fontsize slightly, make text horizontal for easy reading 
     at=c(round(min(x[,i]),3),round(max(x[,i]),3))); #specify where tickmark numbers go and round them to keep it tidy 
     axis(2,yaxp=c(min(x[,i]),max(x[,i]),2),col="white",tcl=0,labels=FALSE) #y-axis: put a 2nd white axis line over the 1st y-axis to make it invisible 
     ymin<-min(x[,i]); tmin<-which.min(x[,i]);ymax<-max(x[,i]);tmax<-which.max(x[,i]); # see the code from Jason below for what these do 
     points(x=c(tmin,tmax),y=c(ymin,ymax),pch=19,col=c("red","blue"),cex=1) # add coloured points at max and min 
     } 
     axis(1,pos=c(-5)) # places horizontal axis at the bottom of it all. 

ここでは、私の問題の解決策である結果の画像です。 y軸の目盛りは、データの最大値と最小値のみを表示し、垂直線を持たないことによってエレガントになります。 @ geek-on-acidにもう一度おねがいします。私はそれらのすべての底に沿って単一のx軸を得る方法についてヒントを得ています。

enter image description here

最後に、完全を期すために私は近いTufte's styleに私はTufte forumで見つけジェイソンDieterleによってスパークラインのためのいくつかのコードが含まれています。彼らは私よりもはるかによく見えますが、コードは一度に1つのプロットしか行いません。ここではオリジナルのポストだ:

#Here is a simple R implementation of sparklines. Running sparkline() will generate a random sparkline; running sparkline(yourdata) will generate a sparkline using the data in yourdata. As an example, here is Google's stock price for the last year. 

#R sparklines 
sparkline<-function(ydata=rnorm(100,500,50),width=1.5,height=0.5,sigfigs=4) { 

# ydata = vector of data to be plotted 
# width = width of sparlkline in inches, including text 
# height = height of sparkline in inches 
# sigfigs = number of significant figures to round min, max, and last values to 

    temppar<-par(no.readonly = TRUE) # store default graphics parameters 
    par(mai=c(0.10,0.05,0.10,0.05),fin=c(width,height)) # adjust graphics parameters for sparklines 
    len<-length(ydata) # determine the length of the data set 
    ymin<-min(ydata) # determine the minimum 
    tmin<-which.min(ydata) # and its index 
    ymax<-max(ydata) # determine the maximum 
    tmax<-which.max(ydata) # and its index 
    yfin<-signif(ydata[len],sigfigs) #determine most recent data point 
    plotrange=c(ymin-0.3*(ymax-ymin),ymax+0.3*(ymax-ymin)) # define plot range to leave enough room for min and max circles and text 
    plot(x=1:len,y=ydata,type="l",xlim=c(1,len*1.5),ylim=plotrange,col="gray",lwd=0.5,ann=FALSE,axes=FALSE) # plot sparkline 
    points(x=c(tmin,tmax),y=c(ymin,ymax),pch=19,col=c("red","blue"),cex=0.5) # plot min and max points 
    text(x=len,y=ymin,labels=signif(ymin,sigfigs),cex=0.5,pos=4,col="red") # show minimum value 
    text(x=len,y=ymax,labels=signif(ymax,sigfigs),cex=0.5,pos=4,col="blue") # show maximum value 
    text(x=len,y=(ymin+ymax)/2,labels=yfin,cex=0.5,pos=4) # show most recent value 
    par(temppar) # restore graphics defaults 
} 
#-- Jason Dieterle (email), January 28, 2008 

、ここでは彼の出力が(少し拡大)のようになります。YaleToolkitパッケージに加えて

enter image description here

、私は時を知っsparkTable packageがありますstats.stackexchangeでも試していません。 R-forgeにはスパークラインを作るのにanother packageのエントリがありますが、これはアクティブでも有効でもないようです。

+1

を - まあ完了しました。おそらく、編集するのではなく別の答えとして投稿したはずです。一番下にX軸を追加するには、 'for'ループの直後に' axis(1、pos = c(-4)) 'を追加してください。 –

+0

@Geek On Acid、もう一度ありがとう!私は元の答えの最後の行を逃した。私は今それを追加しました。 – Ben

答えて

6

まあ、sparklinesはパッケージに大きく依存しているので、それらのフォントサイズのパラメータを検索するには深く掘り下げる必要があります。 viewpointで遊ぶことで、おそらくそこに着くでしょう。 (しかし、非常に「生」)簡単な解決のために、ちょうどparを使用して簡単なプロットオプションを使用 - この方法は、あなたが非常に簡単に、ほぼすべてのパラメータを操作することができます@Ben非常に良い微調整をだ

x <- data.frame(V = rnorm(1000), W = rnorm(1000), X = rnorm(1000), 
     Y = rnorm(1000), Z = rnorm(10)) 
par(mfrow=c(5,1),mar=c(1,0,0,0),oma=c(4,5,4,4)) 
plot(x$V,axes=F,ylab="",xlab="",main="",type="l");axis(2,cex.axis=0.7) 
plot(x$W,axes=F,ylab="",xlab="",main="",type="l");axis(2,cex.axis=0.7) 
plot(x$X,axes=F,ylab="",xlab="",main="",type="l");axis(2,cex.axis=0.7) 
plot(x$Y,axes=F,ylab="",xlab="",main="",type="l");axis(2,cex.axis=0.7) 
plot(x$Z,axes=F,ylab="",xlab="",main="",type="l");axis(2,cex.axis=0.7) 
axis(1,pos=c(-2)) 

enter image description here

+0

ありがとう、それはplot()を使ってスパークラインを複製するための指示を持つと便利です。もし私が私が望むことをするためにスパークライン()を得ることができないなら、これは素晴らしい選択肢です、もう一度ありがとうございます。私はggplot2にもあなたの設定を適応させるのは非常に簡単だと思います。 – Ben

+1

@Ben 'ggplot2'に複製するのにはいくつかの障害があります。これは' grid'にも大きく依存するためですが、 'sparklines'よりも確かに達成可能です。 –

+0

'ggplot2'について知っておいてよかったです。あなたの投稿は、私の質問に答えるためのいくつかの重要な概念を私に与えてくれました。とても役に立ちます。私は、 'plot'で基本的なスパークラインを作るという点で私がどこにいるのかを示すために質問を更新しました。再度、感謝します! – Ben

関連する問題