我使用这个函数来快速简单地使用R sparklines
,但我似乎无法确定如何更改字体大小以避免y轴刻度标签的丑陋重叠。这里是我的代码(请参阅下面的重复的例子):R YaleToolkit:如何更改火花线上刻度线标签的字体大小?
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
图形的任何专家?也欢迎提供更好质量的sparkline式输出的完全不同方法(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轴刻度线与丑陋的重叠号码所产生的示例数据剧情:
UPDATE:使用非常有用有用plot
来自@ geek-on-acid的代码和来自T ufte论坛(见下文)我想出了一种替代的sparkline方法,它不使用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轴的技巧。
最后,完全我包括迷你一些代码由Jason迪特勒我在Tufte forum发现接近Tufte's style。它们看起来比我的好,但代码一次只能做一个阴谋。以下是原帖:
#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 packagestats.stackexchange但还没有尝试过。 R-forge有一个another package的条目来制作迷你图,但是这个看起来并没有活跃或有用。
这@Ben很好的调整 - 好完成。您应该可能将其全部作为单独的答案发布,而不是编辑。要在底部添加一个X轴,只需在'for'循环后面加上'axis(1,pos = c(-4))'即可。 –
@Geek On酸,再次感谢!我错过了原来答案中的最后一行。我现在添加了它。 – Ben