plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

Updates to better support dendrograms

Open jrowen opened this issue 9 years ago • 9 comments

These changes were made to better support dendrograms, particularly those created with dendextend. It adds support for

  • fontface in geom_text
  • a rough translation of hjust and vjust in geom_text
  • -Inf and Inf in geom_rect
  • guide = "none"

Feedback is very welcome. Below is some sample code.

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)
ggplotly(p)

p1 = ggplot(cars) +
  geom_point(aes(x = speed, y = dist)) +
  geom_rect(xmin = 5, xmax = 10, ymin = -Inf, ymax = Inf)
ggplotly(p1)

p2 = ggplot(cars) +
  geom_point(aes(x = speed, y = dist)) +
  geom_rect(ymin = 25, ymax = 50, xmin = -Inf, xmax = Inf)
ggplotly(p2)

p3 = ggplot(cars) +
  geom_point(aes(x = speed, y = dist)) +
  geom_rect(xmin = 5, xmax = 10, ymin = -Inf, ymax = Inf) +
  coord_flip()
ggplotly(p3)

p4 = ggplot(cars) +
  geom_point(aes(x = speed, y = dist)) +
  geom_rect(ymin = 25, ymax = 50, xmin = -Inf, xmax = Inf) +
  coord_flip()
ggplotly(p4)

p5 = ggplot(data.frame(x = seq(5, 25, 5), y = 60)) +
  geom_point(aes(x = x, y = y)) +
  geom_text(x = 5, y = 60, label = "nothing") +
  geom_text(x = 10, y = 60, label = "bold", fontface = "bold", hjust = 0, vjust = 0) +
  geom_text(x = 15, y = 60, label = "italic", fontface = "italic", hjust = 1, vjust = 1) +
  geom_text(x = 20, y = 60, label = "bold italic", fontface = "bold italic", hjust = 0, vjust = 1) +
  geom_text(x = 25, y = 60, label = "plain", fontface = "plain", hjust = 1, vjust = 0)
ggplotly(p5)

jrowen avatar Nov 30 '16 14:11 jrowen

Sorry for the delay @jrowen, this is looking pretty good...before I merge, would you mind writing a couple simple tests?

cpsievert avatar Jan 02 '17 20:01 cpsievert

Sorry, I too have been away from this for a while. I'll see if I can pull those together shortly. Thanks.

jrowen avatar Feb 20 '17 22:02 jrowen

Hi @jrowen Any chance you could get some tests made for this? This would be helpful for heatmaply (since it now has a plot_method="plotly" which could use these features).

Thanks!

talgalili avatar Apr 12 '17 09:04 talgalili

I added some checks for geom_text and geom_rect, but I'm not sure how to test the guide = "none" change without introducing a dependency on dendextend (see code below which now works as expected). It also looks like the R:devel build is failing with the R:release build is fine.

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)
ggplotly(p)

jrowen avatar Apr 12 '17 13:04 jrowen

Don't worry about the build failure @jrowen. I'll have a closer look here next week

cpsievert avatar Apr 13 '17 21:04 cpsievert

Hi Carson, Any chance you can give this a look?

Thanks, Tal

talgalili avatar May 10 '17 08:05 talgalili

I may get to this next week. #929 will take precedence.

cpsievert avatar May 11 '17 00:05 cpsievert

Hi @jrowen, sorry for forgetting about this PR! I thought I was going to have the bandwidth to complete #929, but I'm not sure anymore...I have a couple small questions about this PR, but we should try to get this merged before I submit to CRAN by the end of June

cpsievert avatar May 29 '18 17:05 cpsievert

Hey @cpsievert :) Any chance to add this support?

talgalili avatar Jan 29 '21 10:01 talgalili