首页 > 解决方案 > R - 将圆圈添加到现有的时间序列图中

问题描述

我正在尝试将一个圆圈添加到latticetime-series的特定位置xyplot。我的情节是:

library(zoo)
library(lattice)

t <- structure(list(date = structure(c(18172, 18177, 18182, 18187, 
                                          18192, 18202, 18212, 18217, 18222, 18237, 18257, 18267, 18287, 
                                          18322, 18327, 18332, 18337, 18342, 18347, 18357, 18362, 18367, 
                                          18372, 18377, 18382, 18392, 18402, 18407, 18412, 18432, 18437, 
                                          18447, 18452, 18457, 18462, 18467, 18477, 18482, 18497, 18502
), class = "Date"), t = c(0.22582148400414, 0.256991369867836, 
                             0.20566669217573, 0.197370049842565, 0.277943312725968, 0.409366098650766, 
                             0.485328298701375, 0.265923063666776, 0.193433942553932, 0.146475290734094, 
                             0.261228272794155, 0.287337189727423, 0.431631481918686, 0.555856286432998, 
                             0.500582779759492, 0.406387635091313, 0.270854099747563, 0.327326988684063, 
                             0.302588934307361, 0.249693446719906, 0.305548452947743, 0.397038410635602, 
                             0.439170248751657, 0.46303881959878, 0.488322795840136, 0.509185404897871, 
                             0.55092532581109, 0.551910236346757, 0.591181074665548, 0.648661902423056, 
                             0.430176528691085, 0.405420937495388, 0.437875812057808, 0.391051426411378, 
                             0.375546279814988, 0.397580900426823, 0.3510990639662, 0.196213067375209, 
                             0.188679707217845, 0.190000000000123)), row.names = c(NA, -40L
                             ), class = "data.frame")
ts <- read.zoo(t, format = "%Y-%m-%d")

xyplot(ts, col="darkgreen", lwd=2)

在此处输入图像描述

现在,我想添加一个以 . 的第 12 个元素为中心的圆ts。另外,我可以绘制它(有点):

xyplot(ts[12][[1]] ~ ts[12], pch = 1, col = "red", cex=10)

在此处输入图像描述

但是当我尝试像这样更新主图时什么也没有发生:

p <- xyplot(ts, col="darkgreen", lwd=2)

## insert additional circle
update(p, panel = function(...) {
  panel.xyplot(...)
  panel.xyplot(ndvi_ts[12], ndvi_ts[12][[1]], pch=19, cex=10, col="red")
})

关于如何使它工作的任何想法?

标签: rzoolattice

解决方案


1) as.layer将第二个图定义为要添加到第一个图的图层,如下所示:

library(latticeExtra)   

p1 <- xyplot(ts, col="darkgreen", lwd=2)
p2 <- xyplot(ts[12], type = "p", col = "red", cex = 10)

p1 + as.layer(p2)

(剧情后续) 截屏

2) 层第二种方法是使用layer面板调用而不是as.layer格子对象。 p1是从上面。

library(latticeExtra)

p1 + layer(panel.points(x[12], y[12], col = "red", cex = 10))

3) trellis.focus第三种方法是使用trellis.focus:。 p1是从上面。

p1
trellis.focus()
panel.points(ts[12], cex = 12, col = "red")
trellis.unfocus()

4)更新面板问题中的代码很接近,但第二个panel.xyplot应该是panel.pointsp1是从上面。

update(p1, panel = function(...) {
  panel.xyplot(...)
  panel.points(ts[12], cex=10, col="red")
})

5) autoplot.zoo这可以通过 ggplot 使用autoplot.zoo

library(ggplot2)
autoplot.zoo(ts) + 
  geom_point(aes(x = time(ts)[12], y = ts[12]), pch = 1, col = "red", cex = 10)

6)经典图形 使用经典图形:

plot(ts)
points(ts[12], col = "red", cex = 10)

推荐阅读