我正在使用此功能来快速轻松地sparklines
使用 R,但我似乎无法弄清楚如何更改字体大小以避免 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() 也是如此)。grid
那里有 R图形方面的专家吗?也欢迎链接到一个有据可查的完全不同的方法(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
包并且看起来不错的替代迷你图方法......这是一个可重现的示例(实际上只有两行,但在此处为我的教育进行了注释):
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 有一个用于制作迷你图的包的条目,但这个包似乎没有激活或有用。