[英]How to plot monthly and daily data in same canvas
I have a dataset with over 5,000 records here https://login.filesanywhere.com/fs/v.aspx?v=8c6b678858616ea970a0我在这里有一个包含 5,000 多条记录的数据集https://login.filesanywhere.com/fs/v.aspx?v=8c6b678858616ea970a0
My last column (sal) is monthly data and my other columns are daily data.我的最后一列 (sal) 是每月数据,而我的其他列是每日数据。 I have tried many ways to plot it together in the same r device but my 'sal' column prints some weird lines.我尝试了很多方法在同一个 r 设备中将它绘制在一起,但我的“sal”列打印了一些奇怪的线条。 My other graphics are plotted correctly.我的其他图形绘制正确。 Could some one give me a hint on how I can summarize 'sal' to be only one value per month to plot it on my bottom graphic using facet_wrap?有人可以给我一个提示,告诉我如何将“sal”总结为每月只有一个值,以使用 facet_wrap 在我的底部图形上绘制它? Here is some code that I have tried:这是我尝试过的一些代码:
library(tidyverse)
library(readxl)
library(lubridate)
tab <- read_excel("myjoinedfile.xlsx", sheet="Sheet1")
test <- tab %>% pivot_longer(-c("year","day","date","month")) %>% data.frame()
ggplot(test, aes(x = date, y = value,colour=year)) +
geom_line(size=0.75) + t
facet_wrap(name~.,switch = "y",ncol=1) +
scale_x_date(breaks = seq(as.Date("1991-10-01"),as.Date("1992-09-30"),
by = "1 month"),date_labels = "%b")
All my graphics print correctly but the bottom one is not displaying correctly because is so many repeated values on column 'sal'.我所有的图形都打印正确,但底部的图形显示不正确,因为“sal”列上的重复值太多。 I unsuccessfully tried to get the mean with pivot_longer() Thanks beforehand.我没有成功尝试使用 pivot_longer() 求平均值 事先谢谢。
This could be achieved like so:这可以像这样实现:
sal
by month
and year
, add the name
and the date
. month
和year
聚合列sal
,添加name
和date
。 Here I set the date to the first of the month.在这里,我将日期设置为每月的第一天。 But you could change that.但你可以改变它。sal
将您的数据转换为 long 除了列sal
library(tidyverse)
library(readxl)
library(lubridate)
tab <- read_excel("myjoinedfile.xlsx", sheet="Sheet1")
test1 <- select(tab, year, month, sal) %>%
group_by(year, month) %>%
summarise(value = mean(sal), .groups = "drop") %>%
mutate(name = "sal",
date = ifelse(month < 10, 1992, 1991),
date = paste(date, month, "01", sep = "-"),
date = as.Date(date))
test2 <- tab %>%
select(-sal) %>%
pivot_longer(-c("year","day","date","month")) %>%
mutate(date = as.Date(date))
test <- bind_rows(test1, test2)
ggplot(test, aes(x = date, y = value,colour=year)) +
geom_line(size=0.75) +
facet_wrap(name~.,switch = "y",ncol=1) +
scale_x_date(breaks = seq(as.Date("1991-10-01"),as.Date("1992-09-30"),
by = "1 month"),date_labels = "%b")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.