Charts for more than one series with sparkTable

If I understand correctly, sparkTable allows several types of graphs, but only in one series. So, for example, if my df dataset 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 “value” variable changes in “time” by the “variable” levels, then I can, for example, draw lines and bargraphs of “value” for different levels of the “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 a way to build more than one row on one output? For example, suppose I want to have one spark line for “value” and another for the cumulative series “value” with time (calculated Cumulative_Value = ave(df$value, df$variable, FUN=cumsum) )

+4
source share
1 answer

Do you mean adding extra lines to the resulting sparkTable?

EDIT : OP wants to add extra columns, not rows.

Adding Additional Columns

To add additional columns, you just need to update df , content and varType to include cumulative values. Add the following to 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 may remain unchanged.

The first row adds another spark 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 graphs 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 this is not very nice) is to add extra lines to your df , each of which corresponds to a cumulative value.

For instance:

 # 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 get something like this: enter image description here

+6
source

Source: https://habr.com/ru/post/1387350/


All Articles