amark
amark

Reputation: 51

Pass multiple filter as a parameter in a function or loop and create ggplot chart

I have below data set

Date <- c("01-01-2018"," 01-01-2018"," 01-02-2018","01-02-2018"," 01-03-2018"," 01-04-2018","01-04-2018"," 01-05-2018"," 01-06-2018","01-05-2018"," 01-06-2018"," 01-07-2018")
Metro <- c('BA',"FL","BA","CA","BA","FL","BA","CA","BA","FL","BA","CA")
Type <- c("S2", "S1", "S3", "S2", "S1", "S3", "S2", "S1", "S3", "S2", "S1", "S3")
Forecast1 <- c(123,131,120,128,131,152,128,141, 129, 147, 155, 144)
Forecast2 <- c(152,128,141, 129, 147, 155, 144,123,131,120,128,131)
df <- cbind.data.frame(Date,Metro,Type, Forecast1, Forecast2)
df

        Date Metro Type Forecast1 Forecast2
1   01-01-2018    BA   S2       123       152
2   01-01-2018    FL   S1       131       128
3   01-02-2018    BA   S3       120       141
4   01-02-2018    CA   S2       128       129
5   01-03-2018    BA   S1       131       147
6   01-04-2018    FL   S3       152       155
7   01-04-2018    BA   S2       128       144
8   01-05-2018    CA   S1       141       123
9   01-06-2018    BA   S3       129       131
10  01-05-2018    FL   S2       147       120
11  01-06-2018    BA   S1       155       128
12  01-07-2018    CA   S3       144       131

I want to create a function or loop that will take multiple Metro and Type as values and then for multiple combination of Metro and Type create ggplot line chart with Date, Forecast1 and Forecast2. Currently I am able to create the chart by filtering the data. It is not dynamic.

df_chart<-df[df$Metro=="BA" & df$Type=="S1",]

ggplot(data=df_chart, aes(x=as.Date(Date), y=Forecast1) ) +
  geom_line(color="red", size=1) +
  geom_line(aes(y=Forecast2), color="blue",size=1) +
  ylab("Forecast") +
  xlab("Date")

I want to create metro and Type parameters which can be passed as filters

Metro_list<-c("BA","FL")
Type<-c("S2", "S1")

How to create a loop or function that will take values from the above parameters and create charts with the combination? In the above scenario 4 charts should be created

Upvotes: 1

Views: 505

Answers (1)

Jack Brookes
Jack Brookes

Reputation: 3830

Grouping, then right joining with your parameter lists leaves you with only the groups you want. Nesting the data within groups will allow you to make a new column where each entry is the computed ggplot plot object.

library(tidyverse)

required_df <- tibble(Metro = c("BA", "FL"),
                      Type = c("S2", "S1")) %>%
  expand(Metro, Type)  

df %>%
  group_by(Metro, Type) %>%
  nest() %>%
  right_join(required_df, by = c("Metro", "Type")) %>%
  mutate(g = map(
    data,
    ~ ggplot(., aes(x = as.Date(Date), y = Forecast1)) +
      geom_line(color = "red", size = 1) +
      geom_line(aes(y = Forecast2), color = "blue", size = 1) +
      ylab("Forecast") +
      xlab("Date")
  ))

Output:

  Metro Type  data             g       
  <chr> <chr> <list>           <list>  
1 BA    S1    <tibble [2 x 3]> <S3: gg>
2 BA    S2    <tibble [2 x 3]> <S3: gg>
3 FL    S1    <tibble [1 x 3]> <S3: gg>
4 FL    S2    <tibble [1 x 3]> <S3: gg>

You can then of course access your vector of graphs with $g.

Upvotes: 1

Related Questions