私はいくつかの迅速かつ簡単なsparklines
Rでこの機能を使用していますが、y軸目盛ラベルの醜いオーバーラップを避けるために、フォントサイズを変更する方法を考えることができません。ここに私のコードは、(再現例えば、以下を参照)は次のとおりR YaleToolkit:スパークラインの目盛りラベルのフォントサイズを変更する方法は?
sparklines(gamma.df, sub=c(1:23),outer.margin = unit(c(2, 2, 2, 2), "cm"))
、得られたプロット:
Iが完全
とy軸を抑制することができるように思われますsparklines(gamma.df, sub=c(1:23),yaxis=FALSE,outer.margin = unit(c(2, 2, 2, 2), "cm"))
しかし、私が本当に望むのは、 (そして、線の下に灰色の塗りつぶしを追加しますが、画面にポリゴンを描画しなければならないように見えます。別の質問に値するのはおそらく問題です...そして別のパッケージ)。
documentationはgparは、関連するかもしれないことを示唆している:「グラフィックスパラメータのリストが必要とされているすべての場合において、有効なパラメータ名は、適切な呼び出しで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軸の目盛りで醜い重複番号の結果の一例のデータプロットです:
更新:非常に有用な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軸を得る方法についてヒントを得ています。
最後に、完全を期すために私は近い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パッケージに加えて
、私は時を知っsparkTable packageがありますstats.stackexchangeでも試していません。 R-forgeにはスパークラインを作るのにanother packageのエントリがありますが、これはアクティブでも有効でもないようです。
を - まあ完了しました。おそらく、編集するのではなく別の答えとして投稿したはずです。一番下にX軸を追加するには、 'for'ループの直後に' axis(1、pos = c(-4)) 'を追加してください。 –
@Geek On Acid、もう一度ありがとう!私は元の答えの最後の行を逃した。私は今それを追加しました。 – Ben