1

Is there a way to accomplish this type of faceting in ggplot2 or any other plotting library available in R?

Here is an attempt to create some dummy data.

library(tidyverse)

country <- c(rep("Bahrain", 30), rep("Chile", 30), rep("Czech Rep.", 30))
day <- rep(1:30, 3)
cases <- c(runif(n = 30, 1, 5), runif(n = 30, 5, 10), runif(n = 30, 1, 20)) %>% round()

# trying to create some dummy data
df <- tibble(country, day, cases) %>%
  group_by(country) %>%
  mutate(cases = cumsum(cases))

# creating a plot with basic faceting
ggplot(df, aes(day, cases)) +
  geom_line() +
  facet_wrap(~country)

Only a subset of countries is shown.

Jakub.Novotny
  • 2,912
  • 2
  • 6
  • 21
  • To answer your question, yes it is possible to do that graph in `r`. However, I can't show you how to do it solely based on what you are providing. Your link is available only if you paid for Financial Times. Please read this link to know how to provide a good reproducible example on SO: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-exampledata – dc37 Mar 21 '20 at 19:17
  • I removed the link to the FT article. I hope you might deduce from the picture in the question what I am trying to accomplish. – Jakub.Novotny Mar 21 '20 at 19:21
  • I can deduce what functions you will have to use but I can't write the code that will make the figure without a reproducible data example. – dc37 Mar 21 '20 at 19:32
  • I have added some dummy data I created. – Jakub.Novotny Mar 21 '20 at 19:41

1 Answers1

1

In your case, I think the use of gghighlight could be helpful:

library(ggplot2)
library(gghighlight)
ggplot(df, aes(cases, day, color = country)) +
  geom_line() +
  gghighlight()+
  facet_wrap(~country)

enter image description here

Does it answer your question ?

Official documentation for gghighlight: https://yutannihilation.github.io/gghighlight/articles/gghighlight.html

dc37
  • 15,840
  • 4
  • 15
  • 32