r - 很难将图例添加到我的图表中

标签 r legend

我尝试了多种方法向底部两个图表添加图例,但由于某种原因,它要么给出错误,要么不显示图例。

这是我的代码:

lg <- function(x, a = 1, b = 1){
  exp(a+b*x) / (1+exp(a+b*x))
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
for (b in c(1:5)){
  curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
for (a in c(1:5)){
  curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
}

我在代码中放置图例的位置是否有问题?

最佳答案

legend 函数放在新行中,前面不加逗号。 以及 plot 的右括号:

plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y") 
legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
     for (b in c(1:5)){
         curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
     }

enter image description here

plot2 相同

plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y") 
legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
     for (a in c(1:5)){
         curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
     }

enter image description here

关于r - 很难将图例添加到我的图表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69107722/

相关文章:

r - 第一个参数不是开放的 RODBC channel

r - 使用 purrr::map_dfr 从值而不是名称中分配 id 值

css - 为什么 Firefox 4 对字段集图例的定位绝对不同于其他浏览器?

flutter - 如何自动换行charts_flutter 图例?

python - 当 'a' 和 'b' 离平均值太远时,Python 和 R 中的法线被截断

r - 将 Rle 对象的 S4 DataFrame 转换为 R 中的稀疏矩阵

r - 在 A 列中查找大于 0 的第一个值的位置,并在 B 列中的该位置返回值

r - 使用 gghighlight 时为多个 geom_hlines 创建图例

r - 如何使用 ggplot 在箱线图中包含平均值的图例符号?

javascript - 从 Leaflet 中的图例中删除 ""