[英]R - add circles to existing time-series plot
我正在嘗試將圓圈添加到lattice
時間序列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)
現在,我想添加一個以ts
的第 12 個元素為中心的圓。 另外,我可以繪制它(有點):
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這可以通過使用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.