Cannot Hide ggplot Legend

Hi, I have an application with numerous ggplot outputs and am unable to hide the legend title. Here's a reprex, is there a way I can hide the legend title?

library(shiny)
library(tidyverse)
library(plotly)

ui <- fluidPage(
titlePanel("Legend Test"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotlyOutput("Test")
)
)
)

server <- function(input, output) {
output$Test <- plotly::renderPlotly({
x <- c(1:6)
y <- c("A", "A", "A", "B", "B", "B")
z <- c("a", "a", "a", "b", "b", "b")
d <- data.frame(cbind(x, y, z))

p <-ggplot2::ggplot()+
  geom_boxplot(d, mapping = aes(x = x, y = y, fill = z)) +
  theme(legend.title = element_blank())
p <- ggplotly(p)

})
}

shinyApp(ui = ui, server = server)

Here to are setting legend.title to nothing: you are not removing the legend, only its title. What you want is:

guides(fill="none")

thanks, but looks like you did not read the question. I'm trying to hide the LEGEND TITLE not the legend,

Oops indeed, my bad. Try with:

p <- ggplotly(p) |>
  layout(legend = list(title = list(text = NULL)))

awesome, redeemed!

I've never seen this syntax, but it works. Curious if you have a resource you could point me to? i.e. |> layout()

Haha, great!

The idea is that {plotly} as a package creates its own plotly objects, with a specific format. So writing directly using the {plotly} instructions is doable, but slightly painful as it's a whole different syntax (more inspired by javascript than R in my opinion). The function ggplotly() does its best to convert a ggplot object to a plotly one, that way we avoid having to write {plotly} code ourselves. Once we have that object, we can modify it with the standard {plotly} functions. You can learn more about writing with {plotly} here.

So, you can simply do ?layout to get more information, the man page will point you to the reference with all the parameters that can be specified.

1 Like

thanks, I'll check this out, looks like possibilities are just about unlimited

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.