Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在ggplot2中使用面处理应用after_stat?_R_Ggplot2_Graph_Moving Average - Fatal编程技术网

如何在ggplot2中使用面处理应用after_stat?

如何在ggplot2中使用面处理应用after_stat?,r,ggplot2,graph,moving-average,R,Ggplot2,Graph,Moving Average,我正在使用移动平均法来消除疫苗分发中的一周中的一天影响,以查看按各种因素分层的总体趋势。我可以创建滚动平均值的条形图,以正确显示总体数据。但当我分层或创建刻面时,引入阶段会出现高度递减的“幽灵”条(应该没有条)。我怎样才能避免呢 正确的图形(无分层):g 移动平均线引入期中带有“鬼”条的图形:g+小平面网格(race~,scales=“free\u y”) 我的代码 问题是,在计算统计数据之后,统计数据之后发生的任何计算都不一定要考虑面板。这给zoo::rollmean带来了问题,因为它只看

我正在使用移动平均法来消除疫苗分发中的一周中的一天影响,以查看按各种因素分层的总体趋势。我可以创建滚动平均值的条形图,以正确显示总体数据。但当我分层或创建刻面时,引入阶段会出现高度递减的“幽灵”条(应该没有条)。我怎样才能避免呢

正确的图形(无分层):g

移动平均线引入期中带有“鬼”条的图形:g+小平面网格(race~,scales=“free\u y”)

我的代码
问题是,在计算统计数据之后,统计数据之后发生的任何计算都不一定要考虑面板。这给
zoo::rollmean
带来了问题,因为它只看到一个值向量。因此,您必须按面板循环数据

库(tidyverse)

nPerDay与@teunbrand的方法类似(他对这个问题的简明解释值得赞扬,我没有什么要补充的),但是利用
dplyr
和一个助手函数,你可以达到你想要的结果,如下所示:

库(tidyverse)
种子(42)
#伪造数据:计算70天内每天的剂量,在70天内增加,每周每天变化50%
nPerDay%
拉(滚)
}
#将上一个图中的条形图更改为滚动7天平均值,但将该行保留为每日总计数。
g警告:删除了包含缺失值的30行(位置\u堆栈)。

我会把它作为一个中间环节来做(更容易看到数据并找出错误),然后将其传输到ggplot…谢谢@teunbrand。我的目标是创建一个ggplot对象“g”,它具有
fill=whichDose
,并且我可以向其中添加我想要的任何方面变量。我删除了
doges$race,
订单(doges$race,doges$Admin\u date)
,将
fill=whichDose
添加到
ggplot(…aes())
,并将
split(count,PANEL)
更改为
split(count,list(fill,PANEL)
。效果很好,但
…list(PANEL,fill)
没有-很明显,面板与填充的顺序很重要。我的问题是:除了尝试和错误之外,我如何计算出正确的顺序?面板这个术语记录在哪里,或者是计数、填充和面板的这种用法呢?
面板
变量是一个列,它在内部添加到数据中,以便在面板上分割数据。我在这里使用的方法有点像黑客。正确的方法是在没有这种黑客的情况下进行转换。记录的位在
?stat
之后,你会在
?stat\u bin
“computed variables”部分找到
count
。谢谢@stefan。根据我对@teunbrand的评论,要按填充进行分层,我将将
fill=whichDose
添加到
ggplot(aes())
,并将
.PANEL..
更改为
交互(fill,…PANEL..)
。这很有效,与@teunbrand的
lapply方法不同,(fill,PANEL)顺序似乎并不重要(
交互(…PANEL..,fill)
).很好,但很混乱。我想原因可能在我的脑海里。
library(tidyverse)
# Make fake data: count of doses per day for 70 days, increasing over the 70 days, with a 50% variance per day-of-week
nPerDay <- floor(sample(5:10, 70, replace=T) * (1 + ((1:70)*3/70)) * (.5 + (.5*(1:70 %% 7)/6)))
# Use that to create a data frame where one record is the administration of one dose, giving the dose, vaccine brand, 1st or 2nd dose, pt race, & pt gender
doses <- data.frame(Admin_date = rep(as.Date("2020-12-31") + 1:70, nPerDay)
                    , whichDose = factor(c(rep(1,sum(nPerDay[1:30])), sample(1:2, sum(nPerDay[31:70]), replace=T)))
                    , gender=sample(c("F", "M"), sum(nPerDay), replace=T)
                    , race=sample(LETTERS[1:5], sum(nPerDay), c(.45, .25, .15, .1, .05), replace=T)
                    , brand=sample(c("Pf", "Mo"), sum(nPerDay), replace=T)
)

# plot the doses administered each day, with stacked bars', with bars' color indicating # of 1st or second dose
(ggplot(data=doses, mapping=aes(x=Admin_date))#, fill=whichDose))
  + geom_bar(position = "stack")
  + geom_line(aes(y=..count.., fill=NULL), stat = "bin", binwidth=1)
)

# Change the bars in the prior plot into rolling 7-day averages, but keep the line as a daily total count.
g <- (
  ggplot(data=doses, mapping=aes(x=Admin_date))#, fill=whichDose)) 
  + geom_bar(position = "stack"
             , mapping = aes(y=zoo::rollmean(..count.., 7, align="right", fill=NA))
             , stat="bin", binwidth=1
  )
  + geom_line(aes(y=..count.., fill=NULL), stat = "bin", binwidth=1)
  + labs(y="doses", fill="Which dose,\n7d avg count")
)
g # display this base graph

# explore tha data
g + facet_grid(race~., scales="free_y") # See if the increasing trend and 1st vs 2nd dose porportions or similar across races.
# look at other stratifications
g + facet_grid(gender, scales="free_y")
g + facet_grid(race~brand, scales="free_y")
g + facet_grid(race~gender, scales="free_y")