R ggplot2 rmd returning an empty html - r

Based on the code and data below, the rmd knitted fine initially with the code and output (default option), but then I just changed the chunk option to show output only. And, since then I am getting an empty html. I tried reverting back to default, I even cleared the environment and restarted RStudio, but it does not fix it.
What could be causing the issue?
Code + data:
---
title: "Gantt Chart"
author: "Ed"
date: "2022-08-12"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(include = FALSE)
library(tidyverse)
library(plotly)
```
```{r Gantt Chart, echo=FALSE}
df = data.frame(task=c("Explore Ideas", "Finalize Idea", "Make Plots with\n geom_line", "Make Plots with\n Plan Package", "Add Writeups", "Refine Plots", "Review Tutorial", "Submit Tutorial", "Get Feedback\n and Update"),
start=c("2019-10-15", "2019-10-18", "2019-10-19", "2019-10-22", "2019-10-24", "2019-10-25", "2019-10-26", "2019-10-27", "2019-10-28"),
end=c("2019-10-18", "2019-10-19", "2019-10-24", "2019-10-26", "2019-10-27", "2019-10-27", "2019-10-28", "2019-10-28", "2019-10-31"),
owner=c("Harish", "Phani", "Harish", "Phani", "Phani", "Harish", "Phani", "Harish", "Phani"))
head(df)
df = df %>%
mutate(start = as.Date(start), end = as.Date(end))
df_tidy = df %>%
gather(key=date_type, value=date, -task, -owner)
gantt_plot = ggplot() +
geom_line(data=df_tidy, mapping=aes(x=fct_rev(fct_inorder(task)), y=date, color=owner), size=10) +
geom_hline(yintercept=as.Date("2019-10-27"), colour="black", linetype="dashed") +
coord_flip() +
scale_y_date(date_breaks = "1 day") +
labs(title="Community Contribution Gantt Chart",
x = "Task",
y = "Date",
colour = "Owner") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90),
panel.grid.minor = element_line(colour="white", size=0.5),
legend.position="right",
plot.title = element_text(hjust = 0.5))
ggplotly(gantt_plot)
```

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

Figures alternating when produced through a loop with markdown

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

plots not showing in flexdashboard tabs

When I render the flexdashboard the plotly chart only shows on the first tab, and not on the remaining tabs. The reproducible example below uses the flexdashboard example for .tabset use. I am running R 3.6.1
I have used plotly and tabset in the past, and I am not sure why it is not working now.
---
title: "Tabset Column"
output: flexdashboard::flex_dashboard
---
Column
-------------------------------------
### Chart 1
```{r}
library(ggplot2)
library(plotly)
library(dplyr)
da <-data.frame(ns=rep(0:1,6), month=factor(1:12), a=letters[1:12])
p <- ggplot(da) +
geom_bar(aes(x=month,y=ns,fill=ns),stat = "identity") +
facet_wrap(~a )+
theme_bw(12) + ylab("CYER") +
scale_fill_viridis_c(option="D",direction = -1)
p2 <- ggplot(da) +
geom_bar(aes(x=month,y=ns,fill=ns),stat = "identity") +
facet_wrap(~a )+
theme_bw(12) + ylab("CYER") +
scale_fill_viridis_c(option="C",direction = -1)
```
Column {.tabset}
-------------------------------------
### Chart 2
```{r}
ggplotly(p2)%>%layout(
margin = list(b = 100, l = 100,r = 150))
```
### Chart 3
```{r}
ggplotly(p)%>%layout(
margin = list(b = 100, l = 100,r = 150))
```

Rmarkdown to knit html - graphs not showing

I have the following lines of code:
ggplot(data = subset(gapminder1, year %in% c(seq(1900, 1990, by = 10))), aes(x = year, y = lifeExp)) + geom_boxplot(aes(group = year))
ggplot(subset(gapminder1,country=="United States"),aes(x = year, y = lifeExp)) + geom_line()
ggplot(subset(gapminder1,country %in% c("China","India","United States","Indonesia","Brazil")) , aes(x = year, y = lifeExp)) + geom_line(aes(color=country))
The graphs show up fine in the rmd file when I run the code. However, when I knit the document the graphs do not show up (a blank graph comes up). Can anyone tell me what I could do?
Please see the code below knitted into HTML file:
---
title: "check"
author: "Artem"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
Check graph
```{r gapminder, echo=FALSE}
gapminder <- read.csv("https://raw.githubusercontent.com/birdsarah/pydata-nc/master/tutorial/assets/gapminder.csv", header = TRUE)
gapminder1 <- setNames(gapminder, c(c("country", "year", "lifeExp", "population", "income", "region"
)))
library(ggplot2)
gpm_sub <- subset(gapminder1, country %in% c("China","India","United States","Indonesia","Brazil"))
g1 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) +
geom_line(aes(color=country))
gpm_sub_us <- subset(gapminder1,country=="United States")
g2 <- ggplot(gpm_sub_us,aes(x = year, y = lifeExp)) +
geom_line()
g3 <- ggplot(gpm_sub, aes(x = year, y = lifeExp)) +
geom_line(aes(color=country))
library(gridExtra)
grid.arrange(g1, g2, g3, nrow = 2)
```
Output (No problems experienced).:

A Kableextra table and a ggplot plot on same row in Rmarkdown (PDF - not Flexdashboard)

I have been experimenting with R Markdown to create some PDF reports. I am having difficulty in getting the layout right. Basically, I need to have a KableExtra created table (dataframe) and a ggplot plot on the same row. I have explored some grid packages, but couldn't get it to work.
Here is my code:
---
title: "Untitled"
author: ""
date: "14 June 2018"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(reshape2)
library(dplyr)
library(kableExtra)
```
## R Markdown
```{r chart, echo=FALSE}
Years <- c("2016","2016","2016","2016",
"2017","2017","2017","2017")
Quarters <- c("Q1","Q2","Q3","Q4",
"Q1","Q2","Q3","Q4")
Series1 <- c("100","200","300","400","500","600","700","800")
Series1 <- as.numeric(Series1)
df <- data.frame(Years,Quarters, Series1)
library(ggplot2)
ggplot(df) +
geom_point(aes(x = Quarters, y = Series1)) +
facet_wrap( ~ Years, strip.position = "bottom",scales = "free_x") +
theme(panel.spacing = unit(0,"lines"), strip.background =
element_blank(),
strip.placement = "outside")
```
```{r table, echo=FALSE}
Table <- dcast(df, Years ~ Quarters, fun.aggregate = sum, value.var =
"Series1")
Table <- Table %>%
kable(format = "latex", caption = "Balances", booktabs = TRUE) %>%
kable_styling(latex_options = c("striped","hold_position","condensed"),
font_size = 10)
Table
```
If you are not strongly depending on kable() I can provide this gridExtra solution. When using tableGrob(kable(.)) the latex code won't be executed somehow, maybe somebody else comes up with how to execute latex code within a tableGrob().
```{r chart, echo=FALSE, message=FALSE}
df <- data.frame(Years=rep(2016:2017, each=4),
Quarters=rep(paste0("Q", 1:4), 2),
Series1=seq(100, 800, 100))
library(ggplot2)
p1 <- ggplot(df) +
geom_point(aes(x=Quarters, y=Series1)) +
facet_wrap( ~ Years, strip.position="bottom", scales="free_x") +
theme(panel.spacing=unit(0, "lines"),
strip.background=element_blank(),
strip.placement="outside",
aspect.ratio=1) # set aspect ratio
Table <- dcast(df, Years ~ Quarters, fun.aggregate=sum, value.var="Series1")
library(gridExtra)
t1 <- tableGrob(Table, theme=ttheme_minimal(), rows=NULL) # transform into a tableGrob
grid.arrange(p1, t1, nrow=1)
```
Produces:

Resources