0

One of the features of the R plotting machinery that I use more often is png(file=..., width=..., height=..., res=...).

Normally, I set precise values within the function, e.g. png("out.png", height=1500, width=2500, res=250).

I am now making a tool that makes an automatic plot from the provided dataset, but the tool is agnostic in respect to the number of rows it has to show in the plot, i.e. it plots what it receives. Sometimes, the plot has a large white area around it. Some other times, the canvas is too small and some rows fall outside.

I'm trying to fix this by calculating height and width according to the number of rows in the dataframe, but I find this approach error prone and suboptimal.

For example, inkscape has a nice function called "Resize page to drawing or selection" which will resize the canvas to match the boundaries of the plot. You can even pass a certain tolerance value so that your plot will still have some white around it.

Does R have this possibility, perhaps within ggsave() if not within png()?

Omniswitcher
  • 368
  • 3
  • 13
schmat_90
  • 572
  • 3
  • 22
  • 1
    I'm not understanding the issue here. Somehow your plot size is related to the number of rows in a dataset? In vanilla ggplot2, a plot does not have a size until it is drawn. The benefit of this is that it dynamically adapts to the device window, but the downside is that it isn't predictable. – teunbrand Aug 05 '22 at 11:18
  • I want the plot size to adjust to the size of what is drawn, without me specifying height and width. – schmat_90 Aug 06 '22 at 13:02

0 Answers0