R plot: size and resolution
Asked Answered
C

3

80

I have stacked into the question: I need to plot the image with DPI=1200 and specific print size.

By default the png looks ok... enter image description here

png("test.png",width=3.25,height=3.25,units="in",res=1200)
par(mar=c(5,5,2,2),xaxs = "i",yaxs = "i",cex.axis=1.3,cex.lab=1.4)
plot(perf,avg="vertical",spread.estimate="stddev",col="black",lty=3, lwd=3)
dev.off()

But when I apply this code, the image became really terrible it's not scaling (fit) to the size that is needed. What did I miss? How to "fit" the image to the plot?

enter image description here,

Cothran answered 6/12, 2011 at 11:22 Comment(4)
As a starter, reduce the values of cex.axis and cex.labSupernaturalism
You might want to adjust the pointsize parameter of png as this seems to scale with res.Hickox
pointsize - really helps, but the size of the axis names are really small (almost invisible)Cothran
@Cothran You might need to play around with it to trade off between legibility and amount of the plot canvas that these elements consumeHickox
S
96

A reproducible example:

the_plot <- function()
{
  x <- seq(0, 1, length.out = 100)
  y <- pbeta(x, 1, 10)
  plot(
    x,
    y,
    xlab = "False Positive Rate",
    ylab = "Average true positive rate",
    type = "l"
  )
}

James's suggestion of using pointsize, in combination with the various cex parameters, can produce reasonable results.

png(
  "test.png",
  width     = 3.25,
  height    = 3.25,
  units     = "in",
  res       = 1200,
  pointsize = 4
)
par(
  mar      = c(5, 5, 2, 2),
  xaxs     = "i",
  yaxs     = "i",
  cex.axis = 2,
  cex.lab  = 2
)
the_plot()
dev.off()

Of course the better solution is to abandon this fiddling with base graphics and use a system that will handle the resolution scaling for you. For example,

library(ggplot2)

ggplot_alternative <- function()
{
  the_data <- data.frame(
    x <- seq(0, 1, length.out = 100),
    y = pbeta(x, 1, 10)
  )

ggplot(the_data, aes(x, y)) +
    geom_line() +
    xlab("False Positive Rate") +
    ylab("Average true positive rate") +
    coord_cartesian(0:1, 0:1)
}

ggsave(
  "ggtest.png",
  ggplot_alternative(),
  width = 3.25,
  height = 3.25,
  dpi = 1200
)
Sherrer answered 6/12, 2011 at 13:38 Comment(4)
thank's a lot! your solution works great! But I wonder - why the ylab,xlab have reduced it's size?Cothran
Nice, didn't know you could use ggsave like that. Very handy.Lawannalawbreaker
Love the ggplot solution (+1). For ggsave though, it seems that width/height doesn't do very much and rather it is dpi setting that controls the size. On my Windows machine, I get a 3.25" square by setting dpi=108. The dpi=1200 setting gives an enormous image. Was 1200 a typo?Outsoar
@AssadEbrahim The plot you create will be width * dpi pixels wide and height * dpi pixels high. How large the image appears onscreen depends on your viewing software. If it is smart, it will recognise the intended width and height, and rescale the image to appear at the appropriate size. If not, it will display a very large image. Note that 1200 dpi only really makes sense when you want to print an image to paper: monitor resolutions don't go that high, but photo printers do.Sherrer
A
5

If you'd like to use base graphics, you may have a look at this. An extract:

You can correct this with the res= argument to png, which specifies the number of pixels per inch. The smaller this number, the larger the plot area in inches, and the smaller the text relative to the graph itself.

Audieaudience answered 27/1, 2017 at 12:3 Comment(0)
S
4

An alternate solution to lowering the size of the various components with pointsize and the cex functions is to increase the size of the graph to compensate. This maintains the scale by increasing the size of everything instead of only some components. Your graph will be larger when exported, but will retain the improved resolution if manually decreased in size should you wish to retain the original smaller size.

The png default settings are dpi=72, height=480, width=480. So to maintain the same scale, you need to multiply height and width by the resolution/72. Using your example of width = height = 3.25 inches and a desired resolution dpi of 1200, we will adjust by 1200/72 (equal to 50/3):

reso <- 1200
length <- 3.25*reso/72
png("test.png",units="in",res=reso,height=length,width=length)
par(mar=c(5,5,2,2),xaxs = "i",yaxs = "i",cex.axis=1.3,cex.lab=1.4)
plot(perf,avg="vertical",spread.estimate="stddev",col="black",lty=3, lwd=3)
dev.off()
Shiri answered 21/6, 2022 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.