In a Rmarkdown document, when I use ggplot to plot data (a map in my case) and label points with geom_text_repel from the ggrepel package, the labels disappear when I open the plot in the Rstudio: Notebook output (show in new window). I would like to be able to quickly have a look at my labeled plot and enlarge it with the notebook output view.
Is that a bug in ggrepel, ggplot, Rmarkdown or Rstudio? Can I get rid of it?
I am using Rstudio Version 1.1.453, R version 3.6.2 under Linux Mint. The error persists with Rstudio Version 1.2.5033
Package versions:
> packageVersion("ggplot2")
[1] ‘3.2.1’
> packageVersion("ggrepel")
[1] ‘0.8.1’
> packageVersion("sf")
[1] ‘0.8.1’
> packageVersion("rnaturalearth")
[1] ‘0.1.0’
> packageVersion("rnaturalearthdata")
[1] ‘0.1.0’
> packageVersion("rmarkdown")
[1] ‘2.1’
> packageVersion("knitr")
[1] ‘1.28’
MWE:
---
title: "Test"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library("ggplot2")
library("ggrepel")
library("sf")
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
label <- c(30, 38,39,132-2)
latitude <- c(-55.8,-58.7,-59.3,-52.6)
longitude <- c(-71.3, -68.4,-67.2,-75.5)
df <- data.frame(latitude, longitude, label)
p <- ggplot(data = world) +
geom_sf() +
coord_sf() +
geom_point(data = df, aes(x = longitude, y = latitude))+
geom_text_repel(data = df, aes(x = longitude, y = latitude, label = label))
show(p)
```
"Update" A similar, maybe related (?) behaviour happens, when I want to draw a grid grob with grid.draw. This only happens within output view of the chunks (and related "show in new window" option) rmarkdown files. When I run the same code from an R script, the Viewer-Windows shows the output just as expected.
This (both) seem to be a Rstudio bug related to Rmarkdown documents and specifically the output-view of chunks.