R ggvis multiple plots from one data frame

I was looking, so please forgive me if I missed something.

Suppose a data frame contains a name, a date, calories, where calories are the total number of calories a person consumes on that day.

Name Date Calories Amy 1/1/01 1500 Amy 1/2/01 1600 Amy ... ... Sue 1/1/01 1450 Sue 1/1/02 1500 Sue ... ... Jim ... ... 

What I would like to do is use ggvis to calculate calories for each person (Name). I know that I can use dplyr group_by and get it on one plot, but it will be too busy. And I know that I can use the dplyr filter and filter each person and schedule for each person, but it does not scale.

Is there a way for ggvis to automatically dig out a daily calorie schedule for each person?

Please note that I tried to create a function like the one below:

 makeCharts <- function(myName){ myTbl %>% filter(Name == myName) %>% ggvis(~Date, ~Calories) } 

It works great when you call it manually:

 makeCharts("Amy") 

But when you call it via sapply:

 sapply(levels(myTbl$Name), makeCharts) 

The result is as follows:

  Amy Sue Jim John Sally Frank Sandy etc... marks List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 data List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 props List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 List,1 reactives List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 scales List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 axes List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 legends List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 controls List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 connectors List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 handlers List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 options List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 List,0 cur_data ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cur_props List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 List,2 cur_vis NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 
+4
source share
1 answer

One option is to use do . The dplyr do function can be used for functions that return objects like this, and makes it easy to create a separate graph for each level of some grouping variable.

For your main example, you just need to edit your function with the desired graph, but with the data set as the only argument.

 makeCharts = function(dat) { dat %>% ggvis(~Date, ~Calories) } 

Then group your data set and create a graph for each group via do . . refers to a dataset for each group. I named an output column containing a list of plots .

 allplots = myTbl %>% group_by(Name) %>% do(plots = makeCharts(.)) 

The resulting object is as follows:

 Source: local data frame [2 x 2] Groups: <by row> Name plots 1 Amy <S3:ggvis> 2 Sue <S3:ggvis> 

To print all the charts in a chart window in a row, you just need to run allplots$plots . You can extract only one chart, for example, allplots$plots[[1]] .

Edit

You can use any of the dataset in do by accessing the dataset via . . For example, if you want to add a header with the name of the group, for example this answer , how to do it, you can make a new function to enable this with the second argument:

 makeCharts2 = function(dat, group) { dat %>% ggvis(~Date, ~Calories) %>% add_axis("x", title = "Date") %>% add_axis("x", orient = "top", ticks = 0, title = paste("Plot for", group), properties = axis_props( axis = list(stroke = "white"), labels = list(fontSize = 0))) } 

Then just add the group name as the second argument to the function. One way to do this is through unique .

 allplots2 = myTbl %>% group_by(Name) %>% do(plots = makeCharts2(., unique(.$Name))) 
+4
source

All Articles