plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.56k stars 626 forks source link

geom_dl/geom_label_repel not working with ggplotly #1244

Closed realkrantz closed 6 years ago

realkrantz commented 6 years ago

I would like to propose the implementation of geom_dl/geom_label_repel in plotly because I am unable to convert my ggplots with these geoms to plotly.

I provide a sample data and code to illustrate this:

sample data

library("ggrepel")
library(directlabels)
library(ggplot2)
library(plotly)
library(zoib)
library(dplyr)
data("GasolineYield", package = "zoib")
re.md <- zoib(yield ~ temp | 1 | 1, data=GasolineYield, 
joint = FALSE, random=1, EUID=GasolineYield$batch, 
zero.inflation = FALSE, one.inflation = FALSE, 
n.iter=3200, n.thin=15, n.burn=200)
pred <- pred.zoib(re.md, data.frame(temp = seq(100, 600, 0.01)))
df<- data.frame(temp = seq(100, 600, 0.01), 
                  pred$summary)
df$n<-sample(1:2, 50001, replace=TRUE)
df<- subset(df, select=c("temp", "n", "mean"))
df<-df %>%
         mutate(mean = case_when
                (n == 1 ~ df$mean/2, 
                 n == 2 ~ df$mean))
colnames(df)[2] <- "group"
df$group<-as.factor(df$group)

using geom_dl/geom_label_repel with ggplot2

pp<-ggplot() + 
geom_line(data = df, aes(y = mean, x = temp,
             group = group, 
             colour = group)) +
geom_dl(data=df, aes(y = mean, x = temp, label = group), 
          method = list(dl.combine("first.points", "last.points"), 
          cex = 0.8)) +
xlim(200, 600) + 
theme_classic()
pp

geom_dl/geom_label_repel not working with ggplotly

ggplotly(pp)
Warning message:
In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
geom_GeomDl() has yet to be implemented in plotly.
If you'd like to see this geom implemented,
Please open an issue with your example code at
https://github.com/ropensci/plotly/issues
cpsievert commented 6 years ago

Added directlabels to #566

moutikabdessabour commented 3 years ago

@cpsievert I don't think it makes sense to work on geom_label_repel. Because we have the hover tooltip. I implemented a fix for geom_dl part of this issue.

aloboa commented 2 years ago

@moutikabdessabour I get "geom_GeomDl() has yet to be implemented in plotly." See this example from https://stackoverflow.com/questions/29357612/plot-labels-at-ends-of-lines

temp.dat <- structure(list(
  Year = c("2003", "2004", "2005", "2006", "2007", 
           "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", 
           "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", 
           "2012", "2013", "2014", "2003", "2004", "2005", "2006", "2007", 
           "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", 
           "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", 
           "2012", "2013", "2014"),
  State = structure(c(1L, 1L, 1L, 1L, 
                      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                      2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                      4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("VIC", "NSW", "QLD", "WA"), class = "factor"), 
  Capex = c(5.35641472365348, 
            5.76523240652641, 5.24727577535625, 5.57988239709746, 5.14246402568366, 
            4.96786288162828, 5.493190785287, 6.08500616799372, 6.5092228474591, 
            7.03813541623157, 8.34736513875897, 9.04992300432169, 7.15830329914056, 
            7.21247045701994, 7.81373928617117, 7.76610217197542, 7.9744994967006, 
            7.93734452080786, 8.29289899132255, 7.85222269563982, 8.12683746325074, 
            8.61903784301649, 9.7904327253813, 9.75021175267288, 8.2950673974226, 
            6.6272705639724, 6.50170524635367, 6.15609626379471, 6.43799637295979, 
            6.9869551384028, 8.36305663640294, 8.31382617231745, 8.65409824343971, 
            9.70529678167458, 11.3102788081848, 11.8696420977237, 6.77937303542605, 
            5.51242844820827, 5.35789621712839, 4.38699327451101, 4.4925792218211, 
            4.29934654081527, 4.54639175257732, 4.70040615159951, 5.04056109514957, 
            5.49921208937735, 5.96590909090909, 6.18700407463007)), 
  class = "data.frame", row.names = c(NA, -48L), .Names = c("Year", "State", "Capex"))

library(ggplot2)
library(dplyr)
library(directlabels)
library(plotly)

g1 <- ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) + 
  geom_line() +
  scale_colour_discrete(guide = 'none') +
  scale_x_discrete(expand=c(0, 1)) +
  geom_dl(aes(label = State), method = list(dl.combine("first.points", "last.points")), cex = 0.8) 
g1
ggplotly(g1)

Warning messages:
1: In geom2trace.default(dots[[1L]][[4L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDl() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
2: In geom2trace.default(dots[[1L]][[4L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDl() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
3: In geom2trace.default(dots[[1L]][[4L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDl() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues
4: In geom2trace.default(dots[[1L]][[4L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomDl() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues