微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 facet_wrap_paginate 循环交替乱序生成的图

如何解决使用 facet_wrap_paginate 循环交替乱序生成的图

我一直无法弄清楚这一点,所以我想我会尝试再次发布它。关于如何在 R Markdown 中跨多个页面生成多个分面图的任何建议将不胜感激!

我有来自多个地点、年份和物种的人口规模数据,我正在尝试使用循环创建多个方面图。一些物种比其他物种拥有更多的数据,因此具有更大的分面图。为了在我编织到 PDF 时在多个页面上容纳那些较大的图,我尝试使用 facet_wrap_paginate() 函数,如下所示:https://www.programmingwithr.com/how-to-make-your-facet-wrap-facet-grid-ggplots-span-across-multiple-pages-in-pdf/

我遇到的问题是,由于某种原因,多页图相互交替,而不是作为一组打印。我有一种感觉,这与循环有关,但我终生无法弄清楚!这是一个可重现的示例:

---
output:
  pdf_document:
    fig_caption: yes
    latex_engine: xelatex
---


```{r,echo=FALSE,message=FALSE,warning=FALSE,comment=FALSE,results='hide'} 

library(tidyverse)
library(dplyr)
library(scales)
library(ggplot2)
library(ggforce)
library(MASS)
library(rlist)


site=c(71,72,126,139,146,180,240,4,5,9,10,11,16,23,2    3,26,27,33,34,39,42,43,44,48,50,56,58,62,65,66,71,76,77,79,86,93,94,110,115,116,128,138,147,163,171,181,185,1    98,198,211,231,232,234,1,6,12,17,18,19,20,21,25,30,32,31,35,38,47,51,5    2,52,53,54,55,57,60,63,64,70,73,75,74,78,82,83,84,85,87,89,92,95,96,97,98,99,105,106,107,108,109,112,114,116)
species=c("A","A","A    ","C","D","D    ","G","G    ","G")
size=c(23936,20953,3441,13764,22285,26379,20462,12936,26851,9261,21953,6256,12230,6029,9418,26593,16745,25028,13574,25277,5082,4100,18153,7067,23952,120    75,4750,15114,28718,6513,14051,19109,8042,14314,11190,28471,25527,2062,10875,1512,14099,25335,15873,1753,12494,14172,29171,19706,16618,21544,9606,9099,1    3449,8724,14153,6090,28905,5801,3651,26681,27708,4964,16222,10055,21807,9583,3825,6106,24058,25997,29628,20201,1234,10923,1380,15986,18723,29937,27127,2    5513,23680,7185,7532,14559,23925,761,5976,5070,26145,6885,5724,24299,11083,18167,17288,26562,18243,26999,20722,5223,21510,822,26173,14363,21359,6646,167    47,19681,25835,22308,12547,19352,17106,3407,19726,3106,778,18565,9144,23561,614,6908,24423,151,12592,15051,13801,16464,11086,9385,29443,9372,4753,24323,26782,2555,9612,6443,2355,6076,16619,16081,28311,9419,4827,21655,15289,8709,10343,4359,19348,20287,11196,16578,3814,8875,18533,4592,24807,10214,12840,12686,6835,29187,9023,8535,8737,16089,15160,16439,17777,5263,26520,9332,15415,12915,8434,21310,13869,22711,2575,8315,18471,8274,22094,15884,5968,23551,22549,1171,18035,23299,113,28367,25040,371,25521,10807,19298,16331,10414,2987,16030,27105,28342,29145,7470,11875,27806,2913,17062,17893,22917,5536,24830,17488,18349,28730,1082,28783,26238,29567,26022,23401,189,2019,18513,27073,4794,22501,3739,18935,11758,16808,16560,23961,5787,18267,23868,9730,19393,159    87,27142,18590,20161,26243,633,15695,21340,8447,24877,4551,2596,21810,21079,20629,27610,10092,18588,12951,23099,27136,7389,19377,12650,19367,14069,20966,19513,4894,4019,16459,9319,8024,16126,12359,10197,28976,11423,11590,24033,15054,26984,14745,15796,5588,12181,27260,9957,8187,14227,818,4457,8631,7789,2    8019,8802,16174,4068,13605,6668,2566,12843,13038,1980,18531,28190,12041,8147,7233,12506,700,25076,5841,14641,1817,18595,13717,25859,24239,17815,29599,28    757,1738,5526,17402,6254,23906,4878,5235,2841,12940,17251,25504,27630,17800,8989,27405,11705,1199,25909,20967,19371,458,1737,26254,29051,15799,8332,8464,21272,26090,11457,7019,5609,17542,27109,1572,2073,19199,4163,22952,29693,25201,19667,1154,20031,7187,1989,20132,21430,19176,23979,5634,18012,18206,5182,26005,8359,13758,18700,18006,23670,19268,10781,26584,18754,1969,4160,15903,21034,24990,1938,10898,5766,562,28766,11929,16548,14488,17065,13671,6537,10885,28667,10431,7814,223,26857,2087,26093,29452,19560,20482,21284,17536,15446,3487,9778,2224,23057,14527,29826,856,19457,16302,27760,3344,22919,8879,27765,19111,15808,18563,26791,13369,10545,28693,16551,11635,7626,7829,5430,28940,11756,831,29249,12142,11452,21741,28472,15981,4332,18030,25029,238    30,21817,15079,6812,6917,4029,2221,19018,8309,27531,6211,29473,4535,16826,14083,10705,23147,14582,1928,9999,18789,12209,14852,24935,20667,29500,29912,23    458,8225,4171,122,24397,26736,24358,17012,4463,862,3163,27196,6772,26229,29177)
year=c(2016,2016,1883,1903,1908,1923,1932,1933,1935,1937,1943,1944,1945,1946,1948,1950,1951,1965,1971,1974,1975,1976,1977,1978,1979,1981,1982,1983,1987,1998,2000,2003,2011,2015,2005,2001,2018,2014,1988,2006,2013,2017,2020,1993,20    01,2008,2002,1992,1967,1984,1986,1990,1996,2004,2010,2009,2007,2    016,1999,1961,1970,197    2,1995,1929,1972,1973,1991,19    87,1997,1    970,197    9,19    89,1985,1    979,201    3,1987)
as.factor(site)
as.factor(species)
as.integer(year)
as.integer(size)
data=data.frame(site,species,size,year)

#Split data into list by species
data_split=split(data,data$species)

#Subset list to separate by number of sites
#To allow for plotting large plots with facet_wrap_pageinate
less20=subset(data_split,n_distinct(site)<20)
plus20=subset(data_split,n_distinct(site)>20)
```

#Chunk to create short figures (i.e,less than 20 sites)
```{r,fig.cap=paste("Population size for ",names(less20),"species."),fig.align='center'}

fig.fun1=function(x){
plot1=ggplot(x,aes(x=year,y=size)) + geom_point()+ facet_wrap(~site,scales = "free",ncol = 2)+
  stat_smooth(method="glm.nb") + xlab("Year") + ylab("Population size")+theme_classic() +
  theme(text = element_text(size=20))+theme(strip.text.x = element_text())+
  theme(plot.margin = margin(10,15,10))+
  scale_y_continuous(limits=c(0,NA))+
  scale_x_continuous(breaks= scales::breaks_pretty(n=4))

print(plot1)
cat("\n")
  }
invisible(lapply(less20,fig.fun1))

```
#Chunk to create long figures via facet_wrap_paginate (i.e.,with >20 sites)
```{r,names(plus20),"species"),fig.align='center'}
fig.fun2=function(x){    
p=ggplot(x,y=size)) + geom_point()+ facet_wrap_paginate(~site,ncol = 2,nrow=6)+
  stat_smooth(method="glm.nb") + xlab("Year") + ylab("Population size")+theme_classic() +
  theme(text = element_text())+theme(strip.text.x = element_text())+
  theme(plot.margin = margin(10,NA))+
  scale_x_continuous(breaks= scales::breaks_pretty(n=4))
    required_n_pages = n_pages(p)
for(i in 1:required_n_pages){
print(ggplot(x,nrow=6,page=i)+
  stat_smooth(method="glm.nb") + xlab("Year") + ylab("Population size")+theme_classic() +
  theme(text = element_text())+theme(strip.text.x = element_text())+
  theme(plot.margin = margin(10,NA))+
  scale_x_continuous(breaks= scales::breaks_pretty(n=4)))
}}

invisible(lapply(plus20,fig.fun2))
```

I would like all of the same species' plots to print together (in this example,print all species G plots in a row and all species D plots in row) so they aren't alternating with each other. Additionally,is there a way to make sure they are all labeled as the same figure and not considered separate figures (something akin to the longtable/repeat header function for tables in kable)? Any advice or suggestions would be greatly appreciated! :D

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?