如何将两个同变量分面的ggplot2绘图叠加合并为单张图
抱歉如果这是一个重复问题,我在各处都没有找到对应问题的答案。我现有两张绘图,想要将第二张图叠加到第一张图上,使二者合并为一张图。请问这一需求是否可以实现?我会附上两张图分别展示的效果。两张图均按位置这一相同变量分面,且x轴和y轴的刻度范围一致,因此理论上应该是可以实现的。
谢谢。
绘图1
Proxy<-read.csv("ALLRSL.csv",header=T) p1<-ggplot()+ geom_ribbon(data=Proxy,aes(x=YEAR,ymin=LOWER,ymax=UPPER,fill=SITE),alpha=.5)+ geom_line(data=Proxy,aes(x=YEAR,y=RSL,col=SITE))+ facet_wrap(~ SITE,ncol= 1)+ scale_fill_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+ scale_color_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+ theme_classic()+ xlim(1900, 2020)+ theme(panel.grid.major.x = element_blank())+ theme(panel.grid.minor.x = element_blank())+ theme(panel.grid.minor.y = element_blank())+ theme(panel.grid.major.y = element_blank())+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())+ theme( strip.background = element_blank(), strip.text.x = element_blank() )+ theme(legend.position="none") p1

绘图2
tgsm<-read.csv("tgsm.csv",header=T) tgsm<-na.omit(tgsm) tglonger<-pivot_longer(tgsm, cols=c(-Year),names_to="Site", values_to = "value") p2<-ggplot()+ geom_point(data=tglonger,aes(x=Year,y=value,col=Site),alpha=.7,size=1)+ facet_wrap(~Site,ncol=1)+ theme_classic()+ xlim(1900,2020)+ scale_color_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+ theme(panel.grid.major.x = element_blank())+ theme(panel.grid.minor.x = element_blank())+ theme(panel.grid.minor.y = element_blank())+ theme(panel.grid.major.y = element_blank())+ theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank())+ theme( strip.background = element_blank(), strip.text.x = element_blank() )+ theme(legend.position="none") p2

示例数据
Proxy <- structure(list(RSL = c(-0.305251214, -0.306414006, -0.307194187, -0.308202139, -0.309150572, -0.309679123), UPPER = c(-0.182716456, -0.186724068, -0.189331305, -0.193118273, -0.197069799, -0.20118809 ), LOWER = c(-0.416725663, -0.413606073, -0.411131729, -0.408930899, -0.406531588, -0.404478981), YEAR = 1820:1825, SITE = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Little Swanport", "Lutregala", "Tarra", "Wapengo"), class = "factor")), row.names = c(NA, 6L ), class = "data.frame") tgsm <- structure(list(Year = 1993:1998, Lg2002 = c(-0.001164223, -0.002229453, -0.002734792, -0.002977787, -0.002786098, -0.002026994), Wap2002 = c(-0.002531348, -0.002051993, -0.001468704, -0.001182162, -0.001027132, -0.00020881 ), Tar2002 = c(-0.029020612, -0.024330561, -0.019927593, -0.015682528, -0.012907219, -0.009784772), LSP2002 = c(-0.034514531, -0.030171621, -0.026095138, -0.021952898, -0.018480702, -0.014531318)), na.action = structure(c(`1` = 1L, `2` = 2L, `3` = 3L, `4` = 4L, `5` = 5L, `6` = 6L, `7` = 7L, `8` = 8L, `9` = 9L, `10` = 10L, `11` = 11L, `12` = 12L, `13` = 13L, `14` = 14L, `15` = 15L, `16` = 16L, `17` = 17L, `18` = 18L, `19` = 19L, `20` = 20L, `21` = 21L, `22` = 22L, `23` = 23L, `24` = 24L, `25` = 25L, `26` = 26L, `27` = 27L, `28` = 28L, `29` = 29L, `30` = 30L, `31` = 31L, `32` = 32L, `33` = 33L, `34` = 34L, `35` = 35L, `36` = 36L, `37` = 37L, `38` = 38L, `39` = 39L, `40` = 40L, `41` = 41L, `42` = 42L, `43` = 43L, `44` = 44L, `45` = 45L, `46` = 46L, `47` = 47L, `48` = 48L, `49` = 49L, `50` = 50L, `51` = 51L, `52` = 52L, `53` = 53L, `54` = 54L, `55` = 55L, `56` = 56L, `57` = 57L, `58` = 58L, `59` = 59L, `60` = 60L, `61` = 61L, `62` = 62L, `63` = 63L, `64` = 64L, `65` = 65L, `66` = 66L, `67` = 67L, `68` = 68L, `69` = 69L, `70` = 70L, `71` = 71L, `72` = 72L, `73` = 73L, `74` = 74L, `75` = 75L, `76` = 76L, `77` = 77L, `78` = 78L, `79` = 79L, `80` = 80L, `81` = 81L, `82` = 82L, `83` = 83L, `84` = 84L, `85` = 85L, `86` = 86L, `87` = 87L, `88` = 88L, `89` = 89L, `90` = 90L, `91` = 91L, `92` = 92L, `93` = 93L, `94` = 94L, `95` = 95L, `96` = 96L, `97` = 97L, `98` = 98L, `99` = 99L, `100` = 100L, `101` = 101L, `102` = 102L, `103` = 103L, `104` = 104L, `105` = 105L, `106` = 106L, `107` = 107L, `108` = 108L, `109` = 109L, `110` = 110L, `111` = 111L, `112` = 112L, `113` = 113L, `114` = 114L, `115` = 115L, `116` = 116L, `117` = 117L, `118` = 118L, `119` = 119L, `120` = 120L, `121` = 121L, `122` = 122L, `123` = 123L, `124` = 124L, `125` = 125L, `126` = 126L, `127` = 127L, `128` = 128L, `129` = 129L, `130` = 130L, `131` = 131L, `132` = 132L, `133` = 133L, `134` = 134L, `135` = 135L, `136` = 136L, `137` = 137L, `138` = 138L, `139` = 139L, `140` = 140L, `141` = 141L, `142` = 142L, `143` = 143L, `144` = 144L, `145` = 145L, `146` = 146L, `147` = 147L, `148` = 148L, `149` = 149L, `150` = 150L, `151` = 151L, `152` = 152L, `153` = 153L, `154` = 154L, `155` = 155L, `156` = 156L, `157` = 157L, `183` = 183L ), class = "omit"), row.names = 158:163, class = "data.frame")
解答
该需求可以直接实现,优先推荐合并图层的方案,避免分面对齐问题:
首先需要统一两个数据集的分面字段命名和站点取值对应,再把两个图的图层合并到同一个ggplot对象中即可,示例代码如下:
library(tidyverse) # 统一tgsm转换后的站点名称,和Proxy的SITE字段匹配 tglonger <- tglonger %>% mutate(SITE = case_when( Site == "LSP2002" ~ "Little Swanport", Site == "Lg2002" ~ "Lutregala", Site == "Tar2002" ~ "Tarra", Site == "Wap2002" ~ "Wapengo" )) # 合并图层绘图 p_combined <- ggplot()+ # 绘图1的 ribbon 和折线图层 geom_ribbon(data=Proxy,aes(x=YEAR,ymin=LOWER,ymax=UPPER,fill=SITE),alpha=.5)+ geom_line(data=Proxy,aes(x=YEAR,y=RSL,col=SITE))+ # 绘图2的散点图层 geom_point(data=tglonger,aes(x=Year,y=value,col=SITE),alpha=.7,size=1)+ facet_wrap(~ SITE,ncol= 1)+ scale_fill_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+ scale_color_manual(values=c("#4E193D","#342955","#4E617E","#97B4CB"))+ theme_classic()+ xlim(1900, 2020)+ theme(panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), panel.grid.major.y = element_blank(), axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), strip.background = element_blank(), strip.text.x = element_blank(), legend.position="none") p_combined
如果希望直接叠加两个已生成的ggplot对象,可以使用patchwork包实现:
library(patchwork) p1 + inset_element(p2, left = 0, bottom = 0, right = 1, top = 1)
内容的提问来源于stack exchange,提问作者Sophie Williams
相关产品推荐
相关产品推荐

