Legend disappaers when plotting in R

2019-07-23 21:23发布

问题:

I have plotted five graphs and a legend. The graphs work just fine, however the legens disappears without an error. My preview in RStudio looks like this

When I zoom in, the area where the legend should be is blank. I use the following code:

opar <- par (no.readonly = TRUE)
par (mfrow = c(3, 2))

library(deSolve)

# Plot A
LotVmod <- function (Time, State, Pars) {
    with(as.list(c(State, Pars)), {
        dx = (b*x) - (b*x*x/K) - (y*(x^k/(x^k+C^k)*(l*x/(1+l*h*x))))
        dy = (y*e*(x^k/(x^k+C^k)*(l*x/(1+l*h*x)))) - (m*y)
        return(list(c(dx, dy)))
    })
}

Pars <- c(b = 1.080, e = 2.200, K = 130.000, k = 20.000, l = 2.000, 
          h = 0.030, C = 2.900, m = 0.050)

State <- c(x = 0.25, y = 2.75)  
Time <- seq(1, 9, by = 1)

out <- as.data.frame(ode(func = LotVmod, y = State, parms = Pars, times = Time))

matplot(out[,-1], type = "l", xlim = c(1, 9), ylim = c(0, 45),  
        xlab = "time", 
        ylab = "population",
        main = "Compartment A")
mtext ( "Coefficient of Variance 4.96", cex = 0.8 )

x <- c(# Validation data)
y <- c(# Validation data)

lines (Time, x,  type="l", lty=1, lwd=2.5, col="black") 
lines (Time, y, type="l", lty=1, lwd=2.5, col="red")

# Legend
plot.new()
legend("center", c(expression (italic ("F. occidentalis")*" observed"), 
                   expression (italic ("M. pygmaeus")*" observed"), 
                   expression (italic ("F. occidentalis")*" simulated"),
                   expression (italic ("M. pygmaeus")*" simulated")),
       lty = c(1, 1, 1, 2), 
       col = c(1, 2, 1, 2), 
       lwd = c(2.5, 2.5, 1, 1),
       box.lwd = 0, bty = "n")

# Plot C to F = same as A

par(opar)

My output doesn't give an error. I have used the exact same code before without any trouble, thus I restarted R, removed all objects, cleared all plots and restarted both RStudio and my computer.

回答1:

Try to add xpd=TRUE in your legend statement. I.e.

legend("center", c(expression (italic ("F. occidentalis")*" observed"), 
                   expression (italic ("M. pygmaeus")*" observed"), 
                   expression (italic ("F. occidentalis")*" simulated"),
                   expression (italic ("M. pygmaeus")*" simulated")),
       lty = c(1, 1, 1, 2), 
       col = c(1, 2, 1, 2), 
       lwd = c(2.5, 2.5, 1, 1),
       box.lwd = 0, bty = "n", xpd=TRUE)

By default, the legend is cut off by the plotting region. This xpd parameter enables plotting outside the plot region. See e.g. ?par for more on xpd.



回答2:

This is due to how the plot canvas is set up and how rescaling that device works. The way you do it, you add the legend in the plotting region of the top right plot. The plotting region is however not the complete device, but only the part inside the space formed by the axes. If you rescale, that plotting region will be rescaled as well. The margins around the plotting region don't change size though, so zooming in makes your plotting region so small that it doesn't fit the legend any longer. It is hidden by the margins around the plotting region.

For that reason AEBilgrau is very right you need to add xpd = TRUE. This allows the legend to extend outside of the plotting region, so it doesn't disappear behind the margins when resizing the plotting device.



标签: r plot legend