You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改R语言robfilter绘图的图例位置并移除自动生成图例?

解决robfilter绘图图例位置问题

针对你遇到的plot(res)自动生成图例无法移除、新增legend()会出现双图例的问题,有两种简洁的解决方式:

方法一:直接利用plot函数的内置参数调整位置

robfilter包的plot.robfilter函数本身支持指定图例位置,不需要额外添加legend()。只需要在调用plot()时传入legend.pos参数,设置为topleft即可将默认图例移到左上角:

library(robfilter)
res <- robust.filter(cow, width=7)
# 指定图例位置为左上角
plot(res, legend.pos = "topleft")
ind <- which(res$ol!=0)
points(ind, cow[ind])

方法二:关闭自动图例后手动添加

如果你的包版本不支持legend.pos参数,可以先关闭自动生成的图例,再手动绘制自定义图例:

library(robfilter)
res <- robust.filter(cow, width=7)
# 关闭自动图例
plot(res, legend = FALSE)
ind <- which(res$ol!=0)
points(ind, cow[ind])
# 手动添加左上角图例,内容对应原默认图例的元素(可根据实际显示调整)
legend("topleft", 
       legend = c("原始数据", "过滤后数据", "异常值"),
       col = c("black", "red", "blue"),
       lty = c(1, 1, NA),
       pch = c(NA, NA, 1))

内容的提问来源于stack exchange,提问作者lowerCamelCase

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 18:32:02