如何在R中的现有晶格图上添加新点

has*_*ISC 0 plot r lattice trellis

我使用了点阵包装绘制线图。

library(lattice)  
xyplot(price~month,groups=perc,data=Edf,type='l',
       main="Percentile chart of OpRes Charge Rates Forcast", 
       ylab="OpRes Charge Rate ($/MWh)", xlab="Months",ylim=c(0,40),auto.key=TRUE)
Run Code Online (Sandbox Code Playgroud)

然后,我想在现有绘图上添加一些点。

points(rep(1,length(OpResWestJan)),OpResWestJan) 
Run Code Online (Sandbox Code Playgroud)

OpResWestJan 是向量,但点从未出现在现有图中,也没有警告。

fde*_*sch 6

为了完整起见,这里是一个可复制的示例。只需将创建xyplot的变量存储在变量中,然后update与自定义panel函数一起使用即可添加其他点。

library(lattice)

## create scatterplot
p <- xyplot(1:10 ~ 1:10)

## insert additional points
update(p, panel = function(...) {
  panel.xyplot(...)
  panel.xyplot(1:10, 10:1, pch = 4, col = "orange")
})
Run Code Online (Sandbox Code Playgroud)

散点图

或者,你也可以创建一个第二xyplot,并使用as.layerlatticeExtra将其添加到您最初的情节。

library(latticeExtra)

## create second scatterplot and add it to first plot
p2 <- xyplot(10:1 ~ 1:10, pch = 4, col = "orange")
p + as.layer(p2)
Run Code Online (Sandbox Code Playgroud)

或者,按照@Pascal的建议,layer与一起使用panel.points可以实现您的目标。

p + layer(panel.points(1:10, 10:1, pch = 4, col = "orange"))
Run Code Online (Sandbox Code Playgroud)