plotly.R
plotly.R copied to clipboard
Extra (unwanted) geoms in the legend
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:
The ggplotly version:
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
This was already described in https://github.com/ropensci/plotly/issues/571
but 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-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
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.
@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?
https://plot.ly/create/?fid=RPlotBot:4128
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, 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.
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)
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)
To 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 items
this worked for me! I've been looking for how to bypass this bug in R for 2 days! THANK YOU @WhyBeeYes