'Create multiplot with map and scatter
I am trying to create multiplot that contains a map (using geom_sf
) and a graph next to each other.
I tried to use GGpubr::ggarrange
and cowplot::plot_grid
for this purpose. But I get this warning:
1: In as_grob.default(plot) :
Cannot convert object of class tbl_dftbldata.frame into a grob.
And the result is not showing the map.
Does anyone know Any packages that can help me with this issue? A packages to use instead of GGpubr
or cowplot
?
Solution 1:[1]
use {patchwork}
library(sf)
library(ggplot2)
library(patchwork)
library(maps)
# usa polygon in lamert equal area b/c it's pretty
usa <- st_as_sf(map("usa", plot = FALSE, fill = TRUE)) |>
st_transform(st_crs("+proj=laea +lat_0=30 +lon_0=-95"))
# plot 1: geom_sf map of the us
p1 <- ggplot() +
geom_sf(data = usa)
# plot 2: geom_line ggplot
p2 <- ggplot(diamonds, aes(carat, price)) +
geom_point(alpha = 0.2) +
geom_smooth()
# use pathwork to combine plots
p1 + p2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rich Pauloo |