tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.52k stars 2.03k forks source link

RShiny mouse-click does not work with facet_grid #1539

Closed choobamboo closed 8 years ago

choobamboo commented 8 years ago

Hi, after ggplot2 updated from ver 1.0.1 to 2.0.0, I found that interative facet_grid ggplot in shiny app is not working as expected as before. Below is an example of ggplot2 calendar heatmap. The data generation (which is not essential to this problem) is referenced from here: http://www.r-bloggers.com/ggplot2-time-series-heatmaps/

Running this script with ggplot2 ver 1.0.1 works perfectly - when clicking anywhere on the plot, x and y coordinates are prompted. However with ver 2.0.0, no coordinates are shown. If commenting out the line of facet_grid(year~monthf) +, then it works again.

Any help is appreciated!

require(quantmod)
require(ggplot2)
require(reshape2)
require(plyr)
require(scales)
library(shiny)

#This function is only to produce data
getData = function(){
  # Download some Data, e.g. the CBOE VIX 
  getSymbols("^VIX",src="yahoo")

  # Make a dataframe
  dat<-data.frame(date=index(VIX),VIX)
  #cut
  dat <- dat[dat$date>as.Date("2015-01-01"),]

  # We will facet by year ~ month, and each subgraph will
  # show week-of-month versus weekday
  # the year is simple
  dat$year<-as.numeric(as.POSIXlt(dat$date)$year+1900)
  # the month too 
  dat$month<-as.numeric(as.POSIXlt(dat$date)$mon+1)
  # but turn months into ordered facors to control the appearance/ordering in the presentation
  dat$monthf<-factor(dat$month,levels=as.character(1:12),labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE)
  # the day of week is again easily found
  dat$weekday = as.POSIXlt(dat$date)$wday
  # again turn into factors to control appearance/abbreviation and ordering
  # I use the reverse function rev here to order the week top down in the graph
  # you can cut it out to reverse week order
  dat$weekdayf<-factor(dat$weekday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE)
  # the monthweek part is a bit trickier 
  # first a factor which cuts the data into month chunks
  dat$yearmonth<-as.yearmon(dat$date)
  dat$yearmonthf<-factor(dat$yearmonth)
  # then find the "week of year" for each day
  dat$week <- as.numeric(format(dat$date,"%W"))
  # and now for each monthblock we normalize the week to start at 1 
  dat<-ddply(dat,.(yearmonthf),transform,monthweek=1+week-min(week))

  return(dat)

}

#######################################
#Shiny Server
server = function(input,output){
  data = getData()
  output$myplot = renderPlot({
    ggplot(data, aes(monthweek, weekdayf, fill = VIX.Close)) +
      geom_tile(colour = "white") + 
 ## >>>>> The following line makes the interactive plot not working w/ ggplot2 2.0.0
      facet_grid(year~monthf) +
 ## <<<<<
      scale_fill_gradient(low="red", high="yellow")
  })
  output$x = renderText(input$myplot_click$x)
  output$y = renderText(input$myplot_click$y)
}

#Shiny UI
ui = fluidPage(
  mainPanel(
    fluidRow(
      plotOutput("myplot" ,click = "myplot_click")
    ),
    fluidRow(
      verbatimTextOutput("x"), 
      verbatimTextOutput("y")
    )

  )
)

shinyApp(ui=ui,server=server)
wch commented 8 years ago

What version of Shiny are you using? You need the latest, 0.13.0.

choobamboo commented 8 years ago

Ah, that's it. I was using 0.12.2. The latest version works. Thank you for looking into it!