Using plotly I often had trouble with multiple line plot and data of different length. 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? 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? Eg Honda and Merc left, Toyota right. 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)
For further information please see this .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.