r - 使用 geom_smooth 维护 x 轴的日期类

标签 r ggplot2

我正在尝试使用 this approach 平滑三条线并遮蔽最小线和最大线之间的区域。我的 x 轴一开始是一个日期类,但在最后一步中出现错误。

输入无效:date_trans 仅适用于 Date 类的对象

平滑到p1时我丢失了日期类。如何防止这种情况发生,或者在最后一步中将日期传递给 geom_ribbon

library(tidyverse)
mmr <- structure(list(year = structure(c(3834, 3926, 3987, 4108, 4169, 
4230, 4352, 4442, 4503, 4595, 4687, 4748, 4929, 4991, 5052, 5204, 
5265, 5326, 5448, 5538, 5599, 5752, 5813, 5844, 6025, 6087, 6148, 
6299, 6360, 6574, 6634, 6695, 6848, 6909, 7091, 7183, 7213, 7395, 
7456, 7517, 7639, 7729, 7790, 7943, 8035, 8187, 8248, 8309, 8460, 
8552, 8613, 8735, 8825, 8886, 9009, 9100, 9131, 9282, 9343, 9404, 
9556, 9617, 9678, 9831, 9862, 9952, 10105, 10135, 10196, 10347, 
10408, 10470, 10592, 10682, 10896, 10957, 11078, 11170, 11231, 
11382, 11474, 11596, 11657, 11688, 11869, 11931, 11961, 12112, 
12173, 12234, 12357, 12418, 12478, 12600, 12692, 12874, 12935, 
12996, 13118, 13208, 13239, 13392, 13483, 13665, 13757, 13787, 
13939), class = "Date"), mmrU = c(13.8231596, 13.7529708, 13.7148858, 
13.6118164, 13.5638772, 13.5478975, 13.519933, 13.5012899, 13.474657, 
13.4014166, 13.4173963, 13.3787786, 13.2882268, 13.2589306, 13.2354049, 
13.1763686, 13.1470724, 13.1255442, 13.0618471, 13.032551, 13.0108008, 
13.0139079, 13.0232294, 13.0352143, 13.0498623, 13.0498623, 13.0376556, 
13.0232294, 13.0205662, 12.987275, 12.9739586, 12.9033814, 12.9792852, 
12.99127, 13.0059181, 13.0059181, 13.0059181, 13.0205662, 13.0130202, 
13.0034767, 12.9606421, 12.9406675, 12.9277949, 12.8727536, 12.8203756, 
12.7728802, 12.7582321, 12.7569005, 12.7422524, 12.7382575, 12.7171414, 
12.6397157, 12.601098, 12.5737993, 12.6583588, 12.7382575, 12.7979151, 
12.9473257, 12.9965966, 13.0327729, 13.1191079, 13.1617205, 13.2061087, 
13.332171, 13.39609, 13.4586773, 13.6477709, 13.7676189, 13.8408594, 
14.0619124, 14.1604541, 14.2246394, 14.3815071, 14.4851535, 14.7996436, 
14.9234866, 15.0436305, 15.3022444, 15.4277361, 15.7198102, 15.9745982, 
16.2684478, 16.4362351, 16.5409911, 16.8277386, 16.9728879, 17.0634398, 
17.3204472, 17.4616016, 17.5578129, 17.819814, 17.9623, 18.0631721, 
18.3045327, 18.4784455, 18.6694034, 18.7612869, 18.8153517, 18.9250792, 
18.9770134, 19.0189602, 19.0675652, 19.07955, 19.1008563, 19.0582437, 
19.0292138, 18.9410589), mmr = c(12.1431454, 12.11401197, 12.05086241, 
12.02612341, 11.96590348, 11.93823486, 11.8828976, 11.8601117, 
11.8275604, 11.8047745, 11.7784079, 11.7461821, 11.6955796, 11.7256748, 
11.6689467, 11.6396505, 11.5997012, 11.6033632, 11.5384455, 11.5144759, 
11.4532202, 11.4532202, 11.5411088, 11.5198025, 11.4971645, 11.5118126, 
11.5118126, 11.5345985, 11.5586865, 11.4532202, 11.4532202, 11.4692, 
11.5144759, 11.5201829, 11.5494791, 11.525129, 11.5662198, 11.570405, 
11.4665367, 11.4984962, 11.4345772, 11.4692, 11.4092759, 11.3893013, 
11.3653317, 11.3799798, 11.4406647, 11.3014127, 11.2721165, 11.2833024, 
11.2720179, 11.2161875, 11.1675824, 11.1749065, 11.1928097, 11.2546572, 
11.3295252, 11.397883, 11.568574, 11.5020472, 11.5437721, 11.5792826, 
11.6846601, 11.7494372, 11.8112847, 11.8868038, 12.0684401, 12.1749717, 
12.2844995, 12.3747184, 12.4756571, 12.5664753, 12.7422524, 12.8274777, 
13.114314, 13.2216445, 13.3164576, 13.575862, 13.7156847, 13.8555074, 
14.1513989, 14.2922869, 14.4147983, 14.5559526, 14.8196183, 14.9421296, 
15.0779574, 15.203132, 15.3283066, 15.4521496, 15.686519, 15.7970456, 
15.9248835, 16.0292844, 16.21678, 16.3945102, 16.4693782, 16.5442463, 
16.6028386, 16.661431, 16.7265336, 16.7734075, 16.8304019, 16.8407888, 
16.781464, 16.7411817, 16.6532932), mmrL = c(10.68619282, 10.63692196, 
10.5963068, 10.49976255, 10.45848156, 10.44250182, 10.4092107, 
10.39189932, 10.38390945, 10.31732721, 10.28270445, 10.25695931, 
10.20813234, 10.21612221, 10.22278043, 10.25207662, 10.26139813, 
10.24719392, 10.21079563, 10.19481589, 10.17883615, 10.17883615, 
10.17883615, 10.17883615, 10.1921526, 10.19481589, 10.20813234, 
10.20946398, 10.22011714, 10.19481589, 10.18283109, 10.17883615, 
10.17883615, 10.17883615, 10.17883615, 10.17484122, 10.16418806, 
10.14554503, 10.13356023, 10.12024378, 10.09893747, 10.08828431, 
10.0762995, 10.06165141, 10.04700332, 10.02436536, 9.999064105, 
9.9810869, 9.936476799, 9.911175548, 9.890756995, 9.840598374, 
9.815297123, 9.79798574, 9.880547718, 9.947129958, 9.997199802, 
10.1149172, 10.14021845, 10.14953997, 10.18948931, 10.21345892, 
10.24963527, 10.36659807, 10.42519044, 10.47179801, 10.63958525, 
10.74478519, 10.82335224, 10.99261909, 11.11726527, 11.15293432, 
11.27611147, 11.33869877, 11.52113411, 11.58638471, 11.6377862, 
11.82075419, 11.96190854, 12.31346277, 12.45195383, 12.52985505, 
12.74491568, 12.85810549, 12.93633962, 13.16971038, 13.29887992, 
13.38676848, 13.61581138, 13.71834803, 13.78226698, 13.95005423, 
14.03927443, 14.10159541, 14.27497556, 14.42971269, 14.57725893, 
14.60788676, 14.63185637, 14.69843861, 14.72373986, 14.73439302, 
14.73439302, 14.73439302, 14.73683436, 14.674469, 14.60744288
)), row.names = c(NA, -107L), class = "data.frame")

# smooth lines
p1 <- 
mmr %>% 
  ggplot(.) +
  geom_smooth(aes(x=year, y=mmrL)) +
  geom_smooth(aes(x=year, y=mmr)) +
  geom_smooth(aes(x=year, y=mmrU)) +
  ylim(0, 25) +
  scale_x_date(date_breaks="2 years", date_labels = "%Y")

# build plot object for rendering 
pp1 <- ggplot_build(p1)

# extract data from the upper and lower lines
df2 <- data.frame(x = pp1$data[[1]]$x,
                  ymin = pp1$data[[2]]$y,
                  ymax = pp1$data[[3]]$y) 

# use the lm data to add the ribbon to the plot 
p1 +  geom_ribbon(data = df2, aes(x = x, ymin = ymin, ymax = ymax), fill = "grey", alpha = 0.4)

最佳答案

问题是 pp1$data[[1]]$x 不再是日期格式。我们可以改为使用

df2 <- data.frame(x = as.Date(pp1$data[[1]]$x, origin = "1970-01-01"),
                  ymin = pp1$data[[1]]$y,
                  ymax = pp1$data[[3]]$y) 

我还调整了ymin。这给出了

enter image description here

关于r - 使用 geom_smooth 维护 x 轴的日期类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670198/

相关文章:

r - 在每个面的密度图的中心平均值上画一条线

r - 如何在同一标签文本中切换纯文本字体和纯文本字体

r - 如何在 R 或 Stata 中绘制相对频率

r - 如何在 ggplot2 中绘制一组旋转密度?

r - write.csv 用于大数据.table

r - 使用 dplyr 按组获取累积计数

python - 将纬度/经度分配给邮政编码

r - 在ggplot2中添加轴注释

R Shiny : Dynamic Row of Action Buttons

r - 在 R 中用另一个矩阵索引一个矩阵 - 索引越界