admin管理员组文章数量:1348135
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")
```
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")
```
Share
Improve this question
asked 2 days ago
MohanMohan
8,9875 gold badges40 silver badges61 bronze badges
2
|
2 Answers
Reset to default 0In 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.
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
- Save ggplot as SVG (Vector graphic):
ggsave("plot.svg", plot = p, width = 12, height = 12)
astemp_file
- 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)
本文标签: rHow do I change plot size in RStudio without making plots blurryStack Overflow
版权声明:本文标题:r - How do I change plot size in RStudio without making plots blurry? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743845018a2548960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{r fig.width=6.5, fig.height=6, dpi=300}
– margusl Commented 2 days agofig.retina
doesn't seem to work. – Mohan Commented 2 days ago