forked from jbkunst/highcharter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdendro.R
66 lines (52 loc) · 1.5 KB
/
dendro.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
rm(list = ls())
library("highcharter")
library("ggdendro")
library("dplyr")
x <- iris[, -5] %>% dist %>% hclust %>% as.dendrogram()
x <- mtcars %>% dist %>% hclust %>% as.dendrogram()
hchart(as.dendrogram(hclust(dist(mtcars))))
attr(x, "class") <- "dendrogram"
class(x)
plot(x)
hchart(x)
highcharter:::hchart.dendrogram(x)
hc <- hchart(x)
#' @importFrom ggdendro dendro_data
hchart.dendrogram <- function(x, ...) {
dddata <- dendro_data(x)
by_row2 <- function(.d, .f, ...) {
purrr::by_row(.d, .f, ..., .to = "out")[["out"]]
}
dsseg <- dddata$segments %>%
mutate(x = x - 1, xend = xend - 1) %>%
by_row2(function(x){
list(list(x = x$x, y = x$y), list(x = x$xend, y = x$yend))
})
hc <- highchart() %>%
hc_plotOptions(
series = list(
lineWidth = 2,
showInLegend = FALSE,
marker = list(radius = 0),
enableMouseTracking = FALSE
)
) %>%
hc_xAxis(categories = dddata$labels$label,
tickmarkPlacement = "on") %>%
hc_colors(list(hex_to_rgba("#606060")))
for (i in seq_along(dsseg)) {
hc <- hc %>% hc_add_series(data = dsseg[[i]], type = "scatter")
}
hc
}
hc %>%
hc_chart(type = "column")
hc %>%
hc_chart(type = "bar") %>%
hc_xAxis(tickLength = 0)
hc %>% hc_chart(type = "bar") %>%
hc_yAxis(reversed = TRUE) %>%
hc_xAxis(opposite = TRUE, tickLength = 0)
Shc %>% hc_chart(polar = TRUE) %>%
hc_yAxis(reversed = TRUE, visible = TRUE) %>%
hc_xAxis(gridLineWidth = 0, lineWidth = 0)