Skip to content

ggplotly conversion of dendextend ggplot #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jrowen opened this issue Nov 22, 2016 · 7 comments
Closed

ggplotly conversion of dendextend ggplot #807

jrowen opened this issue Nov 22, 2016 · 7 comments

Comments

@jrowen
Copy link

jrowen commented Nov 22, 2016

I noticed a few differences when converting a ggplot created via dendextend to a plotly plot. The text alignment is lost (should remain to the right of the marker) and the legend re-appears.

library(ggplot2)
library(plotly)
library(dendextend)

dend <- iris[1:30,-5] %>% dist %>% hclust %>% as.dendrogram %>%
  set("branches_k_color", k=3) %>% set("branches_lwd", c(1.5,1,1.5)) %>%
  set("branches_lty", c(1,1,3,1,1,2)) %>%
  set("labels_colors") %>% set("labels_cex", c(.9,1.2)) %>% 
  set("nodes_pch", 19) %>% set("nodes_col", c("orange", "black", "plum", NA))

ggd1 <- as.ggdend(dend)

p = ggplot(ggd1, horiz = TRUE, theme = NULL)
p

ggplotly(p)
@jrowen
Copy link
Author

jrowen commented Nov 29, 2016

It might be possible to fix the text alignment by updating geom2trace.GeomText to inlcude

    textposition = paste0(
      ifelse(data[["vjust"]] < 0.5, "top ",
             ifelse(data[["vjust"]] > 0.5, "bottom ", "")
      ),
      ifelse(data[["hjust"]] < 0.5, "right ",
             ifelse(data[["vjust"]] > 0.5, "left ", "center")
      )
    ),

This isn't a perfect hjust/vjust mapping but is closer to the intended text offset.

@jrowen
Copy link
Author

jrowen commented Nov 29, 2016

It also doesn't look like ggplotly handles -Inf/Inf values passed to geom_rect. It might be possible to make this work by updating to_basic.GeomRect to use

  if (layout$xanchor == "x") {
    dat <- with(data, {
      rbind(cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin),
                  y = ifelse(ymin == -Inf, layout$y_min, ymin), others),
            cbind(x = ifelse(xmin == -Inf, layout$x_min, xmin),
                  y = ifelse(ymax == Inf, layout$y_max, ymax), others),
            cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax),
                  y = ifelse(ymax == Inf, layout$y_max, ymax), others),
            cbind(x = ifelse(xmax == -Inf, layout$x_max, xmax),
                  y = ifelse(ymin == -Inf, layout$y_min, ymin), others))
    })
  } else {
    dat <- with(data, {
      rbind(cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin),
                  y = ifelse(ymin == -Inf, layout$x_min, ymin), others),
            cbind(x = ifelse(xmin == -Inf, layout$y_min, xmin),
                  y = ifelse(ymax == Inf, layout$x_max, ymax), others),
            cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax),
                  y = ifelse(ymax == Inf, layout$x_max, ymax), others),
            cbind(x = ifelse(xmax == -Inf, layout$y_max, xmax),
                  y = ifelse(ymin == -Inf, layout$x_min, ymin), others))
    })
  }

There's probably a more elegant way to do this.

@jrowen
Copy link
Author

jrowen commented Nov 29, 2016

One more item I noticed, it doesn't appear that ggplotly is handling guide = "none". One possible fix could be to also filter out scales where the guide was set to none.

npscales$scales <- Filter(function(x) x$guide != "none", npscales$scales)

@jrowen
Copy link
Author

jrowen commented Nov 30, 2016

Moved to pull request #818

@jrowen jrowen closed this as completed Nov 30, 2016
@talgalili
Copy link
Contributor

Thank you for your work @jrowen

@jrowen
Copy link
Author

jrowen commented Nov 30, 2016

Actually, I should be thanking you for creating the dendextend package. It's been very useful.

@talgalili
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants