Figures alternating when produced through a loop with markdown - r
I have population size data from multiple sites, years, and species and I am trying to create multiple facet plots using loops. Some of the species have much more data than the others and, as such, have much larger facet plots. In order to accommodate those larger plots across multiple pages when I knit to PDF I am attempting to use the facet_wrap_paginate() function as per: https://www.programmingwithr.com/how-to-make-your-facet-wrap-facet-grid-ggplots-span-across-multiple-pages-in-pdf/
The issue that I am having is that for some reason the multi-paged plots are alternating with each other and not printing as a group. I have a feeling this has something to do with the loop but I can't for the life of me figure it out! Here a reproducible example:
---
output:
pdf_document:
fig_caption: yes
latex_engine: xelatex
---
#Chunk to set up data
{r , echo=FALSE, message=FALSE, warning=FALSE, comment=FALSE,results='hide'}
# Load packages
library(tidyverse)
library(dplyr)
library(scales)
library(ggplot2)
library(ggforce)
library(MASS)
library(rlist)
site=c(71,72,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,139,146,146,146,180,180,180,180,72,126,126,126,126,126,126,126,126,126,126,126,240,240,240,4,5,9,9,9,9,9,10,11,16,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,26,26,27,27,27,27,27,33,33,33,34,39,42,43,44,48,50,50,50,50,56,56,56,56,58,62,65,66,71,72,72,76,77,79,79,79,79,79,79,79,79,79,79,79,79,79,86,93,94,94,110,115,116,116,116,128,128,128,128,128,128,128,128,128,138,147,147,163,163,163,163,163,171,171,180,181,185,185,185,185,185,198,198,198,211,231,232,232,232,232,232,234,234,1,1,1,6,6,6,6,9,9,9,9,9,9,9,9,10,10,10,11,11,11,12,12,12,12,16,16,16,16,17,17,17,17,17,18,18,18,19,19,19,19,19,19,20,20,20,20,21,21,21,21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,25,25,25,25,26,26,26,27,27,27,27,27,27,27,27,27,27,27,30,30,30,32,32,32,32,31,31,31,33,33,33,33,33,33,35,35,35,38,38,38,38,38,42,42,42,42,42,47,47,47,48,48,48,50,50,50,50,50,51,51,51,52,52,52,52,53,53,53,53,54,54,54,54,55,55,55,56,56,56,56,57,57,57,57,57,58,58,58,60,60,60,60,62,62,62,62,62,63,63,63,63,64,64,64,65,65,65,70,70,70,70,71,71,71,71,72,72,72,72,73,73,73,73,75,75,75,76,76,76,74,74,74,78,78,78,79,79,79,79,79,79,79,79,82,82,82,82,82,82,82,83,83,83,83,84,84,84,84,85,85,85,85,85,86,86,86,86,87,87,87,89,89,89,92,92,92,92,92,94,94,94,94,94,95,95,95,95,96,96,96,97,97,97,98,98,98,99,99,99,105,105,105,105,106,106,106,106,107,107,107,108,108,108,108,108,109,109,109,110,110,110,110,110,110,112,112,112,112,114,114,114,115,115,115,116)
species=c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","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,12075,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,13449,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,25513,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,16747,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,85,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,15987,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,28019,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,28757,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,31,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,29187,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,23830,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,23458,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,1980,1981,1982,1983,1987,1998,2000,2003,2011,2015,2016,2019,2005,2001,2016,2018,2001,2014,2016,2018,2016,1988,2000,2003,2006,2013,2014,2015,2017,2018,2019,2020,1987,1993,2001,1987,2008,1987,2020,2019,2018,2016,1987,2002,1992,1967,1971,1976,1979,1980,1981,1984,1986,1987,1988,1990,1992,1996,1998,2000,2002,2004,2006,2008,2010,2016,2018,1980,1987,1980,1981,1987,2006,2009,1980,1987,1989,2009,2016,2006,2006,1989,2002,1971,1976,1980,1987,2002,1987,1989,1990,1979,2007,2002,2008,2016,2006,2016,2004,1987,1981,1986,1989,1990,1992,1996,1998,1999,2007,2008,2010,2015,2019,2007,2010,1987,2001,1961,2002,1987,1989,1990,1961,1970,1971,1972,1974,1979,1982,1986,2008,2008,1995,2001,1976,1978,1979,1980,1987,2002,2009,2016,2009,1929,1965,1972,1976,1992,1980,1982,1987,1987,1973,1975,1991,1992,1995,2001,1999,2006,1987,2002,2013,1971,1987,2002,2013,1971,1986,1987,2002,2013,2018,2019,2020,1987,2002,2013,1987,2002,2013,1987,2002,2013,2018,1971,1987,2002,2013,1979,1998,2001,2010,2013,1987,2002,2013,1971,1987,2002,2006,2009,2013,1979,1996,1998,2013,1971,1978,1987,1989,1997,2002,2013,1967,1971,1976,1978,1979,1980,1981,1984,1986,1987,1988,1990,1992,1996,1998,2000,2002,2004,2006,2008,2010,2013,2014,2015,2016,2018,1979,1998,2006,2013,1987,2002,2013,1970,1971,1981,1987,2002,2006,2009,2013,1990,2003,2005,1987,2002,2013,1987,2002,2013,2016,1987,2002,2013,1971,1986,1987,2002,2007,2013,1979,1998,2013,1979,1995,1998,2001,2013,1979,1998,2006,2009,2013,1979,1998,2013,1987,2002,2013,1971,1976,1987,2002,2013,1987,2002,2013,1979,1998,2007,2013,1979,1998,2001,2013,1971,1987,2002,2013,1987,2002,2013,1971,1987,2002,2013,1971,1987,2002,2013,2018,1979,1998,2013,1987,2002,2013,2016,1971,1987,2002,2007,2013,1987,1989,2002,2013,1979,1998,2013,1987,2002,2013,1979,1998,2001,2013,1987,2002,2013,2016,1987,2002,2013,2016,1971,1987,2002,2013,1987,2002,2013,1979,1998,2013,1987,2002,2013,1979,1998,2013,1978,1979,1980,1981,1985,1989,1998,2013,1971,1987,1990,2002,2013,2016,2017,1979,1998,2001,2013,1979,1998,2001,2013,1971,1979,1998,2001,2013,1987,2002,2013,2018,1987,2002,2013,1979,1998,2013,1979,1998,2001,2013,2016,1979,1987,1998,2001,2013,1979,1998,2006,2013,1979,1998,2013,1987,2002,2013,1979,1998,2013,1979,1998,2013,1987,2002,2013,2016,1971,1987,2002,2013,1987,2002,2013,1971,1981,1987,2002,2013,1987,2002,2013,1979,1998,2001,2013,2009,2015,1971,1987,2002,2013,1987,2002,2013,1987,2002,2013,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,echo=FALSE, fig.cap=paste("Population size for ", names(less20),"species."),warning=FALSE,message=FALSE,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, 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,echo=FALSE, fig.cap=paste("Population size for ", names(plus20),"species"),warning=FALSE,message=FALSE,fig.align='center'}
fig.fun2=function(x){
p=ggplot(x,aes(x=year, y=size)) + geom_point()+ facet_wrap_paginate(~site,scales = "free",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, 15, 10, 10))+
scale_y_continuous(limits=c(0,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,aes(x=year, y=size)) + geom_point()+ facet_wrap_paginate(~site,scales = "free",ncol = 2,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, 15, 10, 10))+
scale_y_continuous(limits=c(0,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
Your code does not have the correct Rmarkdown syntax, this is what I used to reproduce your code. The figures on mine were in order. Maybe attempting to visualize the data from a different perspective would allow for a more dense and overview. Do you know exactly what kind of visualization you are trying to report?
---
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,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126 ,139,146,146,146,180,180,180,180,72,126,126,126,126,126,126,126,126,126,126,126,240,240,240,4,5,9,9,9,9,9,10,11,16,23,23,23,23,23,23,23,23,23,23,23,23,2 3,23,23,23,23,23,23,23,23,23,26,26,27,27,27,27,27,33,33,33,34,39,42,43,44,48,50,50,50,50,56,56,56,56,58,62,65,66,71,72,72,76,77,79,79,79,79,79,79,79,79, 79,79,79,79,79,86,93,94,94,110,115,116,116,116,128,128,128,128,128,128,128,128,128,138,147,147,163,163,163,163,163,171,171,180,181,185,185,185,185,185,1 98,198,198,211,231,232,232,232,232,232,234,234,1,1,1,6,6,6,6,9,9,9,9,9,9,9,9,10,10,10,11,11,11,12,12,12,12,16,16,16,16,17,17,17,17,17,18,18,18,19,19,19, 19,19,19,20,20,20,20,21,21,21,21,21,21,21,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,25,25,25,25,26,26,26,27,27,27,27 ,27,27,27,27,27,27,27,30,30,30,32,32,32,32,31,31,31,33,33,33,33,33,33,35,35,35,38,38,38,38,38,42,42,42,42,42,47,47,47,48,48,48,50,50,50,50,50,51,51,51,5 2,52,52,52,53,53,53,53,54,54,54,54,55,55,55,56,56,56,56,57,57,57,57,57,58,58,58,60,60,60,60,62,62,62,62,62,63,63,63,63,64,64,64,65,65,65,70,70,70,70,71, 71,71,71,72,72,72,72,73,73,73,73,75,75,75,76,76,76,74,74,74,78,78,78,79,79,79,79,79,79,79,79,82,82,82,82,82,82,82,83,83,83,83,84,84,84,84,85,85,85,85,85 ,86,86,86,86,87,87,87,89,89,89,92,92,92,92,92,94,94,94,94,94,95,95,95,95,96,96,96,97,97,97,98,98,98,99,99,99,105,105,105,105,106,106,106,106,107,107,107 ,108,108,108,108,108,109,109,109,110,110,110,110,110,110,112,112,112,112,114,114,114,115,115,115,116)
species=c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A ","A","A","A","A","A","A","A","A","A","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D","D","D","D","D","D","D","D","D ","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D ","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D ","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G","G ","G","G","G","G","G","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,85,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,31,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,29187,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,1980,1981,1982,1983, 1987,1998,2000,2003,2011,2015,2016,2019,2005,2001,2016,2018,2001,2014,2016,2018,2016,1988,2000,2003,2006,2013,2014,2015,2017,2018,2019,2020,1987,1993,20 01,1987,2008,1987,2020,2019,2018,2016,1987,2002,1992,1967,1971,1976,1979,1980,1981,1984,1986,1987,1988,1990,1992,1996,1998,2000,2002,2004,2006,2008,2010 ,2016,2018,1980,1987,1980,1981,1987,2006,2009,1980,1987,1989,2009,2016,2006,2006,1989,2002,1971,1976,1980,1987,2002,1987,1989,1990,1979,2007,2002,2008,2 016,2006,2016,2004,1987,1981,1986,1989,1990,1992,1996,1998,1999,2007,2008,2010,2015,2019,2007,2010,1987,2001,1961,2002,1987,1989,1990,1961,1970,1971,197 2,1974,1979,1982,1986,2008,2008,1995,2001,1976,1978,1979,1980,1987,2002,2009,2016,2009,1929,1965,1972,1976,1992,1980,1982,1987,1987,1973,1975,1991,1992, 1995,2001,1999,2006,1987,2002,2013,1971,1987,2002,2013,1971,1986,1987,2002,2013,2018,2019,2020,1987,2002,2013,1987,2002,2013,1987,2002,2013,2018,1971,19 87,2002,2013,1979,1998,2001,2010,2013,1987,2002,2013,1971,1987,2002,2006,2009,2013,1979,1996,1998,2013,1971,1978,1987,1989,1997,2002,2013,1967,1971,1976 ,1978,1979,1980,1981,1984,1986,1987,1988,1990,1992,1996,1998,2000,2002,2004,2006,2008,2010,2013,2014,2015,2016,2018,1979,1998,2006,2013,1987,2002,2013,1 970,1971,1981,1987,2002,2006,2009,2013,1990,2003,2005,1987,2002,2013,1987,2002,2013,2016,1987,2002,2013,1971,1986,1987,2002,2007,2013,1979,1998,2013,197 9,1995,1998,2001,2013,1979,1998,2006,2009,2013,1979,1998,2013,1987,2002,2013,1971,1976,1987,2002,2013,1987,2002,2013,1979,1998,2007,2013,1979,1998,2001, 2013,1971,1987,2002,2013,1987,2002,2013,1971,1987,2002,2013,1971,1987,2002,2013,2018,1979,1998,2013,1987,2002,2013,2016,1971,1987,2002,2007,2013,1987,19 89,2002,2013,1979,1998,2013,1987,2002,2013,1979,1998,2001,2013,1987,2002,2013,2016,1987,2002,2013,2016,1971,1987,2002,2013,1987,2002,2013,1979,1998,2013 ,1987,2002,2013,1979,1998,2013,1978,1979,1980,1981,1985,1989,1998,2013,1971,1987,1990,2002,2013,2016,2017,1979,1998,2001,2013,1979,1998,2001,2013,1971,1 979,1998,2001,2013,1987,2002,2013,2018,1987,2002,2013,1979,1998,2013,1979,1998,2001,2013,2016,1979,1987,1998,2001,2013,1979,1998,2006,2013,1979,1998,201 3,1987,2002,2013,1979,1998,2013,1979,1998,2013,1987,2002,2013,2016,1971,1987,2002,2013,1987,2002,2013,1971,1981,1987,2002,2013,1987,2002,2013,1979,1998, 2001,2013,2009,2015,1971,1987,2002,2013,1987,2002,2013,1987,2002,2013,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,echo=FALSE, fig.cap=paste("Population size for ", names(less20),"species."),warning=FALSE,message=FALSE,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, 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,echo=FALSE, fig.cap=paste("Population size for ", names(plus20),"species"),warning=FALSE,message=FALSE,fig.align='center'}
fig.fun2=function(x){
p=ggplot(x,aes(x=year, y=size)) + geom_point()+ facet_wrap_paginate(~site,scales = "free",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, 15, 10, 10))+
scale_y_continuous(limits=c(0,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,aes(x=year, y=size)) + geom_point()+ facet_wrap_paginate(~site,scales = "free",ncol = 2,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, 15, 10, 10))+
scale_y_continuous(limits=c(0,NA))+
scale_x_continuous(breaks= scales::breaks_pretty(n=4)))
}}
invisible(lapply(plus20,fig.fun2))
```
Related
Is there a function for adjusting the size of my ggplot chart in R
I am trying to increase the size of my ggplot chart: And I tried this: ggplot(accident_day, aes(year, accidents, group = 1)) + geom_point() + geom_line() + labs(title = "THE RATE OF ACCIDENTS", x = "Year", y = "Number of Accidents")+ theme(figure.width = 8, figure.height = 4) and also this : theme(plot.width = 10, plot.height = 5) But got this output: Error in `mapply()`: ! The `plot.width` theme element is not defined in the element hierarchy. Run `rlang::last_error()` to see where the error occurred.
Assuming you mean the output of the plot in the HTML markdown file, you can use the following argument in the code chunk's header: ```{r fig.height=10, fig.width=10} ggplot(accident_day, aes(year, accidents, group = 1)) + geom_point() + geom_line() + labs(title = "THE RATE OF ACCIDENTS", x = "Year", y = "Number of Accidents") ``` You can also set the default size of the plots in the knitr setup chunk like so: ```{r} knitr::opts_chunk$set( fig.width=10, fig.height=10) ```
R: How to join two different plots in the same pdf page over a loop
I am interested in visualizing two plots together in the same page. One plot shows Usage of Drugs over time and the other plot shows Occurrence of Resistance over time. The Y-axis scales are very different for both so I think displaying them in separate graphs is useful. I use the following code to generate the two different graphs for 676 ids(or elements in my data) in to two separate pdfs. This is not helpful when comparing how usage and resistance for one id is varying with time. Instead I would like to generate one pdf and in each page of the pdf, I would like to show the resistance and usage variation over time for the same id/element. So goal is to have 676 pages for 676 ids in my pdf and in each page display the use and resistance for the same id. I know this can be done using grid.arrange from gridExtra but not sure how to use it in a loop and with lapply. ###Resistance Plot_list1 =list() #this is the loop for (i in J0$id){ temp1 <- J0%>% filter(id==i)%>% ggplot(aes(x = Year , y = Rest)) + geom_line()+ geom_point()+ scale_x_continuous(breaks=c(2008, 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018))+ theme(axis.text.x = element_text(angle = 90))+ theme(legend.position = "none") + ggtitle(i) Plot_list1[[i]] <- temp1 } ##saving the loop in pdf pdf("Resistance.pdf") invisible(lapply(Plot_list1, print)) dev.off() ###Usage Plot_list2 =list() #this is the loop for (i in J0$id){ temp2 <- J0%>% filter(id==i)%>% ggplot(aes(x = Year , y = DUL0)) + geom_line()+ geom_point()+ scale_x_continuous(breaks=c(2008, 2009,2010,2011,2012,2013,2014,2015,2016,2017,2018))+ theme(axis.text.x = element_text(angle = 90))+ theme(legend.position = "none") + ggtitle(i) Plot_list2[[i]] <- temp2 } ##saving the loop in pdf pdf("UsageDUL0.pdf") invisible(lapply(Plot_list2, print)) dev.off()
Here's a terse walk-through. Step 1, generate fake data, plot it individually into gg1 and gg2, then combine them using patchwork. This could easily (and perhaps arguably should) be broken into multiple stages, but it's small enough to use just one lapply. library(ggplot2) library(patchwork) set.seed(42) allgg <- lapply(1:3, function(ind) { dat <- mtcars[sample(NROW(mtcars), 10),] gg1 <- ggplot(dat, aes(disp, mpg)) + geom_point(color = "red") + labs(title = paste("Page ", ind), subtitle = "mpg ~ disp") gg2 <- ggplot(dat, aes(qsec, drat)) + geom_point(color = "blue") + labs(subtitle = "drat ~ qsec") gg1 / gg2 }) Start the pdf file, plot them all, then close the device. pdf("quux.pdf", onefile = TRUE, width = 6, height = 6) for (pg in allgg) print(pg) dev.off() For me, I get a 6x6 (inch) PDF with three pages, looking like:
How to reorder the results on Plot_composition Microbiome
I got a plot composition with different phylum with a category Other like this : pseq <- Merge_Final_pourcent %>% aggregate_taxa(level = "Phylum") ps1.com.fam <- microbiome::aggregate_top_taxa(pseq, "Phylum", top = 11) ps1.com.fam.rel <- microbiome::transform(ps1.com.fam, "compositional") svg(file="phylum.svg") plot_composition(ps1.com.fam.rel) + theme(legend.position = "bottom") + scale_fill_brewer("Phylum", palette = "Paired") + theme_bw() + theme(axis.text.x = element_text(angle = 90)) + ggtitle("Relative abundance") + theme(legend.title = element_text(size = 18)) dev.off() I try to go down the category Other : So I try to change my phyloseq and convert only the name other by Z_other but it deseappear from the phyloseq object and from the plot too: essai<-as.data.frame(tax_table(ps1.com.fam.rel)) essai$unique <- sub("Other", "Z_Other", essai$unique) essai$Phylum <- sub("Other", "Z_Other", essai$Phylum) rownames(essai) <- sub("Other", "Z_Other", rownames(essai)) tax_table(ps1.com.fam.rel) <- as.matrix(essai) essai<-as.data.frame(otu_table(ps1.com.fam.rel)) rownames(otu_table(ps1.com.fam.rel)) <- sub("Other", "Z_Other", rownames(otu_table(ps1.com.fam.rel))) otu_table(ps1.com.fam.rel) <- as.matrix(essai) svg(file="0_1phylum_BMP.svg") plot_composition(ps1.com.fam.rel) + theme(legend.position = "bottom") + scale_fill_brewer("Phylum", palette = "Paired") + theme_bw() + theme(axis.text.x = element_text(angle = 90)) + ggtitle("Relative abundance") + theme(legend.title = element_text(size = 18)) dev.off()
My suggestion would be to try to reorder the levels using factor. Perhaps this might help. essainoo <- subset(essai, essai$Phylum!="Other") neworder <- c(unique(essainoo$Phylum),"Other") essai$Phylum <- factor(essai$Phylum, levels=neworder)
I know it's very late this answer, but maybe someone else can use it because it's the only forum I could find with this question. I am not an R expert, and this way is only if you intend to generate a microbiome composition graph with top 20-30. It is super rudimentary, but it worked for me. The order of the graph is that of the OTU table, so I simply reordered: Phyloseq_object#otu_table[c(1,2,3,5,4),] Where: -phyloseq_object is my phyloseq object, -otu_table is by default that name -c(1,2,3,5,4) my desired order. Greetings!!!
Circular tree with heatmap
This question is quite trivial but I cannot be handled nicely with. I'm trying to plot a circular tree with a side heatmap. I'm using ggtree but any approach ggplo2 based is welcome. The problems that I'm not understanding well the gheatmap function. I want: 1- names AFTER the heatmap 2- 2 text columns after heatmap (for while may have the same value, but I need to know how to add it ) 3- heatmap columns name nicely handled, should we remove the columns name and use different colors scales for each? wherever the solution falls might better than the way it is now library(tidyverse) library(ggtree) library(treeio) library(tidytree) beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree") beast_tree <- read.beast(beast_file) genotype_file <- system.file("examples/Genotype.txt", package="ggtree") genotype <- read.table(genotype_file, sep="\t", stringsAsFactor=F) colnames(genotype) <- sub("\\.$", "", colnames(genotype)) p <- ggtree(beast_tree, mrsd="2013-01-01",layout = "fan", open.angle = -270) + geom_treescale(x=2008, y=1, offset=2) + geom_tiplab(size=2) gheatmap(p, genotype, offset=5, width=0.5, font.size=3, colnames_angle=-45, hjust=0) + scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"), name="genotype") Thanks in advance UPDATE: I found a better way to plot the name of heatmap columns. Also, I found that the simplification of the data was useful to clean up a little the tip labels. Now, I just need to add two text columns after heatmap. p <- ggtree(beast_tree) gheatmap( p, genotype, colnames=TRUE, colnames_angle=90, colnames_offset_y = 5, colnames_position = "top", ) + scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"), name="genotype") UPDATE 2: A very bad improvement I just used ggplot to create the label and merge with patchwork library(patchwork) p$data %>% ggplot(aes(1, y= y, label = label )) + geom_text(size=2) + xlim(NA, 1) + theme_classic() + theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) -> adText pp + adText
The answer according #xiangpin at GitHub. Big offset value to geom_tiplabel: p <- ggtree(beast_tree) p1 <- gheatmap( p, genotype, colnames=TRUE, colnames_angle=-45, colnames_offset_y = 5, colnames_position = "bottom", width=0.3, hjust=0, font.size=2) + scale_fill_manual(breaks=c("HuH3N2", "pdm", "trig"), values=c("steelblue", "firebrick", "darkgreen"), name="genotype") + geom_tiplab(align = TRUE, linesize=0, offset = 7, size=2) + xlim_tree(xlim=c(0, 36)) + scale_y_continuous(limits = c(-1, NA)) p1 Using ggtreeExtra: library(ggtreeExtra) library(ggtree) library(treeio) library(ggplot2) beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree") genotype_file <- system.file("examples/Genotype.txt", package="ggtree") tree <- read.beast(beast_file) genotype <- read.table(genotype_file, sep="\t") colnames(genotype) <- sub("\\.$", "", colnames(genotype)) genotype$ID <- row.names(genotype) dat <- reshape2::melt(genotype, id.vars="ID", variable.name = "type", value.name="genotype", factorsAsStrings=FALSE) dat$genotype <- unlist(lapply(as.vector(dat$genotype),function(x)ifelse(nchar(x)==0,NA,x))) p <- ggtree(tree) + geom_treescale() p2 <- p + geom_fruit(data=dat, geom=geom_tile, mapping=aes(y=ID, x=type, fill=genotype), color="white") + scale_fill_manual(values=c("steelblue", "firebrick", "darkgreen"), na.translate=FALSE) + geom_axis_text(angle=-45, hjust=0, size=1.5) + geom_tiplab(align = TRUE, linesize=0, offset = 6, size=2) + xlim_tree(xlim=c(0, 36)) + scale_y_continuous(limits = c(-1, NA)) p2
Rmarkdown displays unwanted output on pdf starting with ##
I have been using Rmarkdown for some time. I recently was generating my pdf output from Rmarkdown. I used the ggplot function from ggplot2 and sapply to plot charts. While the charts got populated perfectly fine, There were bunch of other details that got plotted on the pdf. Starting with ##, multiple lines with detail on panel plots data color size got printed. I am not sure how to exclude them from my plot. Is this related to Rmarkdown or sapply. I tried various options like echo=false but had no success. `sapply(unique(turnDatC$vintage),function(x) { tmpflt <- filter(turnDatC,vintage == x) sapply(unique(tmpflt$net_coupon),function(y) { df1 <-filter(tmpflt,(net_coupon == y)) %>% select(product,as_of_date,vintage, net_coupon,cltv_bkt,variable,value) %>% group_by(product,vintage,net_coupon,cltv_bkt,variable) %>% filter(length(value)>1) %>% ungroup() if(nrow(df1)>1) { print(x) print(y) plot(df1%>% ggplot(aes(x = as_of_date,y = value,color = variable,group = variable)) + geom_line() + geom_point() + scale_x_date(breaks = date_breaks("months"),labels = date_format("%Y/%m")) + xlab("TimeSeries") + ylab("CPR") + facet_grid(cltv_bkt ~ .) + theme(legend.position = "bottom", legend.title = element_blank(), axis.text.x = element_text(angle = 90, hjust = 1)) + ggtitle(paste0("Vintage - ",x, " Coupon - ",y , " CLTV across Time Series")))} })`