具有多个x轴的R plotly框架
Posted
技术标签:
【中文标题】具有多个x轴的R plotly框架【英文标题】:R plotly frame with multiple x-axis 【发布时间】:2021-12-15 10:13:05 【问题描述】:我正在尝试使用 plotly 绘制线图,如下所示:
library(plotly)
library(dplyr)
datatoplot<-structure(list(year = c(1397, 1397, 1397, 1397, 1397, 1397, 1397,
1397, 1397, 1397, 1397, 1397, 1398, 1398, 1398, 1398, 1398, 1398,
1398, 1398, 1398, 1398, 1398, 1398, 1399, 1399, 1399, 1399, 1399,
1399, 1399, 1399, 1399, 1399, 1399, 1399), month = c(1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), v10 = c(2.8,
5.1, 6.3, 4.8, 4.1, 2.9, 3, 3.6, 4.4, 4.1, 3.4, 5.9, 1.7, 2.5,
2.1, 2.7, 2.3, 3, 3.2, 3.6, 2, 2, 1.6, 2.6, 1.5, 4, 2.9, 3.6,
3.6, 2.5, 2.2, 1.6, 1.9, 2.2, 3.6, 4.5), v14 = c(0.67, 0.65,
0.56, 0.65, 0.73, 0.67, 0.6, 0.64, 0.61, 0.61, 0.69, 0.64, 0.69,
0.75, 0.76, 0.73, 0.76, 0.68, 0.73, 0.92, 0.69, 0.66, 0.78, 0.71,
0.67, 0.68, 0.69, 0.73, 0.68, 0.76, 0.73, 0.81, 0.75, 0.8, 0.8,
0.71)), row.names = c(NA, 36L), class = "data.frame")
datatoplot %>% plot_ly( alpha = 0.5) %>%
add_lines(
x = ~list(year,month), y = ~v10
#,frame=~list(year,month)
,line = list(simplify = FALSE)
) %>%
layout(yaxis = list(title = "A"))
输出如下:
现在我想使用 'frame' 参数在绘图中添加一个框架
frame=~list(year,month)
但不幸的是它不起作用,输出如下:
如何解决?
【问题讨论】:
【参考方案1】:不确定这是否是您的预期输出,但在我看来,将 year
设置为 frame
是有意义的:
library(plotly)
library(dplyr)
datatoplot <- structure(
list(
year = c(1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397, 1397,
1397, 1397, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398, 1398,
1398, 1398, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399, 1399,
1399, 1399),
month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
v10 = c(2.8, 5.1, 6.3, 4.8, 4.1, 2.9, 3, 3.6, 4.4, 4.1, 3.4, 5.9, 1.7,
2.5, 2.1, 2.7, 2.3, 3, 3.2, 3.6, 2, 2, 1.6, 2.6, 1.5, 4, 2.9, 3.6, 3.6,
2.5, 2.2, 1.6, 1.9, 2.2, 3.6, 4.5),
v14 = c(0.67, 0.65, 0.56, 0.65, 0.73, 0.67, 0.6, 0.64, 0.61, 0.61, 0.69,
0.64, 0.69, 0.75, 0.76, 0.73, 0.76, 0.68, 0.73, 0.92, 0.69, 0.66, 0.78,
0.71, 0.67, 0.68, 0.69, 0.73, 0.68, 0.76, 0.73, 0.81, 0.75, 0.8, 0.8,
0.71)
),
row.names = c(NA, 36L),
class = "data.frame"
)
datatoplot %>% plot_ly(alpha = 0.5) %>%
add_lines(
x = ~ month,
y = ~ v10,
frame = ~ year,
line = list(simplify = FALSE)
) %>%
layout(yaxis = list(title = "A"))
【讨论】:
以上是关于具有多个x轴的R plotly框架的主要内容,如果未能解决你的问题,请参考以下文章
R Shiny Plotly 3D:将x,y,z轴的标题更改为实际变量名称并添加图例标题