Plotly: ggplotly produces legend even if suppressed in ggplot

Created on 3 Jan 2017  路  8Comments  路  Source: ropensci/plotly

User on Stackoverflow reported that legends/guides are not suppressed even when explicitly told so when constructing the ggplot object. For completeness, here's an excerpt from the question

```r
library(ggplot2)
library(plotly)

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

No legend, since the information is redundant

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", stat="identity") +
guides(fill=FALSE)

ggplotly()
```

with plotly package version 4.5.6.

Most helpful comment

you can use theme(legend.position='none') which translates with ggplotly()

All 8 comments

Here is a dirty solution to get rid of the legend by disabling it in the ggplotly object:

library(ggplot2)
library(plotly)

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

# No legend, since the information is redundant
p <- ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", stat="identity") + guides(fill=FALSE)
p <- ggplotly(p)


for (i in 1:nrow(dat)){
    p$x$data[[i]]$text <- c(p$x$data[[i]]$text, "") 
    p$x$data[[i]]$showlegend <- FALSE
}
p

you can use theme(legend.position='none') which translates with ggplotly()

This is more than satisfactory, thank you.

Thanks for suggesting the "dirty hack" @supersambo

Surely there must be a more elegant way to do this though?

ggplot2 provides a few ways to suppress specific aesthetics from the legend, but none of these seem to carry through to the plotly object after a call to ggplotly().

any chance this can be re-opened @romunov? I ran into the same problem, and would love a cleaner solution long-term.

Here you go, @maxheld83.

Hi, have you guys tired hide_legend. I have tried and it worked for me.

Please use hide_legend() for cases like this. And plotly_json()/style() if you want to hide specific legend entries

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ratnanil picture ratnanil  路  3Comments

pssguy picture pssguy  路  7Comments

escorial82 picture escorial82  路  6Comments

d4tagirl picture d4tagirl  路  7Comments

fabiangehring picture fabiangehring  路  4Comments