Search code examples
rggplot2shinytransparencygeom-col

R shiny and ggplot2: How to make border of geom_col transparent?


I want to make the border of geom_col transparent. It works when only using ggplot2:

library(ggplot2)

dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794), 
                          variable = rep('A',5), value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))

p <- ggplot( dataToPlot , aes(x=Freq, y = value, group = variable   )  ) +  #
  # geom_bar(stat = "bin") fill = variable, 
  geom_col( mapping = aes(col = variable, fill = variable), colour = F,  alpha = 0.2, orientation = "x", position = "dodge") + 
  # scale_linetype(aes(linetype = 0))
  guides(color = FALSE)

dev.new(); p

However, the exact same code with shiny gives the error: "Error: invalid color name 'FALSE'"

library(ggplot2)
library(shiny)

dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794), 
                          variable = rep('A',5), value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))

ui <- fluidPage( 
  useShinyjs(),
  fluidRow( 
    column(8,
           plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlotly({
    p <- ggplot( dataToPlot , aes(x=Freq, y = value, group = variable   )  ) +  #
      # geom_bar(stat = "bin") fill = variable, 
      geom_col( mapping = aes(col = variable, fill = variable), colour = F,  alpha = 0.2, orientation = "x", position = "dodge") + 
      # scale_linetype(aes(linetype = 0))
      guides(color = FALSE)

  })
}

shinyApp(ui,server)

What am I doing wrong?


Solution

  • There are some mistakes you are doing.

    First, you forgot to mention that you are also using the packages shinyjs and plotly.

    Second, you are using renderPlotly in the server part, but calling plotOutput in the ui. The correct is plotlyOutput in the ui, since you want a plotly graphic.

    The other thing is: since you want a plotly type of graphic, you have to transform your ggplot graphic p to a plotly one. Therefore, you should add ggplotly(p) into the server part.

    Finally, in order to solve the problem with the borders, you should use colour = NA instead of colour = FALSE. The second way works with ggplot2, but not with plotly. I do not know exactly why. Perhaps someone could clarify this.

    So, your code should look like this:

    library(ggplot2)
    library(shiny)
    library(shinyjs)
    library(plotly)
    
    dataToPlot <- data.frame(Freq = c(0.0000000, 0.7092199, 1.4184397, 2.1276596, 2.8368794),
                             variable = rep('A',5), 
                             value = c(43089.76, 62923.17, 35446.15, 29553.76, 22433.08))
    
    ui <- fluidPage( 
      useShinyjs(),
      fluidRow( 
        column(8,
               plotlyOutput("plot")
        )
      )
    )
    
    server <- function(input, output) {
      output$plot <- renderPlotly({
        p <- ggplot(dataToPlot , aes(x=Freq, y = value, group = variable)) +
          geom_col(mapping = aes(col = variable, fill = variable), colour = NA,  alpha = 0.2, orientation = "x", position = "dodge") + 
          guides(color = FALSE)
    
        ggplotly(p)
      })
    }
    
    shinyApp(ui,server)