I have managed to devise a sort of solution which creates three different range sliders using subplot() instead of facet wrap:
dat <- read.csv("https://raw.githubusercontent.com/LucasTremlett/Proyecto-Visualizacion/master/owid-covid-data.csv")
dat<-dat %>% select(new_cases_per_million, date, location) %>%
mutate(date = as.Date(date)) %>%
filter(location=="Germany"| location=="Spain"| location=="Italy"| location=="United Kingdom"|
location=="Netherlands" | location=="France") %>%
mutate(pais=case_when(location=="Germany" ~ "Alemania",
location=="Italy" ~ "Italia",
location=="Netherlands" ~ "Paises Bajos",
location=="Spain" ~ "España",
location=="United Kingdom" ~ "Reino Unido",
location=="France" ~ "Francia")) %>%
mutate(tex=paste0("El ", as.character(date), " hubo ",
as.character(new_cases_per_million), " casos nuevos en ",
as.character(pais)))
p1<- dat %>%
filter(location=="Germany") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
ylab("") + xlab("") +
theme(panel.grid = element_blank())
p2<- dat %>%
filter(location=="Spain") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
ylab("") + xlab("") +
theme(panel.grid = element_blank())
p3<- dat %>%
filter(location=="Italy") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
ylab("") + xlab("") +
theme(panel.grid = element_blank())
p4<- dat %>%
filter(location=="United Kingdom") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
ylab("") + xlab("") +
theme(panel.grid = element_blank())
p5<- dat %>%
filter(location=="Netherlands") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
theme(panel.grid = element_blank())
p6<- dat %>%
filter(location=="France") %>%
ggplot(aes(x=date, y=new_cases_per_million, text=tex)) +
geom_area(aes(group=1)) +
facet_wrap( ~ pais) +
theme_minimal() +
scale_y_continuous(limits=c(0,200)) +
scale_x_date(limits = c(as.Date("2020-03-05"),as.Date("2020-05-06")),
date_breaks = "3 weeks", date_labels = "%d-%b") +
geom_hline(yintercept = 0) +
theme(panel.grid = element_blank())
subplot(ggplotly(p1, tooltip="text", dynamicTicks = TRUE) %>% rangeslider(),
(ggplotly(p2, tooltip="text", dynamicTicks = TRUE) %>% rangeslider()),
(ggplotly(p3, tooltip="text", dynamicTicks = TRUE) %>% rangeslider()),
(ggplotly(p4, tooltip="text", dynamicTicks = TRUE) %>% rangeslider()),
(ggplotly(p5, tooltip="text", dynamicTicks = TRUE) %>% rangeslider()),
(ggplotly(p6, tooltip="text", dynamicTicks = TRUE) %>% rangeslider()),
nrows=2, shareX = TRUE, shareY = TRUE)
This creates the graph in the link: RPubs - HTML
However I would much prefer having a single rangeslider that controls all graphs. I can do this setting nrows=6, however this creates a pretty ugly graph as all the plots are stacked over each other. Also it would be great if I could change the layout of the range slider, in this same way this user requests, only in R Studio instead of JS. Any suggestions on how to achieve this would be greatly appreciated. I know this is a lot for one post so thank you for bearing with me!!