r - R - 将圆圈添加到现有的时间序列图中
问题描述
我正在尝试将一个圆圈添加到lattice
time-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")
})
关于如何使它工作的任何想法?
解决方案
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.points
。 p1
是从上面。
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)
推荐阅读
- javascript - 如何让我的 Javascript 待办事项列表中的任务出现在新的列表项上?
- java - Java调度程序在一段时间后不工作
- javascript - 从 JS 生成 html
- python - 我在 jupyter notebook 上制作了一个与 python 一起使用的 Twitter API,但是当我要求搜索推文时,它没有返回任何内容
- r - R - 匹配不同开始和结束条件的子集行
- python - python中的len函数。已申报,但在 -1 上需要帮助
- sql - 将日期设置为每个 ID 的最小日期并更新
- php - 如何更改此原始语句以执行相反的操作
- python - after_cancel 在 python tkinter 中不起作用
- sharepoint - 指向 SharePoint 2019 文档集的 Word 超链接不起作用