The legend window width is incorrect when using the par parameter

I have a problem that my legend is too big, my code is:

par(mfrow=c(1,2)) hist(alvsloss,breaks = 100, freq=F,main="Histogramm, density curve (gaussian kernel) \n and fitted normal distribution of Allianz simple losses ",xlim=c(-0.15,0.15),xlab="loss",ylab="density",cex.axis=1.2,cex.lab=1.2) lines(density(alvsloss), col="black", lwd=2) curve(dnorm(x, mean = mean(alvsloss), sd = sd(alvsloss)), add=TRUE, col="black",lwd=2,lty="dotted") legend(-0.155, 30, c("(Gaussian) Kernel density","fitted normal distribution"),lwd=2, cex=0.8, col=c("black","black"), lty=1:2) qqnorm(alvsloss,main="normal QQ Plot",cex.axis=1.2,cex.lab=1.2) qqline(alvsloss) 

This gives the following picture:

Graphs

The problem is that the legend on the left is too big, how can I control the width of the window? The box is too big.

can be found here: http://uploadeasy.net/upload/ocafq.rar

+7
source share
1 answer

The white space on your right legend tells me that you manually expanded the chart window. Legends do not scale well when it comes to manual calibration.

The solution opens the exact size chart that you need before plotting. On Windows, this is done using windows(width=10, height=8) . Units are in inches. Now the surrounding box should be more dense with the text.

If this is still unsatisfactory, you should try:

  • Decrease font size for cex=0.7
  • Removing the border around the legend bty = "n" and using \n to split your legend into several lines
  • You can put your legend even more to the left using "topleft" instead of coordinates

Here is how I would do it:

 legend("topleft", legend=c("(Gaussian)\nKernel\ndensity","Fitted\nnormal\ndistribution\n"), bty = "n",lwd=2, cex=0.7, col=c("black","black"), lty=1:2) 

enter image description here

+8
source

All Articles