gpt4 book ai didi

r - 在 R 中绘图时图例消失

转载 作者:行者123 更新时间:2023-12-04 03:05:51 25 4
gpt4 key购买 nike

我绘制了五张图和一个图例。这些图表工作得很好,但是图例毫无错误地消失了。 My preview in RStudio looks like this

当我放大时,图例所在的区域是空白的。我使用以下代码:

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 和我的计算机。

最佳答案

尝试在您的图例语句中添加 xpd=TRUE。即

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 的更多信息。

关于r - 在 R 中绘图时图例消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44568360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com