我希望 2014 年 (2013) 的滞后值显示在 2014 年分面包装中,包括 2013 年分面的相应颜色。
这是我的代码:
ggplot() +
geom_sf(data=st_centroid(table1) ,aes(color=as.factor(year)),size=0.5) +
theme_void() +
facet_wrap(~year,ncol=4)
我尝试创建第二个数据集并将其与前一个数据集进行比较,但它不起作用:
table1 <- ERF %>% select(year,geometry) %>%
filter(year>=2013,year<=2014)
table2 <- ERF %>% select(year,geometry) %>%
filter(year>=2013,year<=2014) %>%
mutate(year=year-1)
ggplot() +
geom_sf(data=st_centroid(table1) ,aes(color=as.factor(year)),size=0.5) +
geom_sf(data=st_centroid(table2), aes(color=as.factor(year)) ,size=0.5) +
theme_void() +
facet_wrap(~year,ncol=4)
这里是数据:https://drive.google.com/file/d/1W4z3wPuDzW8dInWx6_xPrEfyhk12XXBc/view?usp=share_link
您可以将数据绑定到侧面移动一年的版本。在这里,我区分了
year
(数据的原始年份)和 facet_year
(我想在其中查看它的方面)。
df <- data.frame(year = c(2013, 2014),
val = c(6,7))
首先,像你的例子:
ggplot(df, aes("", val, color = factor(year))) +
geom_point() +
facet_wrap(~year)
现在,将数据绑定到其自身的一个版本已经向前发展了。现在,2014 年方面也显示 2013 年的数据。
library(dplyr)
bind_rows(df |> mutate(facet_year = year),
df |> mutate(facet_year = year + 1)) |>
ggplot(aes("", val, color = factor(year))) +
geom_point() +
facet_wrap(~facet_year)