gpt4 book ai didi

R ggvis 来自单个数据框的多个图

转载 作者:行者123 更新时间:2023-12-04 11:03:58 24 4
gpt4 key购买 nike

我已经搜索过了,所以如果我遗漏了什么,请原谅我。

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

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 的过滤器并过滤掉每个人并为每个人制作一个图表,但这不能缩放。

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

请注意,我尝试创建一个如下所示的函数:
makeCharts <- function(myName){
myTbl %>% filter(Name == myName) %>% ggvis(~Date, ~Calories)
}

手动调用它时效果很好:
makeCharts("Amy")

但是当你通过 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 . do函数来自 dplyr 可用于像这样返回对象的函数,并允许您轻松地为某些分组变量的每个级别绘制单独的图。

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

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

然后将您的数据集分组并通过 do 为每组绘制一个图. .指的是每个组的数据集。我将包含绘图列表的输出列命名为 plots .
allplots = myTbl %>%
group_by(Name) %>%
do(plots = makeCharts(.))

生成的对象如下所示:
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]] 仅提取一个图.

编辑

您可以使用数据集中的任何内容 do通过 . 引用数据集.例如,如果你想添加一个组名的标题,如 this answer展示了如何做,您可以创建一个新函数以将其包含在第二个参数中:
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 .
allplots2 = myTbl %>%
group_by(Name) %>%
do(plots = makeCharts2(., unique(.$Name)))

关于R ggvis 来自单个数据框的多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32058843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com