首页 > 解决方案 > 将 2 个不同年份的数据框组合成一个图表

问题描述

我正在尝试将 3 个单独的数据框组合在一起(2018 年、2019 年和 2020 年文件)以制作一个从 1 月到 12 月的时间序列图,但到目前为止我只能制作一个长图,从 2018 年 1 月到 2020 年 6 月。我想看看折线图,每条线代表一年。下面粘贴了每 3 年数据的预览。从下面很难分辨,但月、日和小时分别位于单独的列中。

我尝试过使用 rbind、merge 和其他几个函数,但不能完全理解。谢谢你的帮助!

2020 DATA
Site        Parameter           Date (LT)       Year    MonthDayHourRaw Conc.
Kathmandu   PM2.5 - Principal   1/1/2020 1:00   2020    1   1   1   171
Kathmandu   PM2.5 - Principal   1/1/2020 2:00   2020    1   1   2   161
Kathmandu   PM2.5 - Principal   1/1/2020 3:00   2020    1   1   3   121
Kathmandu   PM2.5 - Principal   1/1/2020 4:00   2020    1   1   4   119
Kathmandu   PM2.5 - Principal   1/1/2020 5:00   2020    1   1   5   137

2019 DATA
Site    Parameter               Date (LT)       Year    MonthDayHourRaw Conc.
Kathmandu   PM2.5 - Principal   1/1/2019 1:00   2020    1   1   1   200
Kathmandu   PM2.5 - Principal   1/1/2019 2:00   2020    1   1   2   185
Kathmandu   PM2.5 - Principal   1/1/2019 3:00   2020    1   1   3   180
Kathmandu   PM2.5 - Principal   1/1/2019 4:00   2020    1   1   4   190
Kathmandu   PM2.5 - Principal   1/1/2019 5:00   2020    1   1   5   200


2018 DATA
Site        Parameter           Date (LT)       Year    MonthDayHourRaw Conc.
Kathmandu   PM2.5 - Principal   1/1/2019 1:00   2020    1   1   1   250
Kathmandu   PM2.5 - Principal   1/1/2019 2:00   2020    1   1   2   215
Kathmandu   PM2.5 - Principal   1/1/2019 3:00   2020    1   1   3   270
Kathmandu   PM2.5 - Principal   1/1/2019 4:00   2020    1   1   4   221
Kathmandu   PM2.5 - Principal   1/1/2019 5:00   2020    1   1   5   250

标签: rggplot2dplyrtimeserieschart

解决方案


如果您能够以易于复制的格式(例如使用dput(data.frame). 无论如何,我已经通过创建以下单独的数据框、 、 和 来近似复制您的df_2018数据df_2019df_2020

df_2018 <- data.frame(Raw.Conc=c(171,161,121,119,137), Date=c('1/5/2018','2/12/2018','3/1/2018','4/4/2018','5/2/2018'))
df_2019 <- data.frame(Raw.Conc=c(200,185,180,190,200), Date=c('1/2/2019','2/12/2019','3/3/2019','4/1/2019','5/6/2019'))
df_2020 <- data.frame(Raw.Conc=c(250,215,270,221,250), Date=c('1/1/2020','2/5/2020','3/4/2020','4/7/2020','5/5/2020'))

数据准备

每个数据框包含两列:一列是数字向量,$Raw.Conc,另一列现在是因子,$Date。第一步是我们应该将数据组合成一个。至关重要的是,我们需要维护有关每个数据集的来源的信息,我可以首先通过创建一个名为 for each 的新列来做到这一点,但是在将列转换为类$origin之后将数据分门别类会更直接$Date使用Datelubridate软件包并特别提取每个日期的年份。请看下面的代码:

# merging datasets
df <- rbind(df_2018, df_2019, df_2020)

# format $Date field
df$Date <- as.Date(df$Date, format='%m/%d/%Y')

# create new column for year
df$Year <- as.character(year(df$Date))

请注意,我正在使用year()函数 fromlubridate来创建df$Year. 的输出year()是数字,因此我将其转换为字符向量,以确保该特定列被视为离散的而不是连续的,以便稍后用于我们的目的。

阴谋

这足以创建绘图,但请记住,这df$Date表示不同的年份,因此线条不会在 x 轴上与同一空间重叠,而是分开。df$Date当我们绘制为 x 轴、df$Raw.Concy 轴并df$Year用作color=美学时观察输出geom_line()

ggplot(df, aes(Date, Raw.Conc)) + geom_line(aes(color=Year))

在此处输入图像描述

据我了解,这不是你想要的。我们实际上需要删除日期并仅绘制月/日。可能有一种优雅的方式来做到这一点......但是在这里我将破解一个基本上将所有日期放在同一年的解决方案,这样它们就可以像在同一年一样被绘制(即你想要的行为)。注意下面的代码:

# create character vector of "mm-dd"
df$Date.axis <- paste0(month(df$Date),'-',day(df$Date))
# turn that into a class(Date)
df$Date.axis <- as.Date(df$Date.axis, format='%m-%d')

如果您检查df$Date.axis,您会看到它包括所有这些年(2020 年),但它适用于我们的目的。我们现在可以绘图,但使用它scale_x_date()来控制 x 轴的标签,以便我们只包括月份(并忽略年份)。

ggplot(df, aes(x=Date.axis, y=Raw.Conc)) + geom_line(aes(color=Year)) +
  scale_x_date(date_labels = '%b')

在此处输入图像描述

更改美学和标签以获得您想要的图表,但这是一种适用于您想要做的通用方法。


推荐阅读