gpt4 book ai didi

r - 合并数据帧以使用 plyr 绘制多个时间序列

转载 作者:行者123 更新时间:2023-12-05 08:37:18 26 4
gpt4 key购买 nike

我想在一个图上绘制多个时间序列。目前我可以单独绘制它们但不能一起绘制。我怎样才能加入数据,因为年份是按小数分割的。

我基本上想要结束的是这个 Plotting multiple time-series in ggplot (见第一个答案中的情节)

library(tidyverse)
library(plyr)
theme_set(theme_bw(10))

Sydney1<-read.csv("Sydney1.csv",header=TRUE)
Sydney2<-read.csv("Sydney2.csv",header=TRUE)
Eden<-read.csv("Eden.csv",header=TRUE)
StonyBay<-read.csv("Stonybay.csv",header=TRUE)
LowHead<-read.csv("Lowhead.csv",header=TRUE)
Hobart<-read.csv("Hobart.csv",header=TRUE)
Devonport<-read.csv("Devonport.csv",header=TRUE)
Freemantle<-read.csv("Freemantle.csv",header=TRUE)


ggplot(Sydney1,aes(x=Year,y=SLR))+geom_line(aes(color="Sydney1"))
ggplot(Sydney2,aes(x=Year,y=SLR))+geom_line(aes(color="Sydney2"))
ggplot(Eden,aes(x=Year,y=SLR))+geom_line(aes(color="Eden"))
ggplot(StonyBay,aes(x=Year,y=SLR))+geom_line(aes(color="StonyBay"))
ggplot(LowHead,aes(x=Year,y=SLR))+geom_line(aes(color="Lowhead"))
ggplot(Hobart,aes(x=Year,y=SLR))+geom_line(aes(color="Hobart"))
ggplot(Devonport,aes(x=Year,y=SLR))+geom_line(aes(color="Devonport"))
ggplot(Freemantle,aes(x=Year,y=SLR))+geom_line(aes(color="Freemantle"))


#Sydney 1

structure(list(Year = c(1886.0417, 1886.125, 1886.2083, 1886.2917,
1886.375, 1886.4583), SLR = c(6819L, 6942L, 6980L, 6958L, 7015L,
6892L)), row.names = c(NA, 6L), class = "data.frame")

#Sydney 2

structure(list(Year = c(1914.4583, 1914.5417, 1914.625, 1914.7083,
1914.7917, 1914.875), SLR = c(7022L, 6963L, 6915L, 6924L, 6866L,
6956L)), row.names = c(NA, 6L), class = "data.frame")

#Eden
structure(list(Year = c(1986.7917, 1986.875, 1986.9583, 1987.0417,
1987.125, 1987.2083), SLR = c(7003L, 6942L, 6969L, 7067L, NA,
7015L)), row.names = c(NA, 6L), class = "data.frame")

#Stony Bay

structure(list(Year = c(1993.0417, 1993.125, 1993.2083, 1993.2917,
1993.375, 1993.4583), SLR = c(6826L, 6868L, 6796L, 6862L, 6893L,
6951L)), row.names = c(NA, 6L), class = "data.frame")

#Low head

structure(list(Year = c(2010.125, 2010.2083, 2010.2917, 2010.375,
2010.4583, 2010.5417), SLR = c(6971L, 6968L, 7030L, 7088L, 7063L,
7035L)), row.names = c(NA, 6L), class = "data.frame")

#Hobart

structure(list(Year = c(1987.875, 1987.9583, 1988.0417, 1988.125,
1988.2083, 1988.2917), SLR = c(6916L, 6870L, 6930L, 6870L, 6820L,
6817L)), row.names = c(NA, 6L), class = "data.frame")

#Devonport

structure(list(Year = c(1989.875, 1989.9583, 1990.0417, 1990.125,
1990.2083, 1990.2917), SLR = c(6976L, 7025L, 7030L, 7046L, 6999L,
7055L)), row.names = c(NA, 6L), class = "data.frame")

#Freemantle
structure(list(Year = c(1897.0417, 1897.125, 1897.2083, 1897.2917,
1897.375, 1897.4583), SLR = c(6542L, 6524L, 6557L, 6655L, 6648L,
6729L)), row.names = c(NA, 6L), class = "data.frame")

最佳答案

使用末尾注释中的数据,首先将序列累积到列表 L 中——我们假设任何具有列名 YearSLR 的数据框是添加 - 然后将其转换为单个动物园对象并使用使用 ggplot2 的 autoplot.zoo 绘制它。如果您希望将它们绘制在单独的方面,请删除 facet = NULL 参数。

library(ggplot2)
library(zoo)

is_city_df <- function(x) is.data.frame(x) && identical(names(x), c("Year", "SLR"))
L <- Filter(is_city_df, mget(ls()))
z <- do.call("merge", lapply(L, read.zoo))
autoplot(z, facet = NULL)

screenshot

注意事项

我们假设以下输入:

Sydney1 <- 
structure(list(Year = c(1886.0417, 1886.125, 1886.2083, 1886.2917,
1886.375, 1886.4583), SLR = c(6819L, 6942L, 6980L, 6958L, 7015L,
6892L)), row.names = c(NA, 6L), class = "data.frame")

Sydney2 <-
structure(list(Year = c(1914.4583, 1914.5417, 1914.625, 1914.7083,
1914.7917, 1914.875), SLR = c(7022L, 6963L, 6915L, 6924L, 6866L,
6956L)), row.names = c(NA, 6L), class = "data.frame")

Eden <-
structure(list(Year = c(1986.7917, 1986.875, 1986.9583, 1987.0417,
1987.125, 1987.2083), SLR = c(7003L, 6942L, 6969L, 7067L, NA,
7015L)), row.names = c(NA, 6L), class = "data.frame")

Freemantle <-
structure(list(Year = c(1897.0417, 1897.125, 1897.2083, 1897.2917,
1897.375, 1897.4583), SLR = c(6542L, 6524L, 6557L, 6655L, 6648L,
6729L)), row.names = c(NA, 6L), class = "data.frame")

关于r - 合并数据帧以使用 plyr 绘制多个时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65815103/

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