我正在使用此功能来快速轻松地使用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"))
但我真正想要的只是缩小刻度线处的数字(并在线下添加灰色填充,但看起来我必须在屏幕上绘制多边形,这可能足够麻烦值得单独的问题......和另一个包裹。)
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轴刻度线上有重叠的数字:
更新:使用来自@ 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's style找到了Jason Dieterle靠近Tufte forum的迷你线的代码。它们看起来比我的好,但代码一次只做一个情节。这是原帖:
#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但未尝试过。{3}} R-forge有一个another package的条目可以制作迷你图,但是这个条目似乎没有活跃或有用。
答案 0 :(得分:7)
好吧,sparklines
严重依赖于grid
包,因此您需要深入挖掘以搜索这些字体大小参数。和viewpoint
一起玩可能会让你在那里。对于一个简单(但非常简单的原始&#39;)解决方案,只需使用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))