Repository navigation
Extra (unwanted) geoms in the legend #572
Description
Activity
This was already described in
#571but probably deserves its own post...
Actually, these traces should be getting merged to produce 2 legend entries instead of 4, so I'd consider this a bug
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.
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-8attached base packages:
[1] stats graphics grDevices utils datasets methods baseother 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.14loaded 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-47Any 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.
@benjaminrobinson for my own case, I hacked the ggplotly object, deleting those I don't want
@laoyang945 What about hacking on this type of issue to merge the legends for a line and point together or maybe only showing one?
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.In R Shiny, use
plotly_json()/style()to suppress unwanted legends as mentioned in #842. In the above example, definep3<- ggplotly(p1), and thenp4 <- p3 %>% style(p3, showlegend = FALSE, traces = 1:n)## wheren = length(unique(df$z))To suppress all legends, just usehide_legend. Please note that in R Shiny,ggplotly(p2)above will merge the legends for line and point together.Reacted by Deril RajuUse 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)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)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)Reacted by Philip Shirk, philcallahanUWGB and Keith HughittReacted by philcallahanUWGBTo remove redundant tooltips / hovers just use the hoverinfo argument.
p %>% style(hoverinfo = "none", traces = 1:2)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 itemsthis worked for me! I've been looking for how to bypass this bug in R for 2 days! THANK YOU @WhyBeeYes

The code:
The ggplot version:
The ggplotly version:
Session Info: