How can I recreate the image resolution & quality shown in R4DS?

Hi, I'm generating R Notebooks and have been frustrated by how the figures, by default, are generally low resolution. When I increase the resolution using the chunk options fig.width and fig.height, the resolution is increased but the fonts don't scale.

I figured I would try and recreate the image output from R4DS to see how it was possible for the author to generate images with a reasonable resolution. As you can see from the first figure in the 'graphics for communication' section, the image is 1344px by 830px.

If I take that same code, plop it into an R notebook, and preview I get an image that is 583x347. If I increase the figure width and height by using the chunk options fig.width and fig.height, I get a higher resolution image but the fonts don't scale.

Can anyone indicate how Hadley Wickham was able to create such high resolution images without affecting the fonts?

The .Rmd code I'm using is here:

If you set the dpi (dots per inch) chunk option to a high resolution, say, dpi=600, does that result in higher-resolution plot images? This should work independent of the physical plot dimensions.

If it can helps, the code of the book is on Github

No, including dpi=300 doesn't affect the Preview'd notebook. I updated the gist to include that test.

Thanks. I looked through the source code and didn't see anything that would impact figure output. The chapter in question is here

And the knitr options are here:

I tried applying those knitr options with no effect.

I'm not sure if I'm understanding your issue correctly but, as far as I know, R Notebooks preview is always in low resolution for fast rendering, but when you actually knit the file you get the desired resolution.

1 Like

Thanks! Knitting solved it. Is this documented anywhere? Can that be configured?

I'm not sure why rendering at a low resolution would be the default. The image quality is terrible and with modern PC's I don't see why rendering at ~1000px would be much slower than ~500px

I suppose it depends on your point of view, for me it's perfectly intuitive since the functionality is called "preview" and you don't need too much resolution in order to preview a plot.

Not every RStudio user uses a modern PC, just to give you a reference, a bunch of people complained when RStudio Desktop for windows became 64-bit only.

1 Like

For reference, I think I found this behavior documented here:

Plots : Plots emitted from a chunk are rendered to match the width of the editor at the time the chunk was executed. The height of the plot is determined by the golden ratio. The plot’s display list is saved, too, and the plot is re-rendered to match the editor’s width when the editor is resized.

You can use the fig.width , fig.height , and fig.asp chunk options to manually specify the size of rendered plots in the notebook; you can also use knitr::opts_chunk$set(fig.width = ..., fig.height = ...) in the setup chunk to to set a default rendered size. Note, however, specifying a chunk size manually suppresses the generation of the display list, so plots with manually specified sizes will be resized using simple image scaling when the notebook editor is resized.

However, I don't see how one could improve the image quality in-editor or in-Preview. My goal of making 'nicer looking' figures is only half of my concern. The other half is that lower-resolution figures are more difficult to interpret. When the image is pixelated, it becomes hard to compare points or lines in a figure.

This doesn't sounds right, I agree the previews have low image quality but it has never been an issue for me in that regard (or to such extent), maybe you have a different problem, do you happen to be using a high-dpi monitor? There are precedents of undesired behaviors with RStudio on high-dpi screens.

I think we may need to agree to disagree. I don't think it's a controversial statement to say that 'lower resolution figures are more difficult to interpret'. This isn't in response to a technical issue (I have access to both retina and non-retina monitors), it's just a statement of fact.

500px is not a lot of pixels and figures can get very busy depending on the content and domain.

Well, if you feel so strongly about their design choices on this matter, the way to go would be to file an issue on the github repo explaining your concerns to the developers so they can take it into consideration.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.