0
votes

I'm trying to get a simple bar chart with plotly in a Shiny app.

global.R

library(shiny)
library(ggplot2)
library(plotly)
dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)

ui.R

ui <- navbarPage(title = "My Shiny App",
                 tabPanel(title = "App",
                          pageWithSidebar(

                            headerPanel("Shiny App"),

                            sidebarPanel(
                              actionButton(inputId = "clicks",
                                           label = "Click me")
                            ),
                            mainPanel("Main Panel",
                                      plotlyOutput("norm"),
                                      actionButton("renorm", "Resample")
                                      )
                          )         
                 ),
                 tabPanel(title = "Tab2",
                          plotOutput("unif"),
                          actionButton("reunif", "Resample")
                 ),
                 tabPanel(title = "Tab3",
                          plotOutput("chisq"),
                          actionButton("rechisq", "Resample")
                 )             
)

server.R

server <- function(input, output) {

  output$norm <- renderPlotly({
    ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
      geom_bar(stat="identity") + 
      guides(fill=FALSE)
  })

This gives me a plot like below in the Shiny app. There are three issues I have with this.

  1. When I hover the plot, I want to see the bar value, its not currently being shown
  2. The legend is still shown even when it is set to FALSE via ggplot
  3. I need to have the bars ordered with bar of largest value first - ie. Dinner first and then Lunch

enter image description here

1
do these needs to be posted as individual questions ? - user3206440
why the downvote ? - user3206440

1 Answers

0
votes

These following small modifications do all three of those things.

  • There is a bug in plotly. This is a somewhat mysterious workaround.

  • For some reason plotly overrides the guide(fill=FALSE), I don't know why really, plotly docs are not very extensive. This is another way to get rid of the guides that works with plotly.

  • ggplot plots factors on the x-axis in the order that they occur in their level. So you have to order their levels they way you need them, which I do here.

Anyway, here is the code

   library(shiny)
   library(ggplot2)
   library(plotly)
   dat <- data.frame(
      time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
      total_bill = c(14.89, 17.23)
    )

    ui <- navbarPage(title = "My Shiny App",
                     tabPanel(title = "App",
                              pageWithSidebar(

                                headerPanel("Shiny App"),

                                sidebarPanel(
                                  actionButton(inputId = "clicks",
                                               label = "Click me")
                                ),
                                mainPanel("Main Panel",
                                          plotlyOutput("norm"),
                                          actionButton("renorm", "Resample")
                                )
                              )         
                     ),
                     tabPanel(title = "Tab2",
                              plotOutput("unif"),
                              actionButton("reunif", "Resample")
                     ),
                     tabPanel(title = "Tab3",
                              plotOutput("chisq"),
                              actionButton("rechisq", "Resample")
                     )             
    )

    server <- function(input, output) {

      output$norm <- renderPlotly({
        # reorder the levels
        oldlevels <- levels(dat$time)
        neworder <- order(dat$total_bill,decreasing=T)
        dat$time <- factor(dat$time,levels=oldlevels[neworder])

        # get the plotly into a temp var
        gplt <- ggplotly(ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
          geom_bar(stat="identity") + 
            theme(legend.position="none"))

        for (i in 1:length(gplt$x$data)){
          gplt$x$data[[i]]$text <- c(gplt$x$data[[i]]$text, "") 
        }


gplt
  })

}

shinyApp(ui,server)

yielding:

enter image description here