Multiple time-series graphs in each row of a multiple time-series plot in R

I am trying to create multiple time-series graphs into a multiple time-series plot. I have a total of 9 CSV, 3 CSV are for the land use category residential , 3 for non-residential and 3 for slums . All of them have one same column, called Month which has the same dates. I want to create a graph similar to picture1, but in each row of the picture I want to have three lines instead of one. Something like in picture 2 (apologize for the bad sketching). I can create the plot in picture1 but the problem is with the second picture. I don't know if that's possible, or if there is an other way, possible better, to do it.
The code for the plot1:

df <- read.csv("ww/ts_all_month_1.csv")
df_melt = melt(df, id.vars = 'Month')

#multiple ts plots
ggplot(df_melt, aes(x = Month, y = value, group = 1)) + 
  geom_line() + 
  facet_wrap(~ variable, scales = 'free_y', ncol = 1)

The data can be found here.


The link you have shared is private so we don't have access to your data, Can you please share a small part of the data set in a copy-paste friendly format?

In case you don't know how to do it, there are many options, which include:

  1. If you have stored the data set in some R object, dput function is very handy.

  2. In case the data set is in a spreadsheet, check out the datapasta package. Take a look at this link.

@andresrcs what a great package this datapasta, I wasn't aware of it. Here are 2 of my CSV

structure(list(Month = c("2016-01-01", "2016-02-01", "2016-03-01", 
"2016-04-01", "2016-05-01", "2016-10-01", "2016-11-01", "2016-12-01"
), Residential = c(24.60058, 29.55965, 28.32609, 37.65701, 25.14384, 
31.46176, 27.46613, 35.13766), Residential.Informal = c(36.63504, 
33.65881, 33.90984, 32.07269, 30.23018, 35.01135, 34.83798, 36.4656
), Non.Residential = c(69.93954, 71.60853, 78.92461, 79.95062, 
80.70823, 82.10297, 93.07622, 102.25243)), class = "data.frame", row.names = c(NA, 
-8L))
structure(list(Month = c("2016-01-01", "2016-02-01", "2016-03-01", 
"2016-04-01", "2016-05-01", "2016-10-01", "2016-11-01", "2016-12-01"
), Residential = c(29.6179, 32.18668, 28.8618, 37.98332, 29.43785, 
34.32784, 27.4716, 35.22018), Residential.Informal = c(19.04347, 
18.62447, 20.88858, 22.52886, 18.85139, 26.48309, 17.56881, 24.01665
), Non.Residential = c(15.26528, 19.11492, 20.5713, 20.26328, 
14.01063, 18.32237, 17.58561, 15.1267)), class = "data.frame", row.names = c(NA, 
-8L))

This data frame samples doesn't seem to match the structure description you gave on your first post, can you share a valid link to your raw csv files?. My idea is to read all csv files into a long data frame and define an id variable to facet by, but it is hard to give you a working solution without knowing the actual structure of your files.

Here is the link with the data. I re-checked them and the code to create a multiple time-series plot like the one in the picture1.

df1 <- read.csv("wd/ts_all_month_1.csv")
df_melt1 = melt(df1, id.vars = 'Month')

#multiple ts plots
ggplot(df_melt1, aes(x = Month, y = value, group = 1)) + 
  geom_line() + 
  facet_wrap(~ variable, scales = 'free_y', ncol = 1)

df2 <- read.csv("wd/ts_all_month_2.csv")
df_melt2 = melt(df2, id.vars = 'Month')

#multiple ts plots
ggplot(df_melt2, aes(x = Month, y = value, group = 1)) + 
  geom_line() + 
  facet_wrap(~ variable, scales = 'free_y', ncol = 1)

That link is not publicly accessible, please check on that so we can take a look and try to help you.

I apologize for that. this link should work now

It is not clear to me how you assign each csv to a category, at least based on their names so I'm going to give you an example with made-up criteria.

library(tidyverse)

# Donwloading sample data
url_csv_1 <- "https://drive.google.com/uc?export=download&id=1QYMupvbdaQoKMOZIrYgvk7eC8RbjKu6x"
url_csv_2 <- "https://drive.google.com/uc?export=download&id=1D4wrPa3boHx2kSY7y1dtvbE36Y9mi697"

download.file(url_csv_1, "ts_all_month_1.csv")
download.file(url_csv_2, "ts_all_month_2.csv")

# Relevant code

# Read all csv files into a long data frame
long_df <- read_csv(list.files(pattern = "\\.csv$"), id = "land_use", name_repair = "universal")

# To Assign land use, I don't know how you differentiate which file corresponds 
# to each category so I'm going to make it up.
long_df %>% 
    mutate(land_use = case_when(
        land_use == 'ts_all_month_1.csv' ~ 'non-residential',
        land_use == 'ts_all_month_2.csv' ~ 'slums'
    )) %>%
    pivot_longer(cols = contains("Residential"), names_to = 'Variable', values_to = 'Value') %>% 
    ggplot(aes(x = Month, y = Value, color = Variable)) + 
    geom_line() + 
    facet_wrap(~ land_use, scales = 'free_y', ncol = 1)

Created on 2022-05-12 by the reprex package (v2.0.1)

Thank you for the code. I have one question, the legend on the right side indicates that each line is a different land use, while the title of each plot shows that each line belongs to the same land use class.

Yes, as I said, I'm confused about the structure of your data, the description you gave about your csv files and the plot you are trying to get so I just made an example using a made up clasification criteria hoping that you can modify it to your requirements or at least clarify your request in the context of this example.

What variable should be represented by the different lines on each facet and what variable should be represented by the facets?

@andresrcs I see. In any case that's what I wanted to achieve, now all I have to do is to manipulate your code a little bit. Thank you for your support

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.