R语言 具有拼接拼接的并排ggplot2

yftpprvb  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(147)

我想把下面的图片并排绘制好吗?

  1. library(ggplot2)
  2. library(patchwork)
  3. # Plot the DEMs
  4. p1 <- ggplot(dem %>% as.data.frame(xy = TRUE)) +
  5. geom_raster(aes(x = x, y = y, fill = `10m_BA`)) +
  6. scale_fill_gradientn(colors = terrain.colors(100))
  7. p2 <- ggplot(dem30 %>% as.data.frame(xy = TRUE)) +
  8. geom_raster(aes(x = x, y = y, fill = `10m_BA`)) +
  9. scale_fill_gradientn(colors = terrain.colors(100))
  10. # Combine the plots side by side
  11. combined_plot <- p1 + p2 + plot_layout(ncol = 2, guides = "collect")
  12. # Display the combined plot
  13. combined_plot

字符串


的数据
我如何分享y轴和图例?

mum43rcc

mum43rcc1#

您可以使用facet_wrappivot_longer,如下所示

  1. library(tidyverse)
  2. library(raster)
  3. f <- system.file("ex/elev.tif", package="terra")
  4. r1 <- stack(f)
  5. r2 <- stack(f)
  6. r <- stack(r1, r2)
  7. ggplot(r %>% as.data.frame(xy = TRUE) %>%
  8. pivot_longer(-c(x, y))) +
  9. geom_raster(aes(x = x, y = y, fill = value)) +
  10. facet_wrap(name~.) +
  11. scale_fill_gradientn(colors = terrain.colors(100))

字符串


的数据

展开查看全部

相关问题