我设法改变了ggplot中的范围,但当将该图传递到plotly时,它完全无视这一点。
- 目标**
我想通过从1996年起的一个长系列到plotly,它希望plotly有所有的日期在它,但只显示一组系列,我决定(约过去18个月)-但用户应该能够缩小,看到整个系列,如果一个想要的。-〉我怎么能做到这一点?
我已经创建了极限值maxdate
和mindate
,所以这些值可以调用。但是请注意,这些更改,所以日期不能在这里的代码中手动传递。* 在我的df中有一个列,它具有日期格式的日期,如果我的变量不能使用,可以调用它 *。
> str(maxdate)
Date[1:1], format: "2023-01-01"
> str(mindate)
Date[1:1], format: "2021-07-01"
- 编辑1**
- df_konj的字符串**
> str(df_konj)
'data.frame': 1950 obs. of 3 variables:
$ Indikator: Factor w/ 6 levels "Barometerindikatorn",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Period : Date, format: "1996-01-01" "1996-02-01" "1996-03-01" "1996-04-01" ...
$ value : num NA NA NA NA NA NA 85.6 88 88.9 91 ...
- ggplot代码:**
df_konj_dia <- ggplot(df_konj, aes(x = Period, y = value, group=Indikator, color=Indikator,
linetype=Indikator, linewidth = Indikator)) +
geom_point(size=2) +
geom_line() +
ylim(50, 150)
scale_x_date(limits=(c(mindate, maxdate)))
- 剧情代码:**
df_konj_plotly <- ggplotly(df_konj_dia, tooltip = c("x","y","colour")) %>%
layout(title = list(text = paste0('Konjunkturbarometern - månadsbarometern, flera indikatorer',
'<br>',
'<sup>',
'Sverige, säsongsrensat','</sup>')),
legend = list(x = 100, y = 0.5),
xaxis = list(rangeslider = list(visible = T)))
df_konj_plotly
- dput-df的输出:"df_konj",年份从2018年过滤,但**
structure(list(Indikator = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), levels = c("Barometerindikatorn",
"\tDetaljhandel", "Bygg & anläggning", "Hushåll", "Tillverkningsindustri",
"Tjänstesektorn"), class = "factor"), Period = structure(c(17532,
17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805,
17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078,
18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353,
18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628,
18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901,
18932, 18962, 18993, 19024, 19052, 19083, 19113, 19144, 19174,
19205, 19236, 19266, 19297, 19327, 19358, 17532, 17563, 17591,
17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866,
17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140,
18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414,
18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687,
18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932, 18962,
18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205, 19236,
19266, 19297, 19327, 19358, 17532, 17563, 17591, 17622, 17652,
17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 17928,
17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201,
18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475,
18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748,
18779, 18809, 18840, 18871, 18901, 18932, 18962, 18993, 19024,
19052, 19083, 19113, 19144, 19174, 19205, 19236, 19266, 19297,
19327, 19358, 17532, 17563, 17591, 17622, 17652, 17683, 17713,
17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987,
18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262,
18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536,
18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809,
18840, 18871, 18901, 18932, 18962, 18993, 19024, 19052, 19083,
19113, 19144, 19174, 19205, 19236, 19266, 19297, 19327, 19358,
17532, 17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775,
17805, 17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048,
18078, 18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322,
18353, 18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597,
18628, 18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871,
18901, 18932, 18962, 18993, 19024, 19052, 19083, 19113, 19144,
19174, 19205, 19236, 19266, 19297, 19327, 19358, 17532, 17563,
17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836,
17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109,
18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383,
18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659,
18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932,
18962, 18993, 19024, 19052, 19083, 19113, 19144, 19174, 19205,
19236, 19266, 19297, 19327, 19358), class = "Date"), value = c(108.2,
108.3, 108.8, 110.1, 109.8, 109.8, 109.5, 110, 109.6, 106.1,
104.7, 104.8, 100.7, 101.8, 102.4, 104.4, 101.8, 100.4, 98.2,
95, 93.3, 92.9, 93.7, 93, 95.9, 98.6, 93.4, 60.9, 64.9, 76.4,
84.4, 87, 93.7, 94.3, 95.8, 95.1, 100.5, 103.7, 105.3, 112.2,
117.4, 118.1, 120.3, 118.9, 118.6, 120, 117.7, 116.9, 110.9,
114, 112.2, 110.3, 110.8, 106.1, 102.4, 97.7, 91.8, 85.6, 85.6,
85, 82.3, 108.8, 109.1, 112.3, 114.8, 114.9, 113.6, 114.5, 116.5,
112.8, 111.3, 111.6, 112.5, 106.8, 109, 104.9, 107.4, 102.6,
100.8, 97, 93.6, 94.8, 94.6, 94.2, 93.5, 98.4, 101.3, 99.1, 72.2,
77.2, 90.4, 95.9, 97.1, 103.9, 103.1, 106.4, 104.6, 111.2, 111.7,
114.5, 117.2, 121.8, 121.2, 124.4, 124.3, 123.3, 126.2, 124,
124.8, 120.9, 123.6, 124.3, 120.5, 124.6, 119.5, 118.9, 115.3,
109.8, 104.8, 104.5, 103.7, 99.5, 111.6, 107, 111.3, 114.1, 107.8,
107.1, 106.4, 107.1, 106.6, 102.1, 99.9, 101.3, 102.1, 101.6,
101.9, 102.8, 102.8, 100.9, 102.2, 101.1, 102.5, 102.8, 102.8,
102.6, 101.4, 100.7, 98.7, 92.7, 88.6, 88.7, 91.7, 93.3, 89.2,
92.3, 94.2, 96, 95.2, 95.7, 96.8, 101.3, 107.2, 108.9, 106.1,
107.5, 109.7, 110.1, 108.5, 107, 108.9, 108, 107.1, 108, 105.6,
106.6, 106.9, 107.4, 105.2, 102.7, 101.5, 101.2, 96.1, 103.8,
105.9, 104.5, 101.6, 103.4, 105.3, 98, 102, 103.8, 101.1, 101.6,
100.9, 99.5, 98.6, 98.3, 104.2, 106.6, 102.2, 101.2, 101.7, 101.1,
101.2, 104.2, 107.8, 107.7, 109.5, 104.4, 73.7, 77.1, 85, 94.2,
97.6, 102, 105.1, 96.3, 96.8, 96.6, 98.4, 96.5, 111, 108.2, 113,
116.7, 109.2, 113.7, 116.9, 116.8, 112.8, 103.4, 119.2, 111.1,
109.5, 110.3, 102.8, 92.2, 91.2, 82.1, 76.2, 76.7, 75.1, 78.6,
104.5, 105.6, 103.5, 103.6, 103.3, 105.8, 104.9, 101.7, 104.5,
101, 98.2, 98.4, 95.9, 96.3, 101.1, 101.5, 101.8, 101.1, 98.6,
97, 92.9, 90.8, 93.2, 91.7, 93.4, 94.2, 88.2, 52.3, 55.1, 63.8,
74.2, 79.2, 86.1, 86.5, 88, 86.7, 91, 96.7, 96.9, 106, 109.1,
112.3, 114, 111.5, 111.2, 112.5, 111.4, 110, 104.2, 107.4, 109.1,
109.6, 108.5, 105.9, 103.7, 97.1, 94.4, 88.5, 86, 87.5, 85.8,
106, 104.7, 103.1, 102.9, 103.3, 101.3, 101.8, 104.1, 104.9,
100.8, 99.9, 97.6, 96.3, 96.4, 99.1, 100.1, 97.6, 98.2, 100.9,
97.2, 94.2, 96.4, 96.1, 96.2, 96, 99.7, 94.1, 81.4, 83.7, 88.6,
90.3, 89.7, 92.3, 94.1, 92.7, 94.9, 97, 99.4, 100.4, 104.2, 110.4,
107.7, 107.6, 107.5, 107.2, 104, 101, 99.5, 93.8, 92.7, 82.1,
81.3, 77.1, 73.7, 64.9, 64.1, 57.2, 53.6, 58.6, 55.4, 56.3)), row.names = c(NA,
-366L), class = "data.frame")
- 编辑2:**
哇,真棒-谢谢山姆!这工程,虽然我的图表在我的预览非常小:
这是怎么回事?我试着理解你获取的这个边距列表(用绘图的方式),但并不真正理解它,它是基于一些位置移动图表(用像素表示),你也可以改变?垫表示什么?
我不知道我削减了系列,但没有使用coord_cartesian
,很高兴知道。因为我的系列是所有的方式回到1996年,日期仍然重叠-有没有办法让一些自动缩放开始?否则我将不得不缩短系列,也许到2018年的数据,你得到的,或者2020年。能够回到1200多万年前只是一个很好的功能--这不是必需的。
再次感谢!
1条答案
按热度按时间9lowa7mx1#
这里有两个问题,主要的一个是在设置轴限制时实际上切断了您感兴趣的绘图部分,您应该使用
coord_cartesian()
:笛卡尔坐标系......不会像设置比例限制那样更改基础数据。
其次,你需要为
plotly
提供轴断点和轴标签,否则,当你向后滚动时,你将看不到任何轴标签。然后将它们提供给
plotly
,如下所示(我还更改了它们重叠的边距):这将允许您在时间上向后移动窗口,但仍能在x轴上看到日期标签: