0
votes

I want to save the image of a plot as png-file; however it cuts off the axis-labels. So I tried leaving a greater margin by setting "omi" and "mar" but it does not seem to work. Adding more pixels does not work either. A smaller value for cex.lab would make the axis visible but unreadably small.

Probably the mistake is silly, yet I cannot figure it out...

png(filename = "CarTheftForeigners.png",
width = 1120, height = 646, units = "px", pointsize = 12,
bg = "white", res = NA, family = "", restoreConsole = TRUE,
type = c("windows", "cairo", "cairo-png")
)
plot(Table$Car.Theft,Table$Foreigners, 
 type="p", pch=20, col="red",
 main="Correlation of the Rate of Car Theft and Rate of Foreigners", cex.main=2, 
 xlab="Rate of Car Thefts",
 ylab="Rate of Foreigners", cex.lab=2,
 par(omi=c(1,1,1,1)+0.1))
abline(reg=lm(Foreigners ~ Car.Theft, data=Table),col="blue")
dev.off()

No matter what values I add for omi or mar, I always get the error:

Fehler in plot.window(...) : ungültiger 'xlim' Wert

Which is German and means that the xlim-value is invalid.

Thank you for your help!

1
The reason for the somewhat cryptic error message is that plot was trying to match the value from par(omi=c(1,1,1,1)+0.1) to the first unmatched parameter value in plot.default's list of arguments, which was xlim in this case.IRTFM

1 Answers

1
votes

You cannot call par() inside plot. Try issuing the par call before plot.

This succeeds:

png(filename = "CarTheftForeigners.png",
    width = 1120, height = 646, units = "px", pointsize = 12)
  par(omi=c(1,1,1,1)+0.1)
  plot(1:10, 1:10)
dev.off()

This does not:

png(filename = "CarTheftForeigners.png",
     width = 1120, height = 646, units = "px", pointsize = 12);
plot(1:10, 1:10 , type="p",  par(omi=c(1,1,1,1)+0.1))
#   Error in plot.window(...) : invalid 'xlim' value
dev.off()

However, it you really wanted to use the par call inside plot() you could have named the value as omi which would prevent the incorrect positional matching that occurred.

png(filename = "CarTheftForeigners.png",
     width = 1120, height = 646, units = "px", pointsize = 12);
   plot(1:10, 1:10 , type="p",  omi = par(omi=c(1,1,1,1)+0.1))
dev.off()