5

I have created the following dataframe and associated ggplot chart in R First we import the libraries using R

 library(plotly)
 library(ggplot2)

Next we create the dataframe as follows

    dataframe_1<-data.frame("Month"=c(1:12))
    dataframe_1$Sales<-25*dataframe_1$Month
    dataframe_1$Fac1=dataframe_1$Sales/100
    dataframe_1$Month<-as.character(dataframe_1$Month)

Next we create a ggplot based bar chart combined with a line chart as folllows

   p<-ggplot(data = dataframe_1, mapping = aes(x = Month, y = Fac1))+geom_bar(data = dataframe_1, mapping = aes(x = Month, y = Sales/100, fill = "#82e600", text=paste0("Sales:", Sales)),stat="identity")+geom_line(mapping = aes(x = Month, y = Fac1, group = 1))+geom_point(mapping = aes(x = Month, y = Fac1, group = 1, text=paste0( "Factoid:", Fac1)),    inherit.aes = FALSE)+scale_y_continuous(sec.axis = sec_axis(~.*100, name = "Sales"))+labs(fill =  "Sales")

When we render the plot p, we get a plot with two y axes. However, when we render the dynamicticks as true, the second y axis disappears

 ggplotly(p = p, tooltip = "text", dynamicTicks = TRUE)

I request someone to take a look and help.

Raghavan vmvs
  • 1,213
  • 1
  • 10
  • 29

1 Answers1

0

I believe we must add a trace or plot_ly object onto the ggplotly object. Adding second Y axis on ggplotly. This solution may work for some applications.

kraggle
  • 196
  • 2
  • 9