2

I want to plot multiple time series on one plot. Currently I can plot them all individually but not together. How can I join the data because the years are split by decimals.

What I basically want to end up with is this Plotting multiple time-series in ggplot (See plot in the first answer)

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")
Sophie Williams
  • 338
  • 1
  • 3
  • 14

4 Answers4

3

Assuming that your global environment only contains the dataframes you want to plot:

bind_rows(mget(ls()), .id = "City") %>% 
ggplot(aes(x = Year, y = SLR, color = City)) +
  geom_line()

enter image description here

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
3

Using the data in the Note at the end first accumulate the series in a list L -- we assume any data frame having column names Year and SLR is to be added -- and then convert that to a single zoo object and plot it using autoplot.zoo which uses ggplot2. Remove the facet = NULL argument if you want them plotted in separate facets.

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

Note

We assume the following inputs:

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")
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
3

Instead of creating multiple objects in the global env, it can be read all at once in a loop

library(dplyr)
library(purrr)
library(ggplot2)
library(readr)
files <- c("Sydney1.csv", "Sydney2.csv", "Eden.csv", "Stonybay.csv", 
   "Lowhead.csv", "Hobart.csv", "Devonport.csv", "Freemantle.csv")
map_dfr(files, ~ read_csv(.x) %>% 
               mutate(City = .x)) %>%
   ggplot(aes(x = Year, y = SLR, color = City)) +
     geom_line())
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Try this:

Sydney1 %>% mutate(city = 'sydney1') %>%
  bind_rows(Sydney2 %>% mutate(city = 'sydney2')) %>%
  bind_rows(Eden %>% mutate(city = 'eden')) %>%
  bind_rows(Freemantle %>% mutate(city = 'freemantle')) %>%
  ggplot(aes(x=Year, y=SLR, color=city)) + geom_line() + facet_wrap(~city, scale='free')

enter image description here

Reza
  • 1,945
  • 1
  • 9
  • 17