簡體   English   中英

R YaleToolkit:如何更改迷你圖上刻度標簽的字體大小?

[英]R YaleToolkit: How to change the font size of tick labels on the sparklines?

我正在使用此函數來獲得一些使用R的快速簡便的sparklines ,但我似乎無法弄清楚如何更改字體大小以避免y軸刻度標簽的丑陋重疊。 這是我的代碼(請參閱下面的可重現示例):

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

以及由此產生的情節:

在此輸入圖像描述

我似乎能夠完全抑制y軸

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

但我真正想要的只是縮小刻度線上的數字(並在線下添加灰色填充,但看起來我必須在屏幕上繪制多邊形,這可能足以讓一個單獨的問題麻煩...和一個不同的包)。

文檔表明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軸刻度線上有重疊的數字:

在此輸入圖像描述

更新 :使用來自@ geek-on-acid的非常有用的plot代碼和來自Tufte論壇的一些代碼(見下文)我想出了一個替代的YaleToolkit方法,它不使用YaleToolkit包,看起來沒問題......這是一個可重復的例子(實際上只有兩行,但這里注釋為我的教育):

     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論壇上找到了Jason Dieterle更接近Tufte風格的迷你線代碼。 它們看起來比我的好,但代碼一次只做一個情節。 這是原帖:

#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包 ,我在得知stats.stackexchange但還沒有嘗試過。 R-forge有一個用於制作迷你圖的另一個包的條目,但是這個看起來沒有活躍或有用。

好吧, sparklines很大程度上依賴於grid包,所以你需要深入挖掘來搜索那些字體大小參數。 viewpoint可能會讓你在那里。 對於一個簡單(但非常“原始”)的解決方案,只需使用par使用簡單的繪圖選項 - 這樣您就可以非常輕松地操作幾乎所有參數:

 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))

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM