Ggplots missing titles and labels in PDF Rmarkdown


#1

Hi everyone.

I can't seem to find the answer to my question anywhere.
I have a whole bunch of code to produce various plots for my boss.
When I knit to an HTML it works fine and all the plots show up as they are in R.
When I try to knit to PDF the plots show up fine, but the title and axis labels are missing.

Here is an example of a plot code chunk:

``` {r message=FALSE, echo=FALSE, warning=FALSE}
#GGplot of trend ;
ggplot(dat, aes(migration$Date, Trend)) + geom_line(size=1.2, colour="dodgerblue3") + xlab("Year") + ylab("Number of People (Net)") + 
  ggtitle("Net Long Term Migration (Arrivals - Departures) Australia - 1976-2018") +

  theme(text=element_text(size=14, family="Calibri"), plot.title=element_text(hjust=0.5)) + (scale_x_date(limits=startend))
```

I have attached a side-by-side image of the in R/HTML output and the PDF output.

Also my setup code is as follows:

---
title: ""
author: ""
date: ""
output:
  word_document: default
  pdf_document:
    fig_caption: yes
    fig_crop: no
---

Any help would be much appreciated!!


#2

Can you provide a reproducible example? Or at least the chunk generating your plot - a guess is that it has something to do with the chunk options.


#3

I added in the code for that graph above. The options I have on that chunk are:
message=FALSE, echo=FALSE, warning=FALSE

Does that help?

Thanks


#4

Sorry - I wasn't very clear. I mean't the entire chunk with options and everything. The info provided doesn't help yet. Can you reproduce the issue on a smaller Rmarkdown document and provide the entire script?


#5

Okay I have written this little code to demonstrate:

---
title: "Untitled"
author: "Author"
date: "3 August 2018"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r include=FALSE}
library(ggplot2)
library(rmarkdown)

```

Migration 

``` {r include=FALSE}
xforgraph <- c("1995-01-01","1996-01-01","1997-01-01","1998-01-01","1999-01-01","2000-01-01",
               "2001-01-01","2002-01-01","2003-01-01","2004-01-01","2005-01-01","2006-01-01",
               "2007-01-01","2008-01-01","2009-01-01","2010-01-01","2011-01-01","2012-01-01",
               "2013-01-01","2014-01-01","2015-01-01","2016-01-01","2017-01-01","2018-01-01","2019-01-01","2020-01-01")
yforgraph <- c(10,13,17,21,28,26,35,36,42,40,43,59,61,65,64,68,75,78,79,80,83,
               86,88,99,100,101)
xy <- data.frame(xforgraph, yforgraph)

xy$xforgraph <- as.Date(xy$xforgraph, "%Y-%m-%d")

```

``` {r message=FALSE, echo=FALSE, warning=FALSE}
#GGplot of trend ;
ggplot(xy, aes(xforgraph, yforgraph)) + geom_line(size=1.2, colour="dodgerblue3") + xlab("X Label") + ylab("Y Label") + 
  ggtitle("Sample Data") +

  theme(plot.title=element_text(hjust=0.5))
```


#6

Thanks! - Unfortunately, I wasn't able to reproduce what you see. Both my html and pdf look fine.

Can you provide the results of running sessionInfo()?


#7

Hi,

Sorry for the late reply.
I actually managed to figure it out - in the original code I had changed the text family and I think it must not be supported in the pdf format as taking that out of the code enabled it to work. In the sample above I hadn't added it in and it worked.

theme(text=element_text(size=14, family="Calibri")

Thank you for your help