Skip to content

ggplotly produces legend even if suppressed in ggplot #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
romunov opened this issue Jan 3, 2017 · 8 comments
Closed

ggplotly produces legend even if suppressed in ggplot #842

romunov opened this issue Jan 3, 2017 · 8 comments

Comments

@romunov
Copy link

romunov commented Jan 3, 2017

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

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.

@supersambo
Copy link

supersambo commented Jan 3, 2017

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

@bcdunbar
Copy link

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

@romunov
Copy link
Author

romunov commented Jan 11, 2017

This is more than satisfactory, thank you.

@romunov romunov closed this as completed Jan 11, 2017
@jonmcalder
Copy link

jonmcalder commented Oct 12, 2017

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().

@maxheld83
Copy link

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

@romunov romunov reopened this Nov 17, 2017
@romunov
Copy link
Author

romunov commented Nov 17, 2017

Here you go, @maxheld83.

@dralmostright
Copy link

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

@cpsievert
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants