0

I am trying to plot on a facet_wrap using geom_line two different variables of different sizes (the second one has a lot of NA) but using the same x axis (Years). Unfortunately the lines plots only for the biggest variable. How can I do it?

Here is an example of the format of my data, and my simplified code:

Sector Scenario Year Emissions1 Emissions2
Buildings S1 2019 65 NA
Buildings S1 2020 45 75
Buildings S2 2021 25 NA
Buildings S2 2022 67 NA
Transport S1 2019 86 65
Transport S1 2020 86 59
Transport S2 2021 68 NA
..... ..... ..... ..... .....
Title <- paste("World total GHG emissions, exc. LULUCF, all scenarios")
Subheader <- paste("Subheader ")


(Plot <- FinalTableAverage %>%
   filter(Sector == 'Transport') %>%
   select(-Sector) %>%
   pivot_longer(c(4:last_col()), names_to = 'Model', values_to = 'Value') %>%
   ggplot(aes(x = as.numeric(Year), y = Value, color = Model )) +
   geom_line(size=1) +
   geom_point(size=1.5) +
   ggtitle(Title , subtitle = Subheader )  +
   theme_bw() +
   facet_wrap(~ Scenario, scale = "free")+
   theme(axis.title = element_blank(),
         plot.title = element_text(face = "bold", size = 15),
         plot.subtitle = element_text(size = 10),
         legend.position = "bottom",
         legend.title = element_blank())
)
  • Hi, @Olivier. Can you please provide a [reproducible example](https://stackoverflow.com/q/5963269/6288065) of your data? E.g., you can provide the output from `dput(FinalTableAverage)` or (preferably) a modified subset version to keep the data minimal. From the looks of you data right now, you don't have any complete 2-time-point "Emissions2" Models within the "S2" Scenarios. I suggest that you filter out those incomplete data pairs before sharing the example data. – LC-datascientist May 03 '22 at 22:30

1 Answers1

0

While it is a bit difficult to see without your source data, you can do something like this:

FinalTableAverage %>% 
  filter(Sector == 'Transport') %>%
  select(-Sector) %>%
  pivot_longer(cols = starts_with("Emissions"), names_to = 'Model', values_to = 'Value') %>%
  filter(!is.na(Value)) %>% 
  ggplot(aes(x = as.numeric(Year), y = Value, color = Model )) +
  geom_line(size=1) +
  geom_point(size=1.5) +
  ggtitle(Title, subtitle = Subheader )  +
  theme_bw() +
  facet_wrap(~ Scenario, scale = "free")+
  theme(axis.title = element_blank(),
        plot.title = element_text(face = "bold", size = 15),
        plot.subtitle = element_text(size = 10),
        legend.position = "bottom",
        legend.title = element_blank())
)

Output:

facet_plot

Input:

structure(list(Sector = c("Buildings", "Buildings", "Buildings", 
"Buildings", "Transport", "Transport", "Transport", "Transport", 
"Transport", "Transport", "Transport", "Transport"), Scenario = c("S1", 
"S1", "S2", "S2", "S1", "S1", "S1", "S1", "S2", "S2", "S2", "S2"
), Year = c(2019, 2020, 2021, 2022, 2019, 2020, 2021, 2022, 2019, 
2020, 2021, 2022), Emissions1 = c(65L, 45L, 25L, 67L, 86L, 86L, 
48L, 50L, 48L, 48L, 68L, 43L), Emissions2 = c(NA, 75L, NA, NA, 
65L, 59L, 88L, 66L, 86L, 61L, NA, 72L)), row.names = c(NA, -12L
), class = "data.frame")
langtang
  • 22,248
  • 1
  • 12
  • 27