1

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")
lokheart
  • 23,743
  • 39
  • 98
  • 169
frankc
  • 11,290
  • 4
  • 32
  • 49
  • can you give a reproducible example? – Abe May 21 '12 at 23:48
  • I'm not really sure what kind of example would be helpful. It's not so much that I can't get histograms to work, it's more that they don't obviously exist. I'm hoping someone knows some incantation of sparkTable that would turn a bar plot into a histogram. sparkTable is somewhat sparsely documented. – frankc May 22 '12 at 00:52
  • A helpful example would be one that illustrates your problem, shows what you have tried, and where you have gotten stuck. If you provide an example, it will be easier for to answer this question and check the answer. Yes, it is possible to do a sparkHist, but this will require creating a new function. If you provided an example dataset and how you use it to create a sparkBar, it would be easier to answer this question. – Abe May 22 '12 at 00:59
  • I think that's a rather unnecessarily snarky reply. An example here is fairly pointless because my question is generic, not specific. You could take any of the example data sets that come with sparkTable to which a sparkBar is applied in the documentation. Here is an example: library(sparkTable). data(pop). – frankc May 22 '12 at 01:18
  • are you just asking if there is a sparkHist function? this is not clear. And there is not a sparkBar function. Also, your example does not do anything, and the [documentation](http://cran.r-project.org/web/packages/sparkTable/sparkTable.pdf) has no examples using sparkBar. If I am snarky, it is because I am frustrated. There is a function `newSparkBar`, but it is not clear how this function relates to your question. The package is sufficiently complex that, without an example, only someone familiar with the package would be able to help. – Abe May 22 '12 at 01:25
  • also, is it necessary for the answer to use the sparkTable package? If so, why, if not, what are you looking for? – Abe May 22 '12 at 01:34
  • @frankc: Abe is expression legitimate frustration at not being told the package from which you are taking sparkBar, and the lack of any examples. – IRTFM May 22 '12 at 01:44
  • I thought it was obvious that the package was sparkTable but perhaps only obvious to me since I have been using it. That is a fair point. However, given that, I don't think an example is that useful since I am seeking if this generic functionality can be gotten from this package. The particular dataset isn't really relevant. There is some invocation or hack or there isn't. – frankc May 22 '12 at 03:35
  • @frankc: the point of including an example is to lower the bar/make it easier for potential answerers to start from your example rather than making up their own. It also avoids wasted time in case there turns out to be something non-obvious about the kind of problem you want to be able to do ... (as in "thanks for the answer, but actually that's not quite like the use case I had in mind (but failed to specify carefully enough the first time round)") – Ben Bolker May 24 '12 at 21:03

2 Answers2

2

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")
0

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

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453