如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

huangapple go评论93阅读模式
英文:

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:

  1. 我正在编写一个 ggplot 代码。我有一个以 tsibble 格式表示的数据集 `AirPassenger`,我想创建一个分面网格图,其中包括四个线图,显示观测数据、趋势、季节和不规则成分。我已经成功使用以下代码创建了这个图:
  2. library(fpp3)
  3. library(tsibble)
  4. library(ggplot2)
  5. library(scales)
  6. library(zoo)
  7. # 载入数据
  8. pr1 <- AirPassengers
  9. # 将时间序列转换为 tsibble 格式
  10. pr1_tsibble <- as_tsibble(pr1)
  11. # 应用 X-13ARIMA-SEATS 模型并执行分解
  12. fit <- pr1_tsibble %>%
  13. model(X_13ARIMA_SEATS(value ~ transform(`function` = "none") + x11(mode = "add"))) %>%
  14. components()
  15. # 提取各成分
  16. index <- pr1_tsibble$index
  17. data <- pr1_tsibble$value
  18. trend <- fit$trend
  19. seasonal <- fit$seasonal
  20. irregular <- fit$irregular
  21. # 将成分与原始数据合并
  22. combined <- data.frame(index, data, trend, seasonal, irregular)
  23. library(dplyr)
  24. combined <- combined %>%
  25. mutate(year = as.factor(format(index, "%Y")))
  26. combined$index <- as.Date(combined$index, format = "%Y%b")
  27. ggplot(combined, aes(x = index)) +
  28. geom_line(aes(y = data, color = "观测值")) +
  29. geom_line(aes(y = trend, color = "趋势")) +
  30. geom_line(aes(y = seasonal, color = "季节")) +
  31. geom_line(aes(y = irregular, color = "不规则")) +
  32. xlab('') + ylab('') +
  33. theme_light() +
  34. scale_color_manual(
  35. values = c("观测值" = "grey", "趋势" = "black", "季节" = "green", "不规则" = "blue"),
  36. name = ''
  37. ) +
  38. scale_y_continuous(
  39. labels = function(x) format(x, nsmall = 2)
  40. ) +
  41. scale_x_date(labels = NULL, breaks = NULL,
  42. date_minor_breaks = "1 month") +
  43. facet_wrap(~year, nrow = 1, scales = 'free_x',
  44. strip.position = 'bottom') +
  45. theme(panel.spacing.x = unit(0, "lines"),
  46. panel.grid = element_blank(),
  47. legend.position = "top",
  48. 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:

  1. library(fpp3)
  2. library(tsibble)
  3. library(ggplot2)
  4. library(scales)
  5. library(zoo)
  6. # Load the data
  7. pr1&lt;-AirPassengers
  8. # Convert time series to tsibble format
  9. pr1_tsibble &lt;- as_tsibble(pr1)
  10. # Apply X-13ARIMA-SEATS model and perform decomposition
  11. fit &lt;- pr1_tsibble %&gt;%
  12. model(X_13ARIMA_SEATS(value ~ transform(`function` = &quot;none&quot;) + x11(mode = &quot;add&quot;))) %&gt;%
  13. components()
  14. # Extract components
  15. index &lt;- pr1_tsibble$index
  16. data &lt;- pr1_tsibble$value
  17. trend &lt;- fit$trend
  18. seasonal &lt;- fit$seasonal
  19. irregular &lt;- fit$irregular
  20. # Combine components with the original data
  21. combined &lt;- data.frame(index, data, trend, seasonal, irregular)
  22. library(dplyr)
  23. combined &lt;- combined %&gt;%
  24. mutate(year = as.factor(format(index, &quot;%Y&quot;)))
  25. combined$index&lt;-as.Date(combined$index, format= &quot;%Y%b&quot;)
  26. ggplot(combined, aes(x = index)) +
  27. geom_line(aes(y = data, color = &quot;Observed&quot;)) +
  28. geom_line(aes(y = trend, color = &quot;Trend&quot;))+
  29. geom_line(aes(y = seasonal, color = &quot;Seasonal&quot;)) +
  30. geom_line(aes(y = irregular, color = &quot;Irregular&quot;))+
  31. xlab(&#39;&#39;) + ylab(&quot;&quot;) +
  32. theme_light() +
  33. scale_color_manual(
  34. values = c(&quot;Observed&quot; = &quot;grey&quot;, &quot;Trend&quot; = &quot;black&quot;, &quot;Seasonal&quot; = &quot;green&quot;, &quot;Irregular&quot; = &quot;blue&quot;),
  35. name = &quot;&quot;
  36. ) +
  37. scale_y_continuous(
  38. labels = function(x) format(x, nsmall = 2)
  39. )+
  40. scale_x_date(labels = NULL, breaks = NULL,
  41. date_minor_breaks = &quot;1 month&quot;) +
  42. facet_wrap(~year, nrow=1, scales= &#39;free_x&#39;,
  43. strip.position = &#39;bottom&#39;)+
  44. theme(panel.spacing.x = unit(0, &quot;lines&quot;),
  45. panel.grid = element_blank(),
  46. legend.position = &quot;top&quot;,
  47. strip.background = element_rect(colour = &quot;white&quot;, fill = &quot;grey&quot;))

It produces the plot :

如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

What I am expecting in x-axis labelling ..
如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

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:

  1. geom_vline(xintercept = yrs, alpha = 0.2) +
  2. annotate(&quot;rect&quot;, xmin = yrs[1:(length(yrs)-1)],
  3. xmax = yrs[2:length(yrs)], ymin = -150, ymax = -100,
  4. fill = &quot;gray90&quot;, color = &quot;gray70&quot;) +
  5. annotate(&quot;text&quot;, x = yrs[1:(length(yrs)-1)] + 182, label = 1949:1960,
  6. y = -125) +

如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

答案2

得分: 2

你可以通过在x轴上设置expand = c(0, 0)来使用facets来实现这个目标。您需要将1月1日的值复制到前一年的12月31日,以确保曲线连续。虽然这可能有点像作弊,但请记住,每个facet上的像素远少于365个,所以这不会影响图表的外观:

  1. combined %>%
  2. filter(lubridate::month(index) == 1) %>%
  3. mutate(index = index - 1,
  4. year = factor(as.numeric(as.character(year)) - 1)) %>%
  5. bind_rows(combined) %>%
  6. filter(year != "1948") %>%
  7. ggplot(aes(x = index)) +
  8. geom_line(aes(y = data, color = "Observed")) +
  9. geom_line(aes(y = trend, color = "Trend"))+
  10. geom_line(aes(y = seasonal, color = "Seasonal")) +
  11. geom_line(aes(y = irregular, color = "Irregular"))+
  12. labs(x = NULL, y = NULL) +
  13. theme_light() +
  14. scale_color_manual(NULL,
  15. values = c("Observed" = "grey", "Trend" = "black",
  16. "Seasonal" = "green", "Irregular" = "blue")) +
  17. scale_y_continuous(labels = ~format(.x, nsmall = 2)) +
  18. scale_x_date(labels = NULL, breaks = NULL, expand = c(0, 0),
  19. date_minor_breaks = "1 month") +
  20. facet_wrap(~year, nrow = 1, scales= 'free_x',
  21. strip.position = 'bottom') +
  22. theme(panel.spacing.x = unit(0, "lines"),
  23. panel.grid = element_blank(),
  24. legend.position = "top",
  25. strip.background = element_rect(colour = "white", fill = "grey"))

如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. ```r
  5. combined %&gt;%
  6. filter(lubridate::month(index) == 1) %&gt;%
  7. mutate(index = index - 1,
  8. year = factor(as.numeric(as.character(year)) - 1)) %&gt;%
  9. bind_rows(combined) %&gt;%
  10. filter(year != &quot;1948&quot;) %&gt;%
  11. ggplot(aes(x = index)) +
  12. geom_line(aes(y = data, color = &quot;Observed&quot;)) +
  13. geom_line(aes(y = trend, color = &quot;Trend&quot;))+
  14. geom_line(aes(y = seasonal, color = &quot;Seasonal&quot;)) +
  15. geom_line(aes(y = irregular, color = &quot;Irregular&quot;))+
  16. labs(x = NULL, y = NULL) +
  17. theme_light() +
  18. scale_color_manual(NULL,
  19. values = c(&quot;Observed&quot; = &quot;grey&quot;, &quot;Trend&quot; = &quot;black&quot;,
  20. &quot;Seasonal&quot; = &quot;green&quot;, &quot;Irregular&quot; = &quot;blue&quot;)) +
  21. scale_y_continuous(labels = ~format(.x, nsmall = 2)) +
  22. scale_x_date(labels = NULL, breaks = NULL, expand = c(0, 0),
  23. date_minor_breaks = &quot;1 month&quot;) +
  24. facet_wrap(~year, nrow = 1, scales= &#39;free_x&#39;,
  25. strip.position = &#39;bottom&#39;) +
  26. theme(panel.spacing.x = unit(0, &quot;lines&quot;),
  27. panel.grid = element_blank(),
  28. legend.position = &quot;top&quot;,
  29. strip.background = element_rect(colour = &quot;white&quot;, fill = &quot;grey&quot;))

如何在ggplot2中跨越时间序列数据中的facet边界创建连续的geom_line?

huangapple
  • 本文由 发表于 2023年6月5日 01:57:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定