rggplot2visual-studio-code

How do I conveniently resize a ggplot2 plot on R+VSC?


I'm in the process of learning R (I'm still a newcomer on SO as well). Being very used to using Visual Studio Code, I decided to choose that over RStudio or RStudio Cloud.

One of the best parts of RStudio was that plots automatically resized/reshaped themselves if we resized the right pane. Moreover, in the tutorials I watched, plots involving map data automatically rendered in the correct aspect ratio (as seen on physical maps).

I replicated the code to make my own plot of the world map. Unfortunately it rendered as a square shape, and resizing the right pane does not affect its shape:

Am I missing any commonly used VSC extensions which can make plots resizeable like in RStudio? (I've installed only the most downloaded extension for R, by Yuki Ueda)

If not, can I modify my code to specify the exact dimensions I need the plot to have?

Thanks in advance!


Solution

  • You can specify the dimensions when you save the file. I usually use (approximately) the golden ratio:

    ggsave("./earth.png", width = 16, height = 10)
    

    The ggsave function reference explains how you can change units - options are c("in", "cm", "mm", "px").