3

I used to run R.3.0.2 on my desktop (Ubuntu 14.04.2 LTS) and have just recently installed a newer version of R (R.3.3.2). For this new version, I found there is an image output issue. First, if I save a picture in JPEG or PNG format, using the new version always generates a picture that is not as sharp as if using the old version. The two pictures were generated by a same chunk of code, the only difference is what version of R that I am using to run the code. The texts in the picture are also less clear using the new version. The JPEG/PNG file generated by the new version R also has a smaller size. Second, If I use RMarkdown to output a html that has the plot embedded, the plot resolution is consistently less clear using the new version R. After I uploaded the html generated by the new version R to github gh-pages, the embedded plot can't be displayed when I visit the webpage site hosted by github (a small question mark would appear in the middle of where the plot was supposed to be), though the plot is totally visible if I open the local html. I have been struggling with this for long time and can't figure out. I think other people who upgrade R may encounter this same issue. So I am posting this question here, hoping someone could help me out. I greatly appreciate any suggestions.

The issue arises basically for any code that outputs a plot or saves a plot in JPEG/PNG.

For example

jpeg("test.jpg")
hist(rnorm(100))
dev.off()

if run in RMarkdown, just a hist(rnorm(100)) would output a image that is not sharp enough in local html and not visible in github webpages.

yuwen liu
  • 31
  • 3

1 Answers1

0

Although it appears as maybe the quality changed between versions of R (and probably their corresponding packages), you can control and specify the resolution of the image using the quality argument in jpeg().

jpeg("test-default.jpg", quality = 75)  # Default quality as percentage
hist(rnorm(100))
dev.off()

jpeg("test-hi-res.jpg", quality = 100)
hist(rnorm(100))
dev.off()

See more options with ?jpeg

You can also opt for a different image type, which may give you a clearer plot.

png("test-default.png", width = 6, height = 6, units = 'in', res = 300)
hist(rnorm(100))
dev.off()

See https://stackoverflow.com/a/9556253/6873133

Community
  • 1
  • 1
Eric Leung
  • 2,354
  • 12
  • 25