0

When I create a plot from a normal R script, click Export and Copy to Clipboard... I get a nice sized plot which works well to paste in a presentation. The size in the Copy Plot to Clipboard plot shows Width of 1065 and Height of 652. See below -

enter image description here

However when I create the sample plot in an rmarkdown and knit, I get something like this - enter image description here

How can I play with plot settings in rmarkdown to produce an output like the first screenshot?

The Rookie
  • 877
  • 8
  • 15
  • Have a look at this question. You can play with plot size for each chunk or set a default plot size for every chunk. https://stackoverflow.com/q/17963962/12400385 – nniloc Sep 15 '22 at 21:46

1 Answers1

0

Hi there so you should stop using the console interface totally for this. You want more control over the size of plots than that can offer you. Also doing it the way you do it offers no means to autogenerate plots, what happens when you need to run a loop and make 100 images or even just like 10 or 20? That's a lot of unnecessary manual labor

Here's how you can do this....

dev.off() # this clears the plotting function
jpeg("filename", width = 100, hieght = "100") # note you can use png or
# other commands, this creates an image file for you to call a plot function on
plot(data) # any plotting function may be called here, ggplots pheatmap etc..
dev.off() # this causes R to save the image of the plot to file 

Controlling image size in Rmarkdown, there is documentation on how to do this which is located here: https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html

I recommend doing what I showed above, supplying the code in markdown but not necessarily running it every time you knit the file. Instead its better to make the plot you want once then load it into markdown from a saved location so you can fiddle with the size easier. It will save you a ton of time running the same computation unnecessarily multiple time each time you knit. Again supply the code you used to make the plot in the Rmarkdown for completeness sake but don't actually run it. Making an Rmarkdown file will require you to knit multiple times repeatedly as you fiddle with the settings on your images and the text itself. Best to avoid running computation as much as possible, especially if your plots take a while to generate (heatmaps for instance can be a little heavy).

Julian
  • 6,586
  • 2
  • 9
  • 33
Angus Campbell
  • 563
  • 4
  • 19