在R中绘图时图例消失 [英] Legend disappaers when plotting in R

查看:565
本文介绍了在R中绘图时图例消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我画了五张图和一个图例.这些图工作正常,但是图例消失却没有错误. 我在RStudio中的预览看起来像这样

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)

我的输出没有给出错误.我之前使用的代码完全相同,没有任何问题,因此我重新启动了R,删除了所有对象,清除了所有图并重新启动了RStudio和我的计算机.

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.

推荐答案

尝试在图例语句中添加xpd=TRUE.即

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)

默认情况下,图例被绘图区域切断.此xpd参数允许在绘图区域之外进行绘图.参见例如?par有关xpd的更多信息.

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.

这篇关于在R中绘图时图例消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆