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))
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.
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
Most helpful comment
you can use
theme(legend.position='none')which translates withggplotly()