R ggvis从单个数据帧获得多个图 [英] R ggvis multiple plots from single data frame

查看:78
本文介绍了R ggvis从单个数据帧获得多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索过,所以如果我遗漏了一些东西,请原谅我。

I've searched, so please forgive me if I'm missing something.

我们假设一个数据框包含名称,日期,卡路里,其中卡路里是该人当天所消耗的卡路里总数。

Let's assume a dataframe that contains Name, Date, Calories, where Calories is the total number of calories that person consumed 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     ...     ...

我想做什么是使用ggvis绘制每个人的卡路里(名称)。
我知道我可以使用dplyr的group_by,并将其放在一个绘图上,但这太忙了。而且我知道我可以使用dplyr的过滤器并过滤出每个人并为每个人绘制一个图表,但这并不能缩放。

What I'd like to do is to use ggvis to plot calories for each person (Name). I know I can use dplyr's group_by, and get this on a single plot, but that will be too busy. And I know I could use dplyr's filter and filter out each person and make a graph for each person, but that doesn't scale.

有没有办法让ggvis自动为每个人吐出每天的卡路里量?

Is there a way to have ggvis spit out a plot of calories per day for each person automatically?

请注意,我尝试创建如下所示的函数:

Note that I tried creating a function like the below:

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

手动调用时效果很好:

makeCharts("Amy")

但是当您通过sapply调用时:

But when you call it via sapply:

sapply(levels(myTbl$Name), makeCharts)

输出看起来像这样:

           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  


推荐答案

一种选择是使用 do dplyr 中的 do 函数可用于返回此类对象的函数,并允许您轻松地为某些对象的每个级别绘制一个单独的图分组变量。

One option is to use do. The do function from dplyr can be used for functions that return objects like this, and allows you to easily make a separate plot for each level of some grouping variable.

对于您的基本示例,只需要使用所需的图来编辑函数,但将数据集作为唯一参数。

For your basic example, you just need to edit your function with the desired plot but with the dataset as the only argument.

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

然后对数据集进行分组,并通过 do 每组绘制一个图。 指每个组的数据集。我将输出列命名为包含图的列表

Then group your dataset and make a plot per group via do. The . refers to the dataset for each group. I named the output column that contains the list of plots plots.

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

结果对象看起来像这样:

The resulting object looks like this:

Source: local data frame [2 x 2]
Groups: <by row>

  Name      plots
1  Amy <S3:ggvis>
2  Sue <S3:ggvis>

要将所有绘图连续打印到绘图窗口,只需运行 allplots $ plots 。您可以通过例如 allplots $ plots [[1]] 提取单个图。

To print all the plots to the plotting window in a row, you just need to run allplots$plots. You can extract just a single plot via, e.g., allplots$plots[[1]].

编辑

您可以在<$ c $之内使用数据集中的任何内容c>通过引用数据集。。例如,如果您想添加标题为组名的标题,例如此答案说明了操作方法,则可以包含第二个参数的新函数:

You can use anything from the dataset within do by referring to the dataset via .. For example, if you wanted to add a title with the group name like this answer shows how to do, you could make a new function to include this with a 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)))
}

然后只需将组名添加为函数的第二个参数。一种方法是通过 unique

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

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

这篇关于R ggvis从单个数据帧获得多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆