Search code examples
rggplot2graphplotlyggplotly

Plotly Interactive Graph not working in R?


In R, I have a dataframe "depins" of the following format, with the leftmost column having dates in a yearmon format:

enter image description here

I wanted to create an interactive graph using the ggplot2 and plotly packages, but when I try to interact/hover over any spot on the graph, it displays the same date and value (Jan 2004, 52) no matter where I interact with it. Does anyone perhaps know why the code below only displays the same date/value whenever I hover over it?

p<-
ggplot(depins, mapping=aes(x=Month, y=deposit_insurance))+
  geom_line(color="blue")+
  ggtitle("Frequency of 'deposit insurance' searches, 2004-present")+
  labs(x="Month", y="Frequency")

p<-ggplotly(p)
p

Solution

  • Here I have converted your Month column to Date Format

    library(zoo)
        
        depins <- data.frame(
          Month = c("Jan 2004", "Feb 2004", "Mar 2004", "Apr 2004", "May 2004", "Jun 2004","Jul 2004", "Aug 2004"),
          deposit_insurance = c(52,46,42,54,47,34,26,34)
          )
        
        depins$Month <- as.yearmon(depins$Month, format = "%b %Y")
        
        p<-
          ggplot(depins, aes(x=Month, y=deposit_insurance))+
          geom_line(color="blue")+
          ggtitle("Frequency of 'deposit insurance' searches, 2004-present")+
          labs(x="Month", y="Frequency")
          
        ggplotly(p)