Skip to content

Extra (unwanted) geoms in the legend #572

Description

@yankev
The code:
library(plotly)

dat1 <- data.frame(
  sex = factor(c("Female","Female","Male","Male")),
  time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(13.53, 16.81, 16.24, 17.42)
)

ggplot(data=dat1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) +
  geom_line(aes(linetype=sex), size=1) +     # Set linetype by sex
  geom_point(size=5) +         # Use larger points, fill with white
  scale_colour_hue(name="Sex",      # Set legend title
                   l=30)  +                  # Use darker colors (lightness=30)
  scale_shape_manual(name="Sex",
                     values=c(22,21)) +      # Use points with a fill color
  scale_linetype_discrete(name="Sex") +
  xlab("Time of day") + ylab("Total bill") + # Set axis labels
  ggtitle("Average bill for 2 people") +     # Set title
  theme_bw()

ggplotly()
The ggplot version:

image

The ggplotly version:

screen shot 2016-05-04 at 5 46 01 pm

Session Info:

sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.9.4 (Mavericks)

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] plotly_3.4.13 ggplot2_2.1.0

loaded via a namespace (and not attached):
[1] Rcpp_0.12.4 tidyr_0.4.1 assertthat_0.1 dplyr_0.4.3
[5] digest_0.6.9 grid_3.2.3 plyr_1.8.3 R6_2.1.2
[9] DBI_0.3.1 jsonlite_0.9.19 gtable_0.2.0 magrittr_1.5
[13] scales_0.4.0 httr_1.1.0 viridis_0.3.4 labeling_0.3
[17] tools_3.2.3 htmlwidgets_0.6 munsell_0.4.3 yaml_2.1.13
[21] parallel_3.2.3 base64enc_0.1-3 colorspace_1.2-6 htmltools_0.3.5
[25] gridExtra_2.2.1

Activity

  1. joey711 commented on May 17, 2016

    @joey711

    This was already described in
    #571

    but probably deserves its own post...

  2. cpsievert commented on May 17, 2016

    @cpsievert
    Collaborator

    Actually, these traces should be getting merged to produce 2 legend entries instead of 4, so I'd consider this a bug

  3. laoyang945 commented on Oct 6, 2016

    @laoyang945

    same here, but one more bug imo

    It's different of the "compare data on hover" command compared my version and the one on the plotly website. For my own version, all tooltips duplicate since it's a geom_line+geom_point. If I only use the geom_line, everything is fine.

    github

    R version 3.3.1 (2016-06-21)
    Platform: x86_64-apple-darwin13.4.0 (64-bit)
    Running under: OS X 10.11.6 (El Capitan)

    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    attached base packages:
    [1] stats graphics grDevices utils datasets methods base

    other attached packages:
    [1] stringr_1.1.0 adjust_0.0.5 data.table_1.9.6 httr_1.2.1 WriteXLS_4.0.0
    [6] plyr_1.8.4 reshape2_1.4.1 RMySQL_0.10.9 DBI_0.5-1 plotly_4.5.5.9000
    [11] ggplot2_2.1.0.9001 shinydashboard_0.5.3 shiny_0.14

    loaded via a namespace (and not attached):
    [1] Rcpp_0.12.7 base64enc_0.1-3 tools_3.3.1 digest_0.6.10 jsonlite_1.1 tibble_1.2
    [7] gtable_0.2.0 viridisLite_0.1.3 yaml_2.1.13 dplyr_0.5.0 htmlwidgets_0.7 grid_3.3.1
    [13] R6_2.1.3 purrr_0.2.2 tidyr_0.6.0 magrittr_1.5 scales_0.4.0.9002 htmltools_0.3.5
    [19] assertthat_0.1 mime_0.5 xtable_1.8-2 colorspace_1.2-6 httpuv_1.3.3 labeling_0.3
    [25] stringi_1.1.1 lazyeval_0.2.0 munsell_0.4.3 chron_2.3-47

  4. benjaminrobinson commented on Apr 6, 2018

    @benjaminrobinson

    Any progress on this? Just wondering. Thanks for the great package! I tried adding a "show.legend = FALSE" to the geom I was trying to hide, which worked in my ggplot2 call but not in my ggplotly call.

  5. laoyang945 commented on Apr 12, 2018

    @laoyang945

    @benjaminrobinson for my own case, I hacked the ggplotly object, deleting those I don't want

  6. benjaminrobinson commented on Apr 12, 2018

    @benjaminrobinson

    @laoyang945 What about hacking on this type of issue to merge the legends for a line and point together or maybe only showing one?

    https://plot.ly/create/?fid=RPlotBot:4128

  7. WhyBeeYes commented on Apr 1, 2020

    @WhyBeeYes

    Use aes outside the geom call to remove the extra unwanted legend in ggplotly. Consider, the basic graph p<- ggplot(df, aes(x = x, y = y)) + geom_point(aes(color=factor(z)), show.legend=FALSE), where df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4]). Then define, p1 <- p + geom_line(aes(color=factor(z), linetype=factor(z))) and p2 <- p + geom_line(aes(color=factor(z) )) + aes(linetype=factor(z))
    Please note that ggplotly(p1) gives two legends, while ggplotly(p2) gives one legend. Perhaps this solution is well known already. I could not find a solution easily. I hope this helps someone else encountering this issue.

  8. WhyBeeYes commented on Apr 2, 2020

    @WhyBeeYes

    In R Shiny, use plotly_json()/style() to suppress unwanted legends as mentioned in #842. In the above example, define p3<- ggplotly(p1), and then p4 <- p3 %>% style(p3, showlegend = FALSE, traces = 1:n) ## where n = length(unique(df$z)) To suppress all legends, just use hide_legend. Please note that in R Shiny, ggplotly(p2) above will merge the legends for line and point together.

  9. paeadin commented on Jun 24, 2021

    @paeadin

    Use aes outside the geom call to remove the extra unwanted legend in ggplotly. Consider, the basic graph p<- ggplot(df, aes(x = x, y = y)) + geom_point(aes(color=factor(z)), show.legend=FALSE), where df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4]). Then define, p1 <- p + geom_line(aes(color=factor(z), linetype=factor(z))) and p2 <- p + geom_line(aes(color=factor(z) )) + aes(linetype=factor(z))
    Please note that ggplotly(p1) gives two legends, while ggplotly(p2) gives one legend. Perhaps this solution is well known already. I could not find a solution easily. I hope this helps someone else encountering this issue.

    Hi WhyBeeYes, can you help me to apply your method to my case, plot is ok, ggploty has double legend, I want to remove the line legend:

    df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4])
    q<- ggplot(df, aes(x = x, y = y)) + geom_point(aes(group=z, color=z, shape=z)) + geom_line(aes(group=z,color=z))
    plot(q)
    ggplotly(q)

  10. paeadin commented on Jun 24, 2021

    @paeadin

    Hi WhyBeeYes, can you help me to apply your method to my case, plot is ok, ggploty has double legend, I want to remove the line legend:

    df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4])
    q<- ggplot(df, aes(x = x, y = y)) + geom_point(aes(group=z, color=z, shape=z)) + geom_line(aes(group=z,color=z))
    plot(q)
    ggplotly(q)

    found:
    q<- ggplot(df, aes(x = x, y = y)) + geom_point() + geom_line() + aes(group=z, color=z, shape=z)

  11. WhyBeeYes commented on Jul 8, 2021

    @WhyBeeYes

    You can delete the unwanted legends as shown below

    
    df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4])
    q <- ggplot(df, aes(x = x, y = y, group=z)) + geom_point(aes(shape=z)) + geom_line()  + aes(color=z)
    p <- ggplotly(q)
    qq <- p %>% style(p, showlegend = FALSE, traces = 5:8)
    
    
    
  12. tlorusso commented on Aug 20, 2021

    @tlorusso

    To remove redundant tooltips / hovers just use the hoverinfo argument.

    p %>% style(hoverinfo = "none", traces = 1:2)

  13. philcallahanUWGB commented on Nov 2, 2023

    @philcallahanUWGB

    You can delete the unwanted legends as shown below

    
    df <- data.frame( x = 1:8, y=1:16, z = LETTERS[1:4])
    q <- ggplot(df, aes(x = x, y = y, group=z)) + geom_point(aes(shape=z)) + geom_line()  + aes(color=z)
    p <- ggplotly(q)
    qq <- p %>% style(p, showlegend = FALSE, traces = 5:8) # ◄the magic line that removes unwanted legend items
    

    this worked for me! I've been looking for how to bypass this bug in R for 2 days! THANK YOU @WhyBeeYes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions