Skip to content

Commit 42332d6

Browse files
committed
ggdendro
1 parent 89e9cb0 commit 42332d6

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: ggdendro lattice dendrograms | plotly
3+
name: ggdendro lattice dendrograms
4+
permalink: ggplot2/ggdendro-dendrograms
5+
description: How to make a dendrogram with ggdendro and ggplot2
6+
layout: base
7+
thumbnail: /images/dendrogram.png
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: chart_type
12+
order: 14
13+
---
14+
15+
```{r, echo = FALSE, message=FALSE}
16+
knitr::opts_chunk$set(message = FALSE)
17+
Sys.setenv("plotly_username"="RPlotBot")
18+
Sys.setenv("plotly_api_key"="q0lz6r5efr")
19+
```
20+
21+
22+
```{r, message=FALSE, height=900}
23+
library(ggplot2)
24+
library(ggdendro)
25+
library(plotly)
26+
27+
#dendogram data
28+
x <- as.matrix(scale(mtcars))
29+
dd.col <- as.dendrogram(hclust(dist(x)))
30+
dd.row <- as.dendrogram(hclust(dist(t(x))))
31+
dx <- dendro_data(dd.row)
32+
dy <- dendro_data(dd.col)
33+
34+
# helper function for creating dendograms
35+
ggdend <- function(df) {
36+
ggplot() +
37+
geom_segment(data = df, aes(x=x, y=y, xend=xend, yend=yend)) +
38+
labs(x = "", y = "") + theme_minimal() +
39+
theme(axis.text = element_blank(), axis.ticks = element_blank(),
40+
panel.grid = element_blank())
41+
}
42+
43+
# x/y dendograms
44+
px <- ggdend(dx$segments)
45+
py <- ggdend(dy$segments) + coord_flip()
46+
47+
# heatmap
48+
col.ord <- order.dendrogram(dd.col)
49+
row.ord <- order.dendrogram(dd.row)
50+
xx <- scale(mtcars)[col.ord, row.ord]
51+
xx_names <- attr(xx, "dimnames")
52+
df <- as.data.frame(xx)
53+
colnames(df) <- xx_names[[2]]
54+
df$car <- xx_names[[1]]
55+
df$car <- with(df, factor(car, levels=car, ordered=TRUE))
56+
mdf <- reshape2::melt(df, id.vars="car")
57+
p <- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value))
58+
59+
60+
# hide axis ticks and grid lines
61+
eaxis <- list(
62+
showticklabels = FALSE,
63+
showgrid = FALSE,
64+
zeroline = FALSE
65+
)
66+
67+
p_empty <- plot_ly(filename="r-docs/dendrogram") %>%
68+
# note that margin applies to entire plot, so we can
69+
# add it here to make tick labels more readable
70+
layout(margin = list(l = 200),
71+
xaxis = eaxis,
72+
yaxis = eaxis)
73+
74+
subplot(px, p_empty, p, py, nrows = 2, margin = 0.01)
75+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: ggdendro lattice dendrograms | plotly
3+
name: ggdendro lattice dendrograms
4+
permalink: ggplot2/ggdendro-dendrograms
5+
description: How to make a dendrogram with ggdendro and ggplot2
6+
layout: base
7+
thumbnail: /images/dendrogram.png
8+
language: ggplot2
9+
page_type: example_index
10+
has_thumbnail: true
11+
display_as: chart_type
12+
order: 14
13+
---
14+
15+
16+
17+
18+
19+
```r
20+
library(ggplot2)
21+
library(ggdendro)
22+
library(plotly)
23+
24+
#dendogram data
25+
x <- as.matrix(scale(mtcars))
26+
dd.col <- as.dendrogram(hclust(dist(x)))
27+
dd.row <- as.dendrogram(hclust(dist(t(x))))
28+
dx <- dendro_data(dd.row)
29+
dy <- dendro_data(dd.col)
30+
31+
# helper function for creating dendograms
32+
ggdend <- function(df) {
33+
ggplot() +
34+
geom_segment(data = df, aes(x=x, y=y, xend=xend, yend=yend)) +
35+
labs(x = "", y = "") + theme_minimal() +
36+
theme(axis.text = element_blank(), axis.ticks = element_blank(),
37+
panel.grid = element_blank())
38+
}
39+
40+
# x/y dendograms
41+
px <- ggdend(dx$segments)
42+
py <- ggdend(dy$segments) + coord_flip()
43+
44+
# heatmap
45+
col.ord <- order.dendrogram(dd.col)
46+
row.ord <- order.dendrogram(dd.row)
47+
xx <- scale(mtcars)[col.ord, row.ord]
48+
xx_names <- attr(xx, "dimnames")
49+
df <- as.data.frame(xx)
50+
colnames(df) <- xx_names[[2]]
51+
df$car <- xx_names[[1]]
52+
df$car <- with(df, factor(car, levels=car, ordered=TRUE))
53+
mdf <- reshape2::melt(df, id.vars="car")
54+
p <- ggplot(mdf, aes(x = variable, y = car)) + geom_tile(aes(fill = value))
55+
56+
57+
# hide axis ticks and grid lines
58+
eaxis <- list(
59+
showticklabels = FALSE,
60+
showgrid = FALSE,
61+
zeroline = FALSE
62+
)
63+
64+
p_empty <- plot_ly(filename="r-docs/dendrogram") %>%
65+
# note that margin applies to entire plot, so we can
66+
# add it here to make tick labels more readable
67+
layout(margin = list(l = 200),
68+
xaxis = eaxis,
69+
yaxis = eaxis)
70+
71+
subplot(px, p_empty, p, py, nrows = 2, margin = 0.01)
72+
```
73+
74+
<iframe height="900" id="igraph" scrolling="no" seamless="seamless" src="https://plot.ly/~RPlotBot/1166.embed" width="800" frameBorder="0"></iframe>

all_static/images/dendrogram.png

8.4 KB
Loading

0 commit comments

Comments
 (0)