繁体   English   中英

ggplot2 灰度方案:澄清变化点图的建议

[英]ggplot2 grey-scale-schemes: suggestions for clarifying changepoint plot

我可以使用 ggplot2 绘制多个同时发生变化点和状态的时间序列,并且我可以使用颜色使状态清晰(使用geom_rect以不同颜色绘制不同部分)。 我需要制作一个情节,在不使用颜色的情况下清楚地说明制度的位置。 对于三种状态,可以区分使用白色、黑色和灰色作为阴影的状态,但如果存在三个以上的状态,则很难区分它们。

我已经举了一个我可以使用颜色制作的情节的例子,如果有人可以建议一个不使用颜色就传达相同信息的情节,我将不胜感激。

library(ggplot2)
library(scales)
# generate 3 time series and store them in a data frame
generate_cp_ts <- function(tau, params) {
    ts(c(arima.sim(model = list(ar = 0.2), n = tau[1], rand.gen = function(n) params[1] * rnorm(n)), arima.sim(model = list(ar = 0.2), n = tau[2] - tau[1], rand.gen = function(n) params[2] * rnorm(n)), arima.sim(model = list(ar = 0.2), n = tau[3] - tau[2], rand.gen = function(n) params[3] * rnorm(n)), arima.sim(model = list(ar = 0.2), n = tau[4] - tau[3], rand.gen = function(n) params[4] * rnorm(n))))
}
tau <- 100 * (1:4)
ts1 <- generate_cp_ts(tau, c(1.7, 0.3, 1.7, 1.7))
ts2 <- generate_cp_ts(tau, c(0.3, 2, 0.3, 0.9))
ts3 <- generate_cp_ts(tau, c(2, 2, 0.1, 0.7))
tsframe <- data.frame(ts = c(ts1, ts2, ts3), ts_level = factor(paste("Time Series", rep(1:3, each = 400))), time = rep(1:400, 3))
# Work out which colors are needed to color the plot and store in a data frame
CPs <- c(0, tau)
colour.frame <- data.frame(regime.from = rep(CPs[-length(CPs)], each = 3), regime.to = rep(CPs[-1], each = 3), ts_level = factor(paste("Time Series", rep(c(1:3), length(CPs) - 1))), regime = factor(c(0,0,0, 1,1,0, 0,0,1, 0,2,2) + 1))
# Plotting
qplot(x = time, y = ts, data = tsframe, facets = ts_level ~ ., alpha = I(1), geom = "line", ylab = "Time Series", xlab = "Time") +
    geom_rect(aes(NULL, NULL, xmin = regime.from, xmax = regime.to, fill = regime), ymin = -6, ymax = 6, data = colour.frame) +
    scale_fill_manual(values = alpha(c("blue", "red", "green"), 0.2))

由上述代码生成的图http://i.stack.imgur.com/A1MQm.jpg

创建colour.frame您可以插入以下代码:

tdf <- colour.frame

tdf$xval <- (tdf$regime.from + tdf$regime.to)/2
tdf$yval <- max(tsframe$ts) * 0.8 # if 0.8 is higher (0.9) then the text is set higher 

ggplot(tsframe, aes(x = time, y = ts)) + 
  geom_line() + 
  facet_grid(ts_level~.) + 
  geom_vline(xintercept = CPs) + # maybe play around with linetype
  geom_text(aes(x = xval, y = yval, label = regime), data = tdf)

这给出了这个情节: 阴谋

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM