如何修改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
相关产品推荐
相关产品推荐

