Help with facet_grid

Hi,

I want to show tsibble a and a1 in the same line chart with different colors. Same goes with b and b1 to be seen in the same line chart with different colors. Then I want to create a facet_grid showing these two new line charts. Is it also possible to remove y-axis ticks in facet_grid? When I try doing that, nothing happens.

a <- data.frame(
          month = c("2020 Jan","2020 Feb","2020 Mar",
                    "2020 Apr","2020 May","2020 Jun"),
              A = c(5067331.423,4856897.658,
                    5194773.26153431,5115975.58575451,5081800.82696086,4977717.337768)
   )
a1 <- data.frame(
          month = c("2020 Jul","2020 Aug","2020 Sep",
                    "2020 Oct","2020 Nov","2020 Dec"),
              A = c(5170004.00689742,5329543.06179574,
                    5133931.03148727,5485318.98324681,5322618.30858017,
                    5852263.27838859)
   )
b <- data.frame(
          month = c("2020 Jan","2020 Feb","2020 Mar",
                    "2020 Apr","2020 May","2020 Jun"),
              B = c(609026,595426.8,613888.040283598,
                    617303.803431672,623692.818482367,631337.814039963)
   )
b1 <- data.frame(
          month = c("2020 Jul","2020 Aug","2020 Sep",
                    "2020 Oct","2020 Nov","2020 Dec"),
              B = c(640845.572917244,639615.61682654,
                    607289.023742614,643378.460602085,654007.4236215,
                    671581.497350881)
   )

Is this the sort of thing you are looking for?

a <- data.frame(
  month = c("2020 Jan","2020 Feb","2020 Mar",
            "2020 Apr","2020 May","2020 Jun"),
  A = c(5067331.423,4856897.658,
        5194773.26153431,5115975.58575451,5081800.82696086,4977717.337768)
)
a1 <- data.frame(
  month = c("2020 Jul","2020 Aug","2020 Sep",
            "2020 Oct","2020 Nov","2020 Dec"),
  A = c(5170004.00689742,5329543.06179574,
        5133931.03148727,5485318.98324681,5322618.30858017,
        5852263.27838859)
)
b <- data.frame(
  month = c("2020 Jan","2020 Feb","2020 Mar",
            "2020 Apr","2020 May","2020 Jun"),
  B = c(609026,595426.8,613888.040283598,
        617303.803431672,623692.818482367,631337.814039963)
  )
b1 <- data.frame(
  month = c("2020 Jul","2020 Aug","2020 Sep",
            "2020 Oct","2020 Nov","2020 Dec"),
  B = c(640845.572917244,639615.61682654,
        607289.023742614,643378.460602085,654007.4236215,
        671581.497350881)
)
library(dplyr, warn.conflicts = FALSE)
a <- rename(a, Value = A)
a1 <- rename(a1, Value = A)
b <- rename(b, Value = B)
b1 <- rename(b1, Value = B)
AllDat <- list(a = a, a1 = a1, b = b, b1 = b1)
AllDat <- bind_rows(AllDat, .id = "Source")
AllDat <- AllDat %>% mutate(Grp = substr(Source, 1, 1))
Levels <- paste("2020", c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                          "Aug", "Sep", "Oct", "Nov", "Dec"))
AllDat <- AllDat %>% mutate(month = factor(month, levels = Levels))
library(ggplot2)
ggplot(AllDat, aes(x = month, y = Value, color = Source, group = Source)) + 
  geom_line() + facet_wrap(~Grp, scales = "free_y") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

Created on 2020-09-20 by the reprex package (v0.3.0)

Thank you so much @FJCC!

Yes, I am looking for something similar. This is exactly I need for the line chart which shows both data in one line chart with different colors.

The only thing is that the real data has several years in it (from 2015 - 2025), so what can be the alternative to represent that in Levels.

Levels <- paste("2020", c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                          "Aug", "Sep", "Oct", "Nov", "Dec"))

I was thinking of using facet_grid to show these many years on x-axis and all line charts from top to bottom. In this example, I have just two line charts using a and b, but otherwise I have 5 such line charts under same years and months. That was another reason I was looking for facet_grid.

Also, can you please help me understanding

AllDat <- AllDat %>% mutate(Grp = substr(Source, 1, 1))

Thanks for your help!

I would represent the x axis with date values and format the display on the x axis to show only the year and month. The example below shows that and how to pick the break points on the x axis. With five years of data, you probably do not want to label every month but the method shows how to manually adjust the breaks if you do not like the defaults.

FirstHalf <- seq.Date(from = as.Date("2020-01-01"), 
                      to = as.Date("2020-06-01"), 
                      by = "month")
SecondHalf <- seq.Date(from = as.Date("2020-07-01"), 
                      to = as.Date("2020-12-01"), 
                      by = "month")
AllDates <- c(FirstHalf, SecondHalf)
DateLabels <- format(AllDates, "%Y %b")

a <- data.frame(
  month = FirstHalf,
  A = c(5067331.423,4856897.658,
        5194773.26153431,5115975.58575451,5081800.82696086,4977717.337768)
)
a1 <- data.frame(
  month = SecondHalf,
  A = c(5170004.00689742,5329543.06179574,
        5133931.03148727,5485318.98324681,5322618.30858017,
        5852263.27838859)
)
b <- data.frame(
  month = FirstHalf,
  B = c(609026,595426.8,613888.040283598,
        617303.803431672,623692.818482367,631337.814039963)
  )
b1 <- data.frame(
  month = SecondHalf,
  B = c(640845.572917244,639615.61682654,
        607289.023742614,643378.460602085,654007.4236215,
        671581.497350881)
)
library(dplyr, warn.conflicts = FALSE)
a <- rename(a, Value = A)
a1 <- rename(a1, Value = A)
b <- rename(b, Value = B)
b1 <- rename(b1, Value = B)
AllDat <- list(a = a, a1 = a1, b = b, b1 = b1)
AllDat <- bind_rows(AllDat, .id = "Source")
AllDat <- AllDat %>% mutate(Grp = substr(Source, 1, 1))

library(ggplot2)
ggplot(AllDat, aes(x = month, y = Value, color = Source, group = Source)) + 
  geom_line() + facet_wrap(~Grp, scales = "free_y") +
  scale_x_date(breaks = AllDates, minor_breaks = NULL, labels = DateLabels)+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

This code

AllDat <- AllDat %>% mutate(Grp = substr(Source, 1, 1))

adds a new column named Grp to AllDat that contains the first character of the Source column. The substr function pulls out a subset of a string. In this case it starts and ends at the first character. When Source is a or a1, Grp is a and when Source is b or b1, Grp is b. By building the facets using the Grp column, a and a1 are on one facet and b and b1 are on the other facet.

This topic was automatically closed 21 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.