2
I’m using Google’s mobility data to compile some charts, I needed to make a 5x5 chart, where in the rows would be the states and in the columns, the locations. I tried to use the facet_grid
, but it didn’t work out.
I would like the structure of the graph to be as follows, with the "-" signaling the graphs:
But when using the facet_grid
, the graph is coming out this way:
Code I am using:
library(tidyverse)
google <- read_csv("https://www.gstatic.com/covid19/mobility/Global_Mobility_Report.csv") %>%
filter(country_region == "Brazil") %>%
select(-c(country_region_code, country_region, sub_region_2, iso_3166_2_code, census_fips_code)) %>%
mutate(sub_region_1 = ifelse(is.na(sub_region_1), "Brazil", sub_region_1),
sub_region_1 = gsub(sub_region_1, pattern = "State of ", replacement = ""))
colnames(google) <- gsub(pattern = "_percent_change_from_baseline",
replacement = "",
x = colnames(google))
estados <- c("São Paulo", "Minas Gerais", "Rio de Janeiro", "Rio Grande do Sul",
"Paraná")
google %>%
filter(sub_region_1 %in% estados) %>%
ggplot(aes(x = date)) +
geom_line(aes(y = transit_stations)) +
geom_line(aes(y = parks)) +
geom_line(aes(y = grocery_and_pharmacy)) +
geom_line(aes(y = retail_and_recreation)) +
geom_line(aes(y = workplaces)) +
facet_grid(sub_region_1 ~ .)
How do I leave the chart as shown above? With variables being columns and states being rows?
You almost got there. Missed leaving the data Tidy and use it in Facet
– Tomás Barcellos