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

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

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

library(ggplot2)
library(patchwork)

# Plot the DEMs
p1 <- ggplot(dem %>% as.data.frame(xy = TRUE)) +
  geom_raster(aes(x = x, y = y, fill = `10m_BA`)) +
  scale_fill_gradientn(colors = terrain.colors(100))
p2 <- ggplot(dem30 %>% as.data.frame(xy = TRUE)) +
  geom_raster(aes(x = x, y = y, fill = `10m_BA`)) +
  scale_fill_gradientn(colors = terrain.colors(100))

# Combine the plots side by side
combined_plot <- p1 + p2 + plot_layout(ncol = 2, guides = "collect")

# Display the combined plot
combined_plot

字符串


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

mum43rcc

mum43rcc1#

您可以使用facet_wrappivot_longer,如下所示

library(tidyverse)
library(raster)

f <- system.file("ex/elev.tif", package="terra")
r1 <- stack(f)
r2 <- stack(f)

r <- stack(r1, r2)

ggplot(r %>% as.data.frame(xy = TRUE) %>% 
         pivot_longer(-c(x, y))) +
  geom_raster(aes(x = x, y = y, fill = value)) +
  facet_wrap(name~.) +
  scale_fill_gradientn(colors = terrain.colors(100))

字符串


的数据

相关问题