【问题标题】:Combine two faceted plots on one plot在一个地块上合并两个多面地块
【发布时间】:2022-01-02 05:26:37
【问题描述】:

对不起,如果这是一个重复的问题,但我似乎无法在任何地方找到我的问题的答案。我有两个情节,我想在情节一上叠加两个情节,以便它们形成一个情节。这可能吗?我将附上这两个图的外观。它们都由相同的变量分面,该变量按位置划分,并且在相同的 x 和 y 轴刻度上,因此理论上应该是可能的。

谢谢。


## Plot one


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

情节二

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")

【问题讨论】:

  • 1) 情节在我看来几乎一样? 2)如果两者都由相同的变量刻面,为什么不将它们绘制为一个? 3) 覆盖图形,我喜欢 {cowplot} -在这里查看如何做到这一点wilkelab.org/cowplot/articles/aligning_plots.html
  • 抱歉,我意识到我已经上传了两次相同的链接,你能看到不同情节的新链接
  • 感谢 Tjebo,我按照您建议的链接进行操作,但这似乎仍然只绘制 align_plot 命令中的第二个情节? aligned_plots &lt;- align_plots(p1, p2, align="h", axis="tblr") ggdraw(aligned_plots[[1]]) + draw_plot(aligned_plots[[2]])
  • 从概念上讲,“价值”和“RSL”是一样的吗?
  • 是的,它们都是以米为单位的值

标签: r ggplot2 facet


【解决方案1】:

请参阅图一,如何使用拼凑而成。

但是。从概念上讲,我猜您想为某些历史值添加某种预测。我个人会将所有内容放在一个数据框中并绘制它。如果两个时间点之间的差距太大,您可以按时间点分面(如我的建议)。

这些图看起来与您的图有点不同,因为您只在Proxy 中提供了一个站点的数据(所以我过滤了另一个我认为是等效的,它仍然可以工作,因为刻面仍然存在)- 并且我删除了所有与问题无关的主题元素。

情节一个 - 组合情节。

library(tidyverse)
library(patchwork)

tgsm<-na.omit(tgsm)

tglonger <-
  pivot_longer(tgsm, cols=c(-Year), names_to="SITE", values_to = "RSL") %>%
  filter(SITE == "LSP2002") %>%
  rename(YEAR = Year)

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) +
  coord_cartesian(xlim = c(1800, 1830), ylim = c(-1, 0)) +
  theme_classic() +
  theme(
    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"
  )

p2 <- ggplot() +
  geom_point(data = tglonger, aes(x = YEAR, y = RSL, col = SITE), alpha = .7, size = 1) +
  facet_wrap(~SITE) +
  coord_cartesian(xlim = c(1990, 2000), ylim = c(-1, 0)) +
  theme_classic() +
  ## only one call to theme!! 
  theme(
    ## this is where the theme call is different to above
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.line.y = element_blank(),
    strip.background = element_blank(),
    strip.text.x = element_blank(),
    legend.position = "none", 
  )
p1 + p2

替代可视化的建议

df_new <- 
    bind_rows(time1 = Proxy, time2 = tglonger, .id = "timevar") %>%
    mutate(SITE = "LSP2002")

  ggplot(df_new)+
    geom_point(aes(x=YEAR,y=RSL))+
    facet_grid(SITE~timevar, scales = "free_x")+
    theme(legend.position="none") +
    theme(panel.spacing = unit(.5, "lines"))

您还可以使用此数据框来创建绘图列表,然后将其拼凑在一起。但是,这种方法不允许更改单个图。

ls_p <- 
  df_new %>%
  split(., .$timevar) %>%
  map(~{ggplot(.x)+
      geom_point(aes(x=YEAR,y=RSL))+
      coord_cartesian(ylim = c(-0.4,0))+
      facet_grid(~SITE, scales = "free_x")+
      theme(legend.position="none") +
      theme(panel.spacing = unit(.5, "lines"))})

library(patchwork)
wrap_plots(ls_p)

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2018-08-03
    • 2012-09-16
    • 1970-01-01
    • 2016-06-04
    相关资源
    最近更新 更多