Plots for more than one series with "sparkTable" package

1.6k Views Asked by At

If I understand it correctly, the sparkTable package allows for multiple kinds of plots, but only on one series. So, for example, if my dataset df looks like this:

variable  value   time
Level_1   34  1947
Level_1   38  1948
Level_1   17  1949
Level_1   61  1950
Level_1   19  1951
Level_1   80  1952
Level_1   57  1953
Level_1   66  1954

i.e. the variable "value" changes over "time" across the levels of "variable", then I can draw, for instance, sparklines and bargraphs of "value" for different levels of "variable" with the following code:

library(sparkTable)
content<-list()
content[['LinePlot']]<-newSparkLine()
content[['BarPlot']]<-newSparkBar()

varType<-rep("value",2)
df<-df[,c("variable","value","time")]
df$time<-as.numeric(as.character(df$time))
dat<-reshapeExt(df,idvar="variable",varying=list(2))
sparkTab<-newSparkTable(dat,content,varType)
plotSparkTable ( sparkTab , outputType = "html", filename = "t1")

But is there any way to graph more than one series in the same output? For example, let's say I want to have one sparkline for "value", and another for cumulative of the "value" series over time (calculated by Cumulative_Value = ave(df$value, df$variable, FUN=cumsum))

1

There are 1 best solutions below

1
On BEST ANSWER

Do you mean adding extra rows to the resulting sparkTable?

EDIT: OP wants adding extra columns, not rows.

Adding extra columns

To add extra columns you just have to update df, content and varType to include the cumulative values. Add the following into your code:

# with the other lines defining content:
content[['Cumulative']] <- newSparkLine()

# add the following to your df
df$cumulative = ave(df$value, df$variable, FUN=cumsum)

# add the following to your varType definition
varType <- c('value','value','cumulative')

The rest can stay the same.

The first line adds another spark line column to your table, the second calculates the cumulative column and adds it to your data frame, and the third tells newSparkTable that the first two plots are for the value column and the last for the cumulative column.

enter image description here

Adding extra rows

The only way I know (and it's not very nice) is to add extra rows to your df, each corresponding to the cumulative value.

For example:

# make dummy data table with Levels 1 2 3,
#  years 1947:1966 for each, and
#  values being random from 1 to 100.
years <- 1947:1966
n     <- length(years)
df    <- data.frame( variable=sprintf('Level_%i',rep(1:3,each=n)), value=sample(100,3*n,replace=T), time=years )

# as before (setting up spark table)
library(sparkTable)
content<-list()
content[['LinePlot']]<-newSparkLine()
content[['BarPlot']]<-newSparkBar()

# ** calculate cumulative value, and APPEND to the dataframe
# There is a different cumulative line for *each* level.
# Hence we need to make new factors 
#  Level_1_cumulative, Level_2_cumulative, Level_3_cumulative
cv  <- ave(df$value, df$variable, FUN=cumsum)
df2 <- rbind(df, data.frame( variable=sprintf('Level_%i_cumulative',rep(1:3,each=n)), value=cv, time=years ))

# as before (make sparktable, but use df2 this time)
dat<-reshapeExt(df2,idvar="variable",varying=list(2))
varType<-rep("value",2)
sparkTab<-newSparkTable(dat,content,varType)
plotSparkTable ( sparkTab , outputType = "html", filename = "t1")

I end up with something like this: enter image description here