R语言 如何将两个图形以特定的布局相互叠加

b09cbbtk  于 2023-03-27  发布在  其他
关注(0)|答案(2)|浏览(226)

我希望我说清楚了,这是我在论坛上的第一个问题。
我有两个图,“A”只是一个变量,它在上面绘制(我在两个图中使用相同的代码,但我通过“Species”和“art”进行过滤),“B”是用facet_wrap绘制的几个变量。图应该是这样的:

但它看起来是这样的:

我已经尝试过网格。安排和拼凑,但我不能很好地容纳图像。下面我粘贴的最后输出的代码,与拼凑。非常感谢您的时间。
下面是代码:

library(tidyverse)
library(ggplot2)
library(datos)
library(readxl)
library(dplyr)
library(esquisse)
library(patchwork)
library(gridExtra)

#1P.corruscans - todas las artes

frec_todas_artes <- Frecuencia_de_tallas_por_arte_2023 %>% 
  #filter(!(Arte %in% c("A – Todas juntas"))) %>%
  filter(Arte %in% "A – Todas juntas") %>%
  filter(Especie %in% "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa", title ="Pseudoplatystoma corruscans") +
  theme_bw () + 
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol=2)

frec_todas_artes

#2PCO - resto de las artes

frec_artes_facetas <- Frecuencia_de_tallas_por_arte_2023 %>% 
  filter(!(Arte %in% c("A – Todas juntas", "J – Maroma"))) %>%
  #filter(Arte %in% "A – Todas juntas") %>%
  filter(Especie %in% "Pseudoplatystoma corruscans") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color="white") + 
  labs(x="Largo total (mm)", y="Frecuencia relativa") +
  theme_bw () + 
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size=8, angle = 90, vjust=0.5)) + 
  scale_x_continuous(breaks = seq(0, 1600, by=100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol=2)

frec_artes_facetas


#grid.arrange(frec_todas_artes, frec_artes_facetas, nrow = 2, widths = c(5,10))

#intento de reordenar con patchwork

frec_todas_artes / frec_artes_facetas +

plot_layout(heights = c(1,2))
8xiog9wr

8xiog9wr1#

一种选择是使用patchwork::plot_layoutdesign参数,该参数允许指定要用子图填充的空间,即使用

design <- 
"
#AA#
BBBB
"

您可以在第一行的图的左侧和右侧添加一些空间。
使用一些伪随机示例数据:

library(tidyverse)
library(patchwork)

set.seed(123)

Frecuencia_de_tallas_por_arte_2023 <- data.frame(
  Rango = seq(600, 1600, 100),
  Porcentaje = runif(11 * 7, 0, 40),
  Arte = rep(LETTERS[1:7], each = 11)
)

frec_todas_artes <- Frecuencia_de_tallas_por_arte_2023 %>%
  filter(Arte %in% "A") %>%
  ggplot() +
  aes(x = Rango, y = Porcentaje) +
  geom_col(fill = "#0c4c8a", color = "white") +
  labs(x = "Largo total (mm)", y = "Frecuencia relativa", title = "Pseudoplatystoma corruscans") +
  theme_bw() +
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5)) +
  scale_x_continuous(breaks = seq(0, 1600, by = 100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol = 2)

frec_artes_facetas <- Frecuencia_de_tallas_por_arte_2023 %>%
  filter(!Arte %in% "A") %>%
  ggplot() +
  aes(x = `Rango`, y = `Porcentaje`) +
  geom_col(fill = "#0c4c8a", color = "white") +
  labs(x = "Largo total (mm)", y = "Frecuencia relativa") +
  theme_bw() +
  theme(plot.title = element_text(family = "sans", face = "italic", hjust = 0.5)) +
  theme(axis.text.x = element_text(size = 8, angle = 90, vjust = 0.5)) +
  scale_x_continuous(breaks = seq(0, 1600, by = 100)) +
  scale_y_continuous(breaks = seq(0, 100, by = 10)) +
  facet_wrap(vars(`Arte`), ncol = 2)

design <- 
"
#AA#
BBBB
"

frec_todas_artes / frec_artes_facetas +
  plot_layout(
    design = design, heights = c(1, 2)
  )

6yt4nkrj

6yt4nkrj2#

我在这里展示我上面解释的内容。我有必要用“缩放”打开图像,看看它设计得很好,比例也很好。左边是“缩放”图像,右边是“图”中看到的图像。screenshot with two part of Rstudio

相关问题