gpt4 book ai didi

r - 将包含行为的事件预算绘制成图表

转载 作者:行者123 更新时间:2023-12-02 22:43:42 25 4
gpt4 key购买 nike

我遇到了一个具有挑战性的问题,希望得到一些建议。我有想要以图形方式显示的事件数据,并且正在寻找可用于解决我的问题的包或程序(最好是 R)。

数据是在 3 周(日历日期)或更长时间内每小时(一天中的时间)收集的运动(Activity)计数相关变量(Food/Vegetation)。

通常,正如我所知,可以在名为 Clocklab 的程序中处理数据并绘制图表,该程序是 Matlab 产品。然而,增加的复杂性是希望根据喂养组的分类绘制此数据。我试图为此在 R 中找到一个公平的程序/包,但没有成功。

数据看起来很简单:

Activity     time of day      Food type         Calendar Date
0 01:00 B 03/24/2007
13 02:00 --- 03/24/2007
0 03:00 B 03/24/2007
0 04:00 B 03/24/2007
: : : :
1246 18:00 C 03/24/2007
3423 19:00 C 03/24/2007
: : : :
0 00:00 --- 03/25/2007

此数据是昼夜节律的、循环的、事件预算的,我想要一个本质上可能是 3-D 的图表,显示饮食选择以及随着时间的推移绘制的与该饮食相关的事件量/周。我会先由个人来做,然后在人口层面上做。我有一个程序链接和程序通常生成的示例图 Clocklab .

最佳答案

没有真实数据,这是我能想到的最好的。不需要特殊的包,只需 ggplot2plyr:

#Some imagined data
dat <- data.frame(time = factor(rep(0:23,times = 20)),
count = sample(200,size = 480,replace = TRUE),
grp = sample(LETTERS[1:3],480,replace = TRUE))

head(dat)
time count grp
1 0 79 A
2 1 19 A
3 2 9 C
4 3 11 A
5 4 123 B
6 5 37 A

dat1 <- ddply(dat,.(time,grp),summarise,tot = sum(count))

> head(dat1)
time grp tot
1 0 A 693
2 0 B 670
3 0 C 461
4 1 A 601
5 1 B 890
6 1 C 580

ggplot(data = dat1,aes(x = time,y = tot,fill = grp)) +
geom_bar(stat = "identity",position = "stack") +
coord_polar()

enter image description here

我只是将一天中的小时编码为整数 0-23,并简单地为 Activity 计数获取了一些随机值。但这似乎通常是您所追求的。

编辑

基于评论的更多选项:

#Force some banding patterns
xx <- sample(10,9,replace = TRUE)
dat <- data.frame(time = factor(rep(0:23,times = 20)),
day = factor(rep(1:20,each = 24),levels = 20:1),
count = rep(c(xx,rep(0,4)),length.out = 20*24),
grp = sample(LETTERS[1:3],480,replace = TRUE))

选项一使用分面:

ggplot(dat,aes(x = time,y = day)) +
facet_wrap(~grp,nrow = 3) +
geom_tile(aes(alpha = count))

enter image description here

选项二使用颜色(即填充):

ggplot(dat,aes(x = time,y = day)) +
geom_tile(aes(alpha = count,fill = grp))

enter image description here

关于r - 将包含行为的事件预算绘制成图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10368221/

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