英文:
How to create a continuous geom_line across a facet boundaries in ggplot2 over the time series data?
问题
I can provide a translation of the code portion you provided without addressing your specific request for a plot. Here's the translated code:
我正在编写一个 ggplot 代码。我有一个以 tsibble 格式表示的数据集 `AirPassenger`,我想创建一个分面网格图,其中包括四个线图,显示观测数据、趋势、季节和不规则成分。我已经成功使用以下代码创建了这个图:
library(fpp3)
library(tsibble)
library(ggplot2)
library(scales)
library(zoo)
# 载入数据
pr1 <- AirPassengers
# 将时间序列转换为 tsibble 格式
pr1_tsibble <- as_tsibble(pr1)
# 应用 X-13ARIMA-SEATS 模型并执行分解
fit <- pr1_tsibble %>%
model(X_13ARIMA_SEATS(value ~ transform(`function` = "none") + x11(mode = "add"))) %>%
components()
# 提取各成分
index <- pr1_tsibble$index
data <- pr1_tsibble$value
trend <- fit$trend
seasonal <- fit$seasonal
irregular <- fit$irregular
# 将成分与原始数据合并
combined <- data.frame(index, data, trend, seasonal, irregular)
library(dplyr)
combined <- combined %>%
mutate(year = as.factor(format(index, "%Y")))
combined$index <- as.Date(combined$index, format = "%Y%b")
ggplot(combined, aes(x = index)) +
geom_line(aes(y = data, color = "观测值")) +
geom_line(aes(y = trend, color = "趋势")) +
geom_line(aes(y = seasonal, color = "季节")) +
geom_line(aes(y = irregular, color = "不规则")) +
xlab('') + ylab('') +
theme_light() +
scale_color_manual(
values = c("观测值" = "grey", "趋势" = "black", "季节" = "green", "不规则" = "blue"),
name = ''
) +
scale_y_continuous(
labels = function(x) format(x, nsmall = 2)
) +
scale_x_date(labels = NULL, breaks = NULL,
date_minor_breaks = "1 month") +
facet_wrap(~year, nrow = 1, scales = 'free_x',
strip.position = 'bottom') +
theme(panel.spacing.x = unit(0, "lines"),
panel.grid = element_blank(),
legend.position = "top",
strip.background = element_rect(colour = "white", fill = "grey"))
Please note that this is just a translation of the code, and I haven't addressed the specific plot issue you mentioned.
英文:
I'm working on a ggplot code. I have a dataset AirPassenger
in tsibble format and I want to create a facet grid plot with four line plots showing the observed data, trend, seasonal, and irregular components. I have managed to create the plot using the following code:
library(fpp3)
library(tsibble)
library(ggplot2)
library(scales)
library(zoo)
# Load the data
pr1<-AirPassengers
# Convert time series to tsibble format
pr1_tsibble <- as_tsibble(pr1)
# Apply X-13ARIMA-SEATS model and perform decomposition
fit <- pr1_tsibble %>%
model(X_13ARIMA_SEATS(value ~ transform(`function` = "none") + x11(mode = "add"))) %>%
components()
# Extract components
index <- pr1_tsibble$index
data <- pr1_tsibble$value
trend <- fit$trend
seasonal <- fit$seasonal
irregular <- fit$irregular
# Combine components with the original data
combined <- data.frame(index, data, trend, seasonal, irregular)
library(dplyr)
combined <- combined %>%
mutate(year = as.factor(format(index, "%Y")))
combined$index<-as.Date(combined$index, format= "%Y%b")
ggplot(combined, aes(x = index)) +
geom_line(aes(y = data, color = "Observed")) +
geom_line(aes(y = trend, color = "Trend"))+
geom_line(aes(y = seasonal, color = "Seasonal")) +
geom_line(aes(y = irregular, color = "Irregular"))+
xlab('') + ylab("") +
theme_light() +
scale_color_manual(
values = c("Observed" = "grey", "Trend" = "black", "Seasonal" = "green", "Irregular" = "blue"),
name = ""
) +
scale_y_continuous(
labels = function(x) format(x, nsmall = 2)
)+
scale_x_date(labels = NULL, breaks = NULL,
date_minor_breaks = "1 month") +
facet_wrap(~year, nrow=1, scales= 'free_x',
strip.position = 'bottom')+
theme(panel.spacing.x = unit(0, "lines"),
panel.grid = element_blank(),
legend.position = "top",
strip.background = element_rect(colour = "white", fill = "grey"))
It produces the plot :
What I am expecting in x-axis labelling ..
The current plot is almost what I need, but I want the line plots to be connected across the facet grid. Currently, each line plot is disconnected between years. How can I modify the code to achieve a continuous geom_line across the facet grid?
Any help or suggestions would be greatly appreciated!
答案1
得分: 2
geom_vline(xintercept = yrs, alpha = 0.2) +
annotate("rect", xmin = yrs[1:(length(yrs)-1)],
xmax = yrs[2:length(yrs)], ymin = -150, ymax = -100,
fill = "gray90", color = "gray70") +
annotate("text", x = yrs[1:(length(yrs)-1)] + 182, label = 1949:1960,
y = -125) +
英文:
Faking the labels without facets:
geom_vline(xintercept = yrs, alpha = 0.2) +
annotate("rect", xmin = yrs[1:(length(yrs)-1)],
xmax = yrs[2:length(yrs)], ymin = -150, ymax = -100,
fill = "gray90", color = "gray70") +
annotate("text", x = yrs[1:(length(yrs)-1)] + 182, label = 1949:1960,
y = -125) +
答案2
得分: 2
你可以通过在x轴上设置expand = c(0, 0)
来使用facets来实现这个目标。您需要将1月1日的值复制到前一年的12月31日,以确保曲线连续。虽然这可能有点像作弊,但请记住,每个facet上的像素远少于365个,所以这不会影响图表的外观:
combined %>%
filter(lubridate::month(index) == 1) %>%
mutate(index = index - 1,
year = factor(as.numeric(as.character(year)) - 1)) %>%
bind_rows(combined) %>%
filter(year != "1948") %>%
ggplot(aes(x = index)) +
geom_line(aes(y = data, color = "Observed")) +
geom_line(aes(y = trend, color = "Trend"))+
geom_line(aes(y = seasonal, color = "Seasonal")) +
geom_line(aes(y = irregular, color = "Irregular"))+
labs(x = NULL, y = NULL) +
theme_light() +
scale_color_manual(NULL,
values = c("Observed" = "grey", "Trend" = "black",
"Seasonal" = "green", "Irregular" = "blue")) +
scale_y_continuous(labels = ~format(.x, nsmall = 2)) +
scale_x_date(labels = NULL, breaks = NULL, expand = c(0, 0),
date_minor_breaks = "1 month") +
facet_wrap(~year, nrow = 1, scales= 'free_x',
strip.position = 'bottom') +
theme(panel.spacing.x = unit(0, "lines"),
panel.grid = element_blank(),
legend.position = "top",
strip.background = element_rect(colour = "white", fill = "grey"))
<details>
<summary>英文:</summary>
You _can_ do this with facets by setting `expand = c(0, 0)` in your x axis. You will need to copy over the 1st of January values to 31st December the previous year to ensure the lines are continuous. Although this may feel a bit like cheating, remember that there are far fewer than 365 pixels across each facet, so this will not effect the look of the plot:
```r
combined %>%
filter(lubridate::month(index) == 1) %>%
mutate(index = index - 1,
year = factor(as.numeric(as.character(year)) - 1)) %>%
bind_rows(combined) %>%
filter(year != "1948") %>%
ggplot(aes(x = index)) +
geom_line(aes(y = data, color = "Observed")) +
geom_line(aes(y = trend, color = "Trend"))+
geom_line(aes(y = seasonal, color = "Seasonal")) +
geom_line(aes(y = irregular, color = "Irregular"))+
labs(x = NULL, y = NULL) +
theme_light() +
scale_color_manual(NULL,
values = c("Observed" = "grey", "Trend" = "black",
"Seasonal" = "green", "Irregular" = "blue")) +
scale_y_continuous(labels = ~format(.x, nsmall = 2)) +
scale_x_date(labels = NULL, breaks = NULL, expand = c(0, 0),
date_minor_breaks = "1 month") +
facet_wrap(~year, nrow = 1, scales= 'free_x',
strip.position = 'bottom') +
theme(panel.spacing.x = unit(0, "lines"),
panel.grid = element_blank(),
legend.position = "top",
strip.background = element_rect(colour = "white", fill = "grey"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论