0

I'm using RStudio version 1.4.1717 on a Windows 10 system.

When I use the code below, working within an rmd file, to render a plot in RStudio it appears fine. But when I knit the rmd file (in this case into a Word docx, though I've encountered the same issue knitting it into pdf) by simply clicking Knit within RStudio, the text in the title is cut off. (The caption fits in both versions of the plot, but in the past I've had the same issue with that too, as well as with legends being cut off.) Compare the two images below.

Plot rendered within RStudio

Plot rendered when rmd knitted from RStudio to word docx

I could iteratively adjust the title text size in order to make sure it is not cut off when knitted, but since I actually have dozens of figures and a very large rmd file (for a ~40 page masters thesis) it would be very time consuming to fix this 'manually' for each figure.

Does anyone know why this issue is happening, and what I might be able to do so that the plot that is in the knitted document is exactly the same as the plot rendered within RStudio?

Thank you!

```
---
title: "temp"
output: word_document
---

```{r data and scatter plot, echo=FALSE}
library(ggplot2)
library(dplyr)
df <- data.frame(c("a",1),
                 c("b",5),
                 c("c",2),
                 c("d",3),
                 c("e",6),
                 c("f",10)) %>% 
  data.table::transpose() %>% mutate(V2 = as.numeric(V2))


ggplot(df, aes(x=V1, y=V2)) + 
    geom_point() +
    guides(fill = guide_legend(title = "Industry")) +
   xlab("Made up labels") +
  ylab("Made up data") +
  labs(title = "Made up title that fits when this is created within RStudio with zero problems",
       caption = "Made up caption that fits when this is created within RStudio")
  
  
```
JacobG
  • 1
  • 1
  • 1

1 Answers1

0

A quick fix to this would be to do something like this

ggplot(df, aes(x=V1, y=V2)) + 
    geom_point() +
    guides(fill = guide_legend(title = "Industry")) +
   xlab("Made up labels") +
  ylab("Made up data") +
  labs(title = "Made up title that fits when this is created within\n RStudio with zero problems",
       caption = "Made up caption that fits when this is created within RStudio") 

Which will just break up the title into two lines. The other option if this is just a one-off is to throw the lines that are getting cut off into subtitle

ggplot(df, aes(x=V1, y=V2)) + 
    geom_point() +
    guides(fill = guide_legend(title = "Industry")) +
   xlab("Made up labels") +
  ylab("Made up data") +
  labs(title = "Made up title that fits when this is created within",
       subtitle = "RStudio with zero problems",
       caption = "Made up caption that fits when this is created within RStudio") 


If you anticipate lots and lots of instances where this is the case then putting it into a function is the way to go. Here is a very minimal example but you can build it up to be as complex as you want!

theme_thesis = function(title_pos = "left"){
  ggplot2::theme_bw(base_size = 8)
  
  
}

ggplot(df, aes(x=V1, y=V2)) + 
    geom_point() +
    guides(fill = guide_legend(title = "Industry")) +
   xlab("Made up labels") +
  ylab("Made up data") +
  labs(title = "Made up title that fits when this is created within RStudio with zero problems",
       caption = "Made up caption that fits when this is created within RStudio")  + 
theme_thesis()


Josh Allen
  • 161
  • 1
  • 5
  • Thank you! I also realized another workaround (based on this answer: https://stackoverflow.com/questions/63551701/input-html-table-from-file-into-r-markdown-knit-to-word/71693885#71693885) is to save the plot as an image and then knit it into the document using: `ggsave("plot_file.png", plot) knitr::include_graphics("plot_file.png") ` – JacobG Apr 04 '22 at 18:44
  • No worries! To avoid tons of `ggsaves()` you might add something like ``````{r opts, echo = FALSE} knitr::opts_chunk$set( fig.path = "images/" ) ``` – Josh Allen Apr 04 '22 at 18:56