Note that there are some explanatory texts on larger screens.

plurals
  1. POR YaleToolkit: How to change the font size of tick labels on the sparklines?
    primarykey
    data
    text
    <p>I'm using this function for some quick and easy <code>sparklines</code> with R but I can't seem to work out how to change the font size to avoid ugly overlaps of the y-axis tick labels. Here's my code (see below for a reproducible example):</p> <p><code>sparklines(gamma.df, sub=c(1:23),outer.margin = unit(c(2, 2, 2, 2), "cm"))</code></p> <p>and the resulting plot:</p> <p><img src="https://i.stack.imgur.com/hHRmu.png" alt="enter image description here"></p> <p>I seem to be able to completely suppress the y-axis with</p> <p><code>sparklines(gamma.df, sub=c(1:23),yaxis=FALSE,outer.margin = unit(c(2, 2, 2, 2), "cm"))</code></p> <p>But what I really want is just shrink the numbers at the tick marks (and add grey fill under the line, but it looks like I'd have to draw polygons on the screen which is probably enough trouble to deserve a separate question... and a different package).</p> <p>The <a href="http://cran.r-project.org/web/packages/YaleToolkit/YaleToolkit.pdf" rel="nofollow noreferrer">documentation</a> suggests that <a href="http://projetos.inpa.gov.br/i3geo/pacotes/r/win/library/grid/html/gpar.html" rel="nofollow noreferrer">gpar</a> might be relevant: 'In all the cases where a list of graphics parameters is needed, the valid parameter names are the same as would be valid when passed to gpar in the appropriate call.' But I need a bit of help to make sense of that as none of my attempts seem to get anywhere (ditto for cex.axis and axis()). Any experts on R's <code>grid</code> graphics out there? Links to a well-documented completely different approach (ggplot2?) that gives better quality sparkline-style output are welcome also.</p> <p>Here's some example data and code that replicates my problem:</p> <pre><code>x &lt;- 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")) </code></pre> <p>And here's the resulting example data plot with ugly overlapping numbers at the y-axis tickmarks: </p> <p><img src="https://i.stack.imgur.com/1Jale.png" alt="enter image description here"></p> <p><strong>UPDATE</strong>: Using the <em>very</em> helpful <code>plot</code> code from @geek-on-acid and some code from the Tufte forum (see below) I've come up with an alternative sparkline method that doesn't use the <code>YaleToolkit</code> package and looks alright... Here's a reproducible example (really just two lines, but annotated here for my education):</p> <pre><code> x &lt;- 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&lt;-min(x[,i]); tmin&lt;-which.min(x[,i]);ymax&lt;-max(x[,i]);tmax&lt;-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. </code></pre> <p>Here's the resulting image, which is basically the solution to my problem. The y-axis tick marks try to be elegant by showing only the max and min values of the data and having no vertical line. Thanks again to @geek-on-acid for the tip on how to I get a single x-axis along the bottom of them all.</p> <p><img src="https://i.stack.imgur.com/jAQy1.png" alt="enter image description here"></p> <p>Finally, for completeness I include some code for sparklines closer to <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0001OR" rel="nofollow noreferrer">Tufte's style</a> by Jason Dieterle that I found on the <a href="http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=00037p" rel="nofollow noreferrer">Tufte forum</a>. They look much nicer than mine, but the code only does one plot at a time. Here's the original post:</p> <pre><code>#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&lt;-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&lt;-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&lt;-length(ydata) # determine the length of the data set ymin&lt;-min(ydata) # determine the minimum tmin&lt;-which.min(ydata) # and its index ymax&lt;-max(ydata) # determine the maximum tmax&lt;-which.max(ydata) # and its index yfin&lt;-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 </code></pre> <p>and here's what his output looks like (enlarged a little):</p> <p><img src="https://i.stack.imgur.com/bpwKk.png" alt="enter image description here"></p> <p>In addition to the YaleToolkit package, there is the <a href="http://cran.r-project.org/web/packages/sparkTable/index.html" rel="nofollow noreferrer">sparkTable package</a> which I learnt of at <a href="https://stats.stackexchange.com/questions/8242/plotting-sparklines-in-r/">stats.stackexchange</a> but have not tried. R-forge has an entry for <a href="https://r-forge.r-project.org/projects/sparkline/" rel="nofollow noreferrer">another package</a> to make sparklines, but this one appears not to have be active or useful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload