R Plotly: Second Y-Axis in a R plotly graph

I need to use plotly for a Second Y-Axis Graph. This was asked here, but the answer is not valid anymore:

I would like to get a bar and line graph, or a 2 lines graph with dual y axis.

Tried searching the documentation but couldn't find updated examples that work.


library(plotly)
library(dplyr)




df <- data.frame(MediaDate = as.Date(c("2016-04-01","2016-05-01","2016-06-01"), format = "%Y-%m-%d"),
                 Spend = c(39654, 34446, 27402),
                 Visits = c(19970, 14450, 12419))



plot_ly(df, x = ~MediaDate, y = ~Spend, type = "bar", name = "Spend") %>%
  add_trace(x = ~MediaDate, y = ~Visits, mode = "lines", yaxis = "y2", name = "Visits") %>%
  layout(yaxis2 = list(overlaying = "y", side = "right"))

Returns:

Error:

Warning message:
'bar' objects don't have these attributes: 'mode'
Valid attributes include:
'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'x', 'x0', 'dx', 'y', 'y0', 'dy', 'text', 'hovertext', 'textposition', 'textfont', 'insidetextfont', 'outsidetextfont', 'constraintext', 'cliponaxis', 'orientation', 'base', 'offset', 'width', 'marker', 'selected', 'unselected', 'r', 't', '_deprecated', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'textpositionsrc', 'basesrc', 'offsetsrc', 'widthsrc', 'rsrc', 'tsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

All packages updated and from Github:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Peru.1252  LC_CTYPE=Spanish_Peru.1252    LC_MONETARY=Spanish_Peru.1252 LC_NUMERIC=C                 
[5] LC_TIME=Spanish_Peru.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] bindrcpp_0.2      plotly_4.8.0.9000 ggplot2_3.0.0     dplyr_0.7.4      

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.12      later_0.7.4       pillar_1.3.0      compiler_3.4.1    git2r_0.19.0      plyr_1.8.4        bindr_0.1        
 [8] tools_3.4.1       digest_0.6.17     jsonlite_1.5.9000 memoise_1.1.0     tibble_1.4.2      gtable_0.2.0      viridisLite_0.3.0
[15] pkgconfig_2.0.1   rlang_0.2.2       shiny_1.1.0       rstudioapi_0.6    crosstalk_1.0.0   curl_2.8.1        yaml_2.2.0       
[22] withr_2.1.2       httr_1.3.1        htmlwidgets_1.2   devtools_1.13.4   grid_3.4.1        glue_1.3.0        data.table_1.11.4
[29] R6_2.2.2          purrr_0.2.5       tidyr_0.8.1       magrittr_1.5      promises_1.0.1    scales_1.0.0      htmltools_0.3.6  
[36] assertthat_0.2.0  xtable_1.8-2      mime_0.5          colorspace_1.3-2  httpuv_1.4.5      lazyeval_0.2.1    munsell_0.5.0    
[43] crayon_1.3.4

Please try the function sec_axis inside the scale_y_continuous.

Follow the link here for proper example with soln.