0

When I run the chunk below in RStudio, I get a very blurry plot. Removing fig.width=6.5, fig.height=6 gives me back a nice, crisp plot. I've tried AGG, Cairo and Cairo PNG backends, and also switched on antialiasing.

How can I change the figure size in RStudio without making the plot blurry? [NB. I know I can save a crisp PNG to a file, but I'm looking to have a readable plot in the IDE.]

```{r fig.width=6.5, fig.height=6}
ggplot(data=data.frame()) + 
    scale_x_continuous(name = "rMZ", limits = c(-0.05, 1.15), breaks = seq(0,1,0.25)) +
    scale_y_continuous(name = "rDZ", limits = c(-0.05, 1.05), breaks = seq(0,1,0.25)) +
    coord_fixed() +
    # clockwise and anticlockwise arrows
    annotate(geom = "curve", 
      x = 1.03, y = 0.1, xend = 0.65, yend = 0.88,
      arrow = arrow(length = unit(0.05, "npc"), ends="both", type="closed"),
      linewidth = 1.8,
      curvature = 0.18
    ) +
    annotate("text", x = 0.61, y = 0.93, angle = 43, label="MORE SHARED\nENVIRONMENT", size = 3, fontface = "bold") +
    annotate("text", x = 1.03, y = 0.03, angle = 7, label="MORE GENETIC\nINTERACTION", size = 3, fontface = "bold")
```

enter image description here

2
  • 2
    For RMarkdown inline plots? Default DPI should be 72, have you tried higher values? {r fig.width=6.5, fig.height=6, dpi=300} Commented Apr 2 at 10:05
  • @margusl Yes, but I run into the issues in stackoverflow.com/questions/58379468/… (plot becomes huge). Fiddling with fig.retina doesn't seem to work. Commented Apr 2 at 10:10

2 Answers 2

0

In the "Plots" panel you have the "zoom" option, which detachs the plot window and allows you to visualize it full-screen. Usually, the resolution I get with this process is enough for my purposes. If you want to inspect the plot in the IDE, that's a good solution.

Additionally, if you want to quickly export the file, you can just take a screenshot of the full-screen plot.

This is your image, as an example.
I just ran "ggplot(data=data.frame()) + ..." without even specifying fig.width and fig.height.

Sign up to request clarification or add additional context in comments.

Comments

0

I came up with a little workaround:

If the rmarkdown is not knitted, knitr::opts_knit$get("rmarkdown.pandoc.to") will be NULL, in this case

  1. Save ggplot as SVG (Vector graphic): ggsave("plot.svg", plot = p, width = 12, height = 12) as temp_file
  2. Use print(knitr::include_graphics(temp_file)) to display the svg-file in the chunk output within the RStudio IDE

else if rmarkdown.pandoc.to is given as HTML / PDF, then just use the normal plotting function.

Code

---
title: "Crisp Plot"
output: html_document
date: "2025-04-02"
---

```{r echo=FALSE, message=FALSE, warning=FALSE}
library(ggplot2)

p <- ggplot(data = data.frame()) + 
  scale_x_continuous(name = "rMZ", limits = c(-0.05, 1.15), breaks = seq(0, 1, 0.25)) +
  scale_y_continuous(name = "rDZ", limits = c(-0.05, 1.05), breaks = seq(0, 1, 0.25)) +
  coord_fixed() +
  # clockwise and anticlockwise arrows
  annotate(geom = "curve", 
           x = 1.03, y = 0.1, xend = 0.65, yend = 0.88,
           arrow = arrow(length = unit(0.05, "npc"), ends = "both", type = "closed"),
           linewidth = 1.8,
           curvature = 0.18) +
  annotate("text", x = 0.61, y = 0.93, angle = 43, label = "MORE SHARED\nENVIRONMENT", size = 3, fontface = "bold") +
  annotate("text", x = 1.03, y = 0.03, angle = 7, label = "MORE GENETIC\nINTERACTION", size = 3, fontface = "bold")

if(is.null(knitr::opts_knit$get("rmarkdown.pandoc.to"))){
  size <- 12 # adjust as needed
  temp_file <- tempfile(fileext = ".svg")
  ggsave(temp_file, plot = p, width = size, height = size)
  print(knitr::include_graphics(temp_file))
  unlink(temp_file)
}else{
  p
}

```

which looks very crisp (The limiting factor here being the resolution of the snipping tool / screen)

out

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.