多个 y 轴使用 plotly 和不同长度的数据
Multiple y-axes using plotly with data of different length
使用 plotly 我经常遇到多线图和不同长度数据的问题。然后我找到了this解决方案:
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"))
现在我面临下一个问题。如何向该示例添加多个 y 轴?是否还可以使用 R 拥有 2 个以上的 y 轴并像 this Python example at the very bottom of the page? E.g. Honda and Merc left, Toyota right. I found a solution 中那样左右排列它们,但数据具有相同的长度,所以这对我不起作用。
我想防止像这个数据框中的不同数量级的数据缩放:
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)
)
请检查以下内容:
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)
有关详细信息,请参阅 this。
使用 plotly 我经常遇到多线图和不同长度数据的问题。然后我找到了this解决方案:
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"))
现在我面临下一个问题。如何向该示例添加多个 y 轴?是否还可以使用 R 拥有 2 个以上的 y 轴并像 this Python example at the very bottom of the page? E.g. Honda and Merc left, Toyota right. I found a solution
我想防止像这个数据框中的不同数量级的数据缩放:
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)
)
请检查以下内容:
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)
有关详细信息,请参阅 this。