Is it possible to plot histograms with sparkTable?

953 Views Asked by At

I can do a sparkBar, but no sparkHist. Is this possible to do?

This is an example of how to create a sparkBar (from example(newSparkBar):

library(sparkTable)
data(pop)
x <- pop[pop[,2]=="Insgesamt",3]
b <- newSparkBar(values=x-min(x))
getParameter(b, type="values")
b <- setParameter(b, c("darkred", "darkgreen","black"), type="barCol")
plotSparks(b, outputType="pdf", filename="testBar1")
2

There are 2 best solutions below

0
On

Is something like this what you had in mind? (It could easily be wrapped into a newSparkHist functon, but I haven't bothered ...)

library(sparkTable)
data(alcohol)
hvals <- hist(alcohol$value,plot=FALSE)$counts
b <- newSparkBar(values=hvals,barCol=c("gray","gray","black"))
plotSparks(b, outputType="png", filename="testBar1")

enter image description here

0
On

After reading the comments here, I have included histograms in the version 0.9.4 of the sparkTable package. Basically in the same style as the previous Answer suggested.

Here are two examples:

1) Only for plotting a sparkHist (not very exciting)

2) For creating a sparkTable with boxplots and histograms for normal and lognormal distributed data

#Example newSparkHist
hh <- newSparkHist(values=rnorm(100))
plotSparks(hh, outputType='pdf', filename='testHist1')


#Example sparkTable with Hist+Box with 2 variables in 10 different groups
datEx <- data.frame(variable=sample(paste("Cat",1:10,sep="_"),1000,replace=TRUE),
  value=rnorm(1000),value2=rlnorm(1000))
b <- newSparkBox()
h <- newSparkHist()
content <- list(function(x) { round(mean(x),2) },
  function(x) { round(median(x),2) },
  function(x) { round(quantile(x,.25),2) },
  function(x) { round(quantile(x,.75),2) },
  b,
  h,
  function(x) { round(mean(x),2) },
  function(x) { round(median(x),2) },
  function(x) { round(quantile(x,.25),2) },
  function(x) { round(quantile(x,.75),2) },
  b,
  h
  )
names(content) <- c(paste(c("Mean","Median","Q25","Q75","Boxplot","Histogram"),
  "_v1",sep=""),
  paste(c("Mean","Median","Q25","Q75","Boxplot","Histogram"),"_v2",sep="")
)
varType <- c(rep("value",length(content)/2),rep("value2",length(content)/2))
datEx <- reshapeExt(datEx,idvar="variable", varying=list(2,3))
x2 <- newSparkTable(datEx, content, varType)
plotSparkTable(x2, outputType="html", graphNames="o2",filename="t1")