Skip to content

ggdendro lattice dendrograms #219

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
jackparmer opened this issue May 14, 2015 · 7 comments
Closed

ggdendro lattice dendrograms #219

jackparmer opened this issue May 14, 2015 · 7 comments

Comments

@jackparmer
Copy link
Contributor

Would be cool if we had a wrapper function to make heatmap dendrograms like this:

image

@cpsievert
Copy link
Collaborator

Do you by chance have the code for that one? It looks like plotly almost translates this more basic example, but not quite.

library(ggplot2)
library(ggdendro)
hc <- hclust(dist(USArrests), "ave")
hcdata <- dendro_data(hc, type="rectangle")
p <- ggplot() + 
    geom_segment(data=segment(hcdata), aes(x=x, y=y, xend=xend, yend=yend)) +
    geom_text(data=label(hcdata), aes(x=x, y=y, label=label, hjust=0), size=3) +
    coord_flip() + 
    scale_y_reverse(expand=c(0.2, 0))

rplot

versus

https://plot.ly/~sadjnawer/0

@jackparmer
Copy link
Contributor Author

Oh yeah, sorry! The code is from this SO issue:

http://stackoverflow.com/questions/6673162/reproducing-lattice-dendrogram-graph-with-ggplot2

One issue with the conversion is it doesn't look like ggplotly supports R grids to Plotly subplots (#220)

@cpsievert
Copy link
Collaborator

When we merge #226, it should close this issue (thanks to subplot()).

library(ggplot2)
library(ggdendro)

#dendogram data
x <- as.matrix(scale(mtcars))
dd.col <- as.dendrogram(hclust(dist(x)))
dd.row <- as.dendrogram(hclust(dist(t(x))))
dx <- dendro_data(dd.row)
dy <- dendro_data(dd.col)

# helper function for creating dendograms
ggdend <- function(df) {
  ggplot() + 
    geom_segment(data = df, aes(x=x, y=y, xend=xend, yend=yend)) +
    labs(x = "", y = "") + theme_minimal() + 
    theme(axis.text = element_blank(), axis.ticks = element_blank(),
          panel.grid = element_blank())
}

# x/y dendograms
px <- ggdend(dx$segments)
py <- ggdend(dy$segments) + coord_flip()

# heatmap
col.ord <- order.dendrogram(dd.col)
row.ord <- order.dendrogram(dd.row)
xx <- scale(mtcars)[col.ord, row.ord]
xx_names <- attr(xx, "dimnames")
df <- as.data.frame(xx)
colnames(df) <- xx_names[[2]]
df$car <- xx_names[[1]]
df$car <- with(df, factor(car, levels=car, ordered=TRUE))
mdf <- reshape2::melt(df, id.vars="car")
p <- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value))


# hide axis ticks and grid lines
eaxis <- list(
  showticklabels = FALSE,
  showgrid = FALSE,
  zeroline = FALSE
)

p_empty <- plot_ly() %>% 
  # note that margin applies to entire plot, so we can
  # add it here to make tick labels more readable
  layout(margin = list(l = 200),
         xaxis = eaxis,
         yaxis = eaxis)

subplot(px, p_empty, p, py, nrows = 2, margin = 0.01)

https://plot.ly/~sadsadf/16

@jackparmer
Copy link
Contributor Author

Carson's the man.

On Tuesday, July 28, 2015, Carson [email protected] wrote:

When we merge #226 #226, it
should close this issue (thanks to subplot()).

library(ggplot2)
library(ggdendro)
#dendogram datax <- as.matrix(scale(mtcars))dd.col <- as.dendrogram(hclust(dist(x)))dd.row <- as.dendrogram(hclust(dist(t(x))))dx <- dendro_data(dd.row)dy <- dendro_data(dd.col)

helper function for creating dendogramsggdend <- function(df) {

ggplot() +
geom_segment(data = df, aes(x=x, y=y, xend=xend, yend=yend)) +
labs(x = "", y = "") + theme_minimal() +
theme(axis.text = element_blank(), axis.ticks = element_blank(),
panel.grid = element_blank())
}

x/y dendogramspx <- ggdend(dx$segments)py <- ggdend(dy$segments) + coord_flip()

heatmapcol.ord <- order.dendrogram(dd.col)row.ord <- order.dendrogram(dd.row)xx <- scale(mtcars)[col.ord, row.ord]xx_names <- attr(xx, "dimnames")df <- as.data.frame(xx)

colnames(df) <- xx_names[[2]]df$car <- xx_names[[1]]df$car <- with(df, factor(car, levels=car, ordered=TRUE))mdf <- reshape2::melt(df, id.vars="car")p <- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value))

hide axis ticks and grid lineseaxis <- list(

showticklabels = FALSE,
showgrid = FALSE
)
p_empty <- plot_ly() %>%

note that margin applies to entire plot, so we may

add it here to make tick labels more readable

layout(margin = list(l = 200),
xaxis = eaxis,
yaxis = eaxis)

subplot(px, p_empty, p, py, nrows = 2, margin = 0.01)

[image: https://plot.ly/~agvd/550]
https://camo.githubusercontent.com/c7d99feea916547f38edd6e0e62826fa1558f8a2/68747470733a2f2f706c6f742e6c792f253745616776642f3535302e706e67


Reply to this email directly or view it on GitHub
#219 (comment).

@cpsievert
Copy link
Collaborator

@jackparmer perhaps we should put this example somewhere so it's more visible? Any ideas where?

@chriddyp
Copy link
Member

chriddyp commented Aug 3, 2015

sweet! I'll add it to https://github.com/plotly/documentation/tree/source

@chriddyp
Copy link
Member

chriddyp commented Aug 3, 2015

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

3 participants