-1

As a beginner on R I manage to produce correct relational graphs thanks to R. They are about correct in the visualization space but of very bad quality when I export them in PDF or in JPEG / PNG. The image is not centered, a part of the legend is missing, the graph is very small or blurred etc.

How do you proceed for the export?

Thanks in advance!

I am looking for the right handling or settings to export the graph visualizations produced in R?

I understood that I had to set up the viewer space but via the code but have no idea how to do it...

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
NA_
  • 21
  • 4
  • 1
    It's not clear exactly how you are creating your images. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. How are you currently creating your output files? – MrFlick Oct 31 '22 at 17:21
  • 2
    Can you post the code you have tried? To export to PDF or JPEG, see `help("pdf")` and `help("jpeg")` or [here](https://bookdown.org/ndphillips/YaRrr/saving-plots-to-a-file-with-pdf-jpeg-and-png.html). – Rui Barradas Oct 31 '22 at 17:21
  • 1
    R creates excellent publication quality graphs. It they ar of "bad quality" the original poster (OP) does something wrong. I would recommend to use a search engine to google for `export graphs from R`. You fill find several web pages and YouTube videos. In addition, just follow the advice of @Rui Barradas and @MrFlick. – tpetzoldt Oct 31 '22 at 17:26

1 Answers1

1

Here an example how to write a plot to a PNG file. The plot "commands" are embedded in png(....)and dev.off(). Several options are available to configure size and resolution.

png("myfile.png", width=1600, height=1200, res=300)  # good for LaTeX or Word
#png("myfile.png", width=800, height=600, res=150)   # good for Powerpoint or Impress
plot(iris$Sepal.Length, iris$Petal.Length)
dev.off()

Some hints:

  • width and height are given in pixels
  • res influences nominal resolution and font size (play with it)
  • use at least 300 dpi (dots per inch). For centimeters, the number of pixels = 300/2.54 * width in cm
  • professionals use 600 or even 1200 pixels per inch, but then .docx and .pptx files will dramatically increase
  • 1600 x 1200px is good for 13.3 x 10 cm size in the printed document

If you work with LaTeX, it is in most cases better to use PDF for the figures. Another very good idea is to use Markdown for the text. Then, figures are automatically embedded.

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
  • Okay, thank you very much. This is more or less the same as what I used to do in "button-click" from my Studio interface... But I wonder if the problem doesn't come from the fact that my graph and the legend don't adapt according to the size of the "display window", when I go from the preview to the Zoom and then to the export, the graph stays at the same size (too small and blurred) and the legend is cut off, as if I had made a bad quality screenshot – NA_ Nov 01 '22 at 09:56
  • 1
    If you just click to export, you 1st may have lower resolution and 2nd RStudio adds another level of complexity with rescaling. The method above is R's "official" way. It would be very helpful to see a reproducible example, or at least the used packages. There are some packages around with different ways of plotting. – tpetzoldt Nov 01 '22 at 17:12