我已经搜索过了,如果我遗失了什么,请原谅我。
我们假设一个数据框包含名称,日期,卡路里,其中卡路里是当天消耗的卡路里总数。
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
答案 0 :(得分:5)
一种选择是使用do
。 dplyr 中的do
函数可用于返回此类对象的函数,并允许您轻松为某些分组变量的每个级别创建单独的图。
对于您的基本示例,您只需要使用所需的绘图编辑您的函数,但将数据集作为唯一参数。
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)))