是否可以使用sparkTable绘制直方图?

时间:2012-05-21 20:27:42

标签: r sparklines tufte sparktable

我可以做一个sparkBar,但没有sparkHist。这可能吗?

这是一个如何创建sparkBar的示例(来自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 个答案:

答案 0 :(得分:2)

在阅读了这里的评论之后,我在sparkTable软件包的0.9.4版中包含了直方图。基本上与之前的答案建议的风格相同。

以下是两个例子:

1)仅用于绘制sparkHist(不是非常令人兴奋)

2)用于创建带有用于正态和对数正态分布式数据的箱线图和直方图的sparkTable

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

答案 1 :(得分:0)

你有什么想法吗? (它很容易被包裹成newSparkHist功能,但我没有打扰......)

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