in plotly you make a bar chart, then add a trace ( a line) and set the layout to be on the right axis)
library(tidyverse)
library(plotly)
set.seed(42)
(df <- tibble(
date_x = seq.Date(from=as.Date("2019-01-01"),
by = "1 month",
length.out = 12),
barheight_y1 = sample.int(n = 100,size=12),
lineheight_y2 = runif(12)
))
(p <- plot_ly(data=df,
x=~date_x,
y=~barheight_y1,
type="bar",
name="mybar"
) %>% add_trace(
type="scatter",
mode="lines+markers",
y=~lineheight_y2,
name="myline",
yaxis="y2"
) %>%
layout(yaxis=list(side="left",title="barheight"),
yaxis2=list(side="right",title="lineheight",overlaying='y')))
