[英]Multiple y-axes using plotly with data of different length
Using plotly I often had trouble with multiple line plot and data of different length.使用 plotly 我经常在处理多线图和不同长度的数据时遇到问题。 Then I found this solution:
然后我找到了这个解决方案:
library(plotly)
df1 <- data.frame(
id = c("Honda", "Honda", "Honda", "Merc", "Merc", "Merc", "Toyota", "Toyota"),
date = c('10/30/12', '10/31/12', '11/1/12', '11/2/12', '10/30/12', '10/31/12', '11/1/12', '11/3/12'),
Value = c(2, 3, 3, 4, 1, 2, 3, 2)
)
df1$date <- as.Date(df1$date, "%m/%d/%y")
df1 %>%
group_by(id) %>%
plot_ly(x=~date, y=~Value, type='scatter', color=~id, mode="lines+markers") %>%
layout(xaxis=list(title="Date"),yaxis=list(title="Cars Sold"))
Now I face the next problem.现在我面临下一个问题。 How can I add multiple y-axes to this example?
如何在此示例中添加多个 y 轴? Is it furthermore possible to have more than 2 y-axes using R and order them left and right like in this Python example at the very bottom of the page?
是否还有可能使用 R 拥有超过 2 个 y 轴并像页面最底部的这个Python 示例那样左右排列它们? Eg Honda and Merc left, Toyota right.
例如本田和 Merc 左,丰田右。 I found a solution here , but the data has the same length, so this is not working for me.
我在这里找到了一个解决方案,但数据的长度相同,所以这对我不起作用。
I want to prevent the scaling of data with different order of magnitude like in this data frame:我想防止像在这个数据框中那样按不同的数量级缩放数据:
df1 <- data.frame(
id = c("Honda", "Honda", "Honda", "Merc", "Merc", "Merc", "Toyota", "Toyota"),
date = c('10/30/12', '10/31/12', '11/1/12', '11/2/12', '10/30/12', '10/31/12', '11/1/12', '11/3/12'),
Value = c(0.02, 0.03, 0.03, 4, 1, 2, 3, 2)
)
Please check the following:请检查以下内容:
library(plotly)
df1 <- data.frame(
id = c("Honda", "Honda", "Honda", "Merc", "Merc", "Merc", "Toyota", "Toyota"),
yaxis = c("y", "y", "y", "y2", "y2", "y2", "y3", "y3"),
date = c('10/30/12', '10/31/12', '11/1/12', '11/2/12', '10/30/12', '10/31/12', '11/1/12', '11/3/12'),
Value = c(0.02, 0.03, 0.03, 4, 1, 2, 3, 2)
)
df1$date <- as.Date(df1$date, "%m/%d/%y")
y2 <- list(
tickfont = list(color = "red"),
overlaying = "y",
side = "left",
title = "Merc",
position=0.1,
showgrid = FALSE
)
y3 <- list(
tickfont = list(color = "green"),
overlaying = "y",
side = "right",
title = "Toyota",
showgrid = FALSE
)
df1 %>%
group_by(id) %>%
plot_ly(x=~date, y=~Value, type='scatter', color=~id, mode="lines+markers", yaxis=~yaxis) %>%
layout(xaxis=list(title="Date", domain = list(0.15, 0.95)), yaxis=list(title="Honda"), yaxis2=y2, yaxis3=y3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.