0

the legend doesn't appear in ggplotly when I use guide in ggplot :

library(plotly)
library(ggplot2)
library(dplyr)
diamonds %>% 
  group_by(cut,color) %>% 
  summarise(price = mean(price)) %>% 
  ggplot(aes(x=cut,y=price,fill= color))+
  geom_bar(stat='identity')  +  
  guides(fill = guide_legend(reverse = TRUE)) -> t
ggplotly(t) 

If I remove the line with guides, no problem :

diamonds %>% 
  group_by(cut,color) %>% 
  summarise(price = mean(price)) %>% 
  ggplot(aes(x=cut,y=price,fill= color))+
  geom_bar(stat='identity') -> t
ggplotly(t) 

How could I use the guide but still keep the legend in ggplotly ?

Edit : As spotted by @Stefan, the problem was that I used plotly 4.10.3, after updating, legend appears normally.

2
  • Your code works fine. But I remember that there was an issue with guides() in the past, i.e. check your plotly version. Mine is 4.10.4. Commented Apr 5, 2024 at 9:28
  • ... but note that reverse=TRUE seems to have no effect. So you probably have to that by converting to a factor with reversed levels. Commented Apr 5, 2024 at 9:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.