I have a two y-axis plot. The reproducible example looks like this:
library(plotly)
library(lubridate)
year <- c(2011:2019)
data1 <- c(2, 3, 2, 5, 1, 8, 2, 7, 9)
data2 <- c(20, 30, 20, 50, 10, 80, 20, 70, 90)
dataset <- data.frame(year, data1, data2)
dataset$year <- lubridate::parse_date_time(dataset$year, orders = "%Y")
dataset$year <- as.Date(dataset$year)
plot_ly() %>%
add_trace(x = dataset$year,
y = dataset$data1,
type="bar",
#mode="lines+markers",
yaxis="y",
name="yaxis data",
color = "red") %>%
add_trace(x = dataset$year,
y = dataset$data2,
type="scatter",
mode="lines+markers",
yaxis="y2",
name="yaxis2 data",
color = "blue") %>%
layout(yaxis=list(side="left"),
yaxis2=list(side="right",overlaying="y"),
showlegend=TRUE)
The code works fine except the color part. How to specify colors for the line and bar in the plot?
1