聽起來像您可能希望將數據幀從寬轉換爲長格式,並將所有布爾列收集到一個列中。例如:使用
library(dplyr)
library(tidyr)
df %>%
gather(subset.variable, logic, -date, -value) %>%
filter(logic) %>%
ggplot(aes(date, value)) +
geom_point() + # using geom_point for illustration
facet_wrap(~subset.variable)
![plot](https://i.stack.imgur.com/znRLu.png)
的樣本數據:
set.seed(123)
n = 200
df <- data.frame(
date = seq.Date(from = as.Date("1999-01-01"),
to = as.Date("1999-01-01") + n - 1,
by = 1),
value = rpois(n, lambda = 2),
x1 = sample(c(TRUE, FALSE), n, replace = T),
x2 = sample(c(TRUE, FALSE), n, replace = T),
x3 = sample(c(TRUE, FALSE), n, replace = T),
x4 = sample(c(TRUE, FALSE), n, replace = T),
x5 = sample(c(TRUE, FALSE), n, replace = T),
x6 = sample(c(TRUE, FALSE), n, replace = T)
)
> head(df)
date value x1 x2 x3 x4 x5 x6
1 1999-01-01 1 TRUE FALSE TRUE TRUE TRUE FALSE
2 1999-01-02 3 FALSE TRUE FALSE TRUE FALSE FALSE
3 1999-01-03 2 FALSE FALSE TRUE TRUE TRUE TRUE
4 1999-01-04 4 FALSE FALSE TRUE TRUE FALSE FALSE
5 1999-01-05 4 TRUE TRUE TRUE TRUE FALSE TRUE
6 1999-01-06 0 FALSE TRUE FALSE FALSE TRUE FALSE
完美!奇蹟般有效。謝謝 – user2969402