ggplot2中的过滤和计算平均值

发布于 2025-02-12 09:55:43 字数 753 浏览 4 评论 0原文

我正在使用一个大型DF,试图通过不同感兴趣的属性来制作Filterig数据的一些图。假设我的df看起来像:

df(site = c(a,b,c,d,e),subsite = c(w,x,y,z),date = c(01/01/1985,05/01 /1985,1995,1995,24/03/1995),物种= C(1,2,3,4),年份= C(1990,1990,1995,2012),朱利安日= C(1,2,3,4),月份= C(6,7,8,11)

。到目前为止,每年都有一个物种。 !

Plot1<- df %>% 
  filter(Site=="A", Year>1985, Species =="2")%>%
  group_by(Month) %>% 
  mutate("Day" = mean(julian day)) %>% 
  ggplot(aes(x=Year, y=Day, color=Species)) +
  geom_boxplot() +
  stat_summary(fun=mean, geom="point", 
               shape=1, size=1, show.legend=FALSE) + 
  stat_summary(fun=mean, colour="red", geom="text", show.legend = FALSE, 
               vjust=-0.7,size=3, aes(label=round(..y.., digits=0))) 

谢谢!

I'm working with a large df trying to make some plots by filterig data through different attributes of interest. Let's say my df looks like:

df(site=c(A,B,C,D,E), subsite=c(w,x,y,z), date=c(01/01/1985, 05/01/1985, 16/03/1995, 24/03/1995), species=c(1,2,3,4), Year=c(1985,1990,1995,2012), julian day=c(1,2,3,4), Month=c(6,7,8,11).

I would like plot the average julian day per month each year in which a species was present in a Subsite and Site. So far I've got this code but the average has been calculated for each month over all the years in my df rather than per year. Any help/ directions would be welcome!

Plot1<- df %>% 
  filter(Site=="A", Year>1985, Species =="2")%>%
  group_by(Month) %>% 
  mutate("Day" = mean(julian day)) %>% 
  ggplot(aes(x=Year, y=Day, color=Species)) +
  geom_boxplot() +
  stat_summary(fun=mean, geom="point", 
               shape=1, size=1, show.legend=FALSE) + 
  stat_summary(fun=mean, colour="red", geom="text", show.legend = FALSE, 
               vjust=-0.7,size=3, aes(label=round(..y.., digits=0))) 

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2025-02-19 09:55:43

我想我发现了错误。

我错过了这个:

group_by(Month, **Year**) %>%

I think I spotted the error.

I was missing this:

group_by(Month, **Year**) %>%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文