Drawing dendrogram of time series data in d3heatmap

51 views Asked by At

I am trying to draw a dendrogram of a time-series data using d3heatmap library.

Right now, I cannot produce a nice heatmap with dendrogram with the following code:

library(d3heatmap)
d3heatmap(hour_flow, colors = "RdYlBu",
          dendrogram = "row",
          k_rows = 3)

dendrogram result

Here is the reproducible data:

structure(list(county = c("강남구", "강남구", "강남구", "강남구", 
"강남구", "강남구", "강남구", "강남구", "강남구", "강남구", "강남구", 
"강남구", "강남구", "강남구", "강남구", "강남구", "강남구", "강남구", 
"강남구", "강남구", "강남구", "강남구", "강남구", "강남구", "강동구", 
"강동구", "강동구", "강동구", "강동구", "강동구", "강동구", "강동구", 
"강동구", "강동구", "강동구", "강동구", "강동구", "강동구", "강동구", 
"강동구", "강동구", "강동구", "강동구", "강동구", "강동구", "강동구", 
"강동구", "강동구", "강북구", "강북구", "강북구", "강북구", "강북구", 
"강북구", "강북구", "강북구", "강북구", "강북구", "강북구", "강북구", 
"강북구", "강북구", "강북구", "강북구", "강북구", "강북구", "강북구", 
"강북구", "강북구", "강북구", "강북구", "강북구", "강서구", "강서구", 
"강서구", "강서구", "강서구", "강서구", "강서구", "강서구", "강서구", 
"강서구", "강서구", "강서구", "강서구", "강서구", "강서구", "강서구", 
"강서구", "강서구", "강서구", "강서구", "강서구", "강서구", "강서구", 
"강서구", "관악구", "관악구", "관악구", "관악구", "관악구", "관악구", 
"관악구", "관악구", "관악구", "관악구", "관악구", "관악구", "관악구", 
"관악구", "관악구", "관악구", "관악구", "관악구", "관악구", "관악구", 
"관악구", "관악구", "관악구", "관악구", "광진구", "광진구", "광진구", 
"광진구", "광진구", "광진구", "광진구", "광진구", "광진구", "광진구", 
"광진구", "광진구", "광진구", "광진구", "광진구", "광진구", "광진구", 
"광진구", "광진구", "광진구", "광진구", "광진구", "광진구", "광진구", 
"구로구", "구로구", "구로구", "구로구", "구로구", "구로구", "구로구", 
"구로구", "구로구", "구로구", "구로구", "구로구", "구로구", "구로구", 
"구로구", "구로구", "구로구", "구로구", "구로구", "구로구", "구로구", 
"구로구", "구로구", "구로구", "금천구", "금천구", "금천구", "금천구", 
"금천구", "금천구", "금천구", "금천구", "금천구", "금천구", "금천구", 
"금천구", "금천구", "금천구", "금천구", "금천구", "금천구", "금천구", 
"금천구", "금천구", "금천구", "금천구", "금천구", "금천구", "노원구", 
"노원구", "노원구", "노원구", "노원구", "노원구", "노원구", "노원구", 
"노원구", "노원구", "노원구", "노원구", "노원구", "노원구", "노원구", 
"노원구", "노원구", "노원구", "노원구", "노원구", "노원구", "노원구", 
"노원구", "노원구", "도봉구", "도봉구", "도봉구", "도봉구", "도봉구", 
"도봉구", "도봉구", "도봉구", "도봉구", "도봉구", "도봉구", "도봉구", 
"도봉구", "도봉구", "도봉구", "도봉구", "도봉구", "도봉구", "도봉구", 
"도봉구", "도봉구", "도봉구", "도봉구", "도봉구", "동대문구", 
"동대문구", "동대문구", "동대문구", "동대문구", "동대문구", "동대문구", 
"동대문구", "동대문구", "동대문구", "동대문구", "동대문구", "동대문구", 
"동대문구", "동대문구", "동대문구", "동대문구", "동대문구", "동대문구", 
"동대문구", "동대문구", "동대문구", "동대문구", "동대문구", "동작구", 
"동작구", "동작구", "동작구", "동작구", "동작구", "동작구", "동작구", 
"동작구", "동작구", "동작구", "동작구", "동작구", "동작구", "동작구", 
"동작구", "동작구", "동작구", "동작구", "동작구", "동작구", "동작구", 
"동작구", "동작구", "마포구", "마포구", "마포구", "마포구", "마포구", 
"마포구", "마포구", "마포구", "마포구", "마포구", "마포구", "마포구", 
"마포구", "마포구", "마포구", "마포구", "마포구", "마포구", "마포구", 
"마포구", "마포구", "마포구", "마포구", "마포구", "서대문구", 
"서대문구", "서대문구", "서대문구", "서대문구", "서대문구", "서대문구", 
"서대문구", "서대문구", "서대문구", "서대문구", "서대문구", "서대문구", 
"서대문구", "서대문구", "서대문구", "서대문구", "서대문구", "서대문구", 
"서대문구", "서대문구", "서대문구", "서대문구", "서대문구", "서초구", 
"서초구", "서초구", "서초구", "서초구", "서초구", "서초구", "서초구", 
"서초구", "서초구", "서초구", "서초구", "서초구", "서초구", "서초구", 
"서초구", "서초구", "서초구", "서초구", "서초구", "서초구", "서초구", 
"서초구", "서초구", "성동구", "성동구", "성동구", "성동구", "성동구", 
"성동구", "성동구", "성동구", "성동구", "성동구", "성동구", "성동구", 
"성동구", "성동구", "성동구", "성동구", "성동구", "성동구", "성동구", 
"성동구", "성동구", "성동구", "성동구", "성동구", "성북구", "성북구", 
"성북구", "성북구", "성북구", "성북구", "성북구", "성북구", "성북구", 
"성북구", "성북구", "성북구", "성북구", "성북구", "성북구", "성북구", 
"성북구", "성북구", "성북구", "성북구", "성북구", "성북구", "성북구", 
"성북구", "송파구", "송파구", "송파구", "송파구", "송파구", "송파구", 
"송파구", "송파구", "송파구", "송파구", "송파구", "송파구", "송파구", 
"송파구", "송파구", "송파구", "송파구", "송파구", "송파구", "송파구", 
"송파구", "송파구", "송파구", "송파구", "양천구", "양천구", "양천구", 
"양천구", "양천구", "양천구", "양천구", "양천구", "양천구", "양천구", 
"양천구", "양천구", "양천구", "양천구", "양천구", "양천구", "양천구", 
"양천구", "양천구", "양천구", "양천구", "양천구", "양천구", "양천구", 
"영등포구", "영등포구", "영등포구", "영등포구", "영등포구", "영등포구", 
"영등포구", "영등포구", "영등포구", "영등포구", "영등포구", "영등포구", 
"영등포구", "영등포구", "영등포구", "영등포구", "영등포구", "영등포구", 
"영등포구", "영등포구", "영등포구", "영등포구", "영등포구", "영등포구", 
"용산구", "용산구", "용산구", "용산구", "용산구", "용산구", "용산구", 
"용산구", "용산구", "용산구", "용산구", "용산구", "용산구", "용산구", 
"용산구", "용산구", "용산구", "용산구", "용산구", "용산구", "용산구", 
"용산구", "용산구", "용산구", "은평구", "은평구", "은평구", "은평구", 
"은평구", "은평구", "은평구", "은평구", "은평구", "은평구", "은평구", 
"은평구", "은평구", "은평구", "은평구", "은평구", "은평구", "은평구", 
"은평구", "은평구", "은평구", "은평구", "은평구", "은평구", "종로구", 
"종로구", "종로구", "종로구", "종로구", "종로구", "종로구", "종로구", 
"종로구", "종로구", "종로구", "종로구", "종로구", "종로구", "종로구", 
"종로구", "종로구", "종로구", "종로구", "종로구", "종로구", "종로구", 
"종로구", "종로구", "중구", "중구", "중구", "중구", "중구", "중구", 
"중구", "중구", "중구", "중구", "중구", "중구", "중구", "중구", 
"중구", "중구", "중구", "중구", "중구", "중구", "중구", "중구", 
"중구", "중구", "중랑구", "중랑구", "중랑구", "중랑구", "중랑구", 
"중랑구", "중랑구", "중랑구", "중랑구", "중랑구", "중랑구", "중랑구", 
"중랑구", "중랑구", "중랑구", "중랑구", "중랑구", "중랑구", "중랑구", 
"중랑구", "중랑구", "중랑구", "중랑구", "중랑구"), start_hour = c(0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L), in_minus_out = c(-0.04, 
-0.05, -0.02, -0.08, 0.02, -0.08, 0.07, 0.22, 0.14, 0.13, 0.08, 
0.04, 0.03, 0.02, -0.02, -0.03, -0.06, -0.11, -0.14, -0.06, -0.09, 
-0.07, -0.06, -0.01, 0.02, 0.09, 0.04, 0.06, 0.03, -0.06, -0.14, 
-0.16, -0.1, -0.08, -0.06, -0.04, -0.04, -0.04, -0.02, 0.02, 
0.01, 0.07, 0.13, 0.07, 0.04, 0.06, 0.07, 0.08, 0.01, 0.06, 0.06, 
0.07, 0.02, 0.06, -0.13, -0.05, 0, -0.05, -0.05, -0.04, 0.01, 
-0.04, -0.04, -0.06, -0.06, 0.08, 0.03, 0.04, -0.01, 0, 0.02, 
-0.03, 0.04, 0.04, 0.02, 0.00999999999999998, -0.01, -0.09, -0.03, 
-0.04, -0.01, -0.04, -0.04, -0.04, -0.04, -0.06, -0.03, -0.05, 
0.02, 0.03, 0.04, 0.04, 0.03, 0.02, 0.05, 0.02, 0.12, 0.05, 0.01, 
0.04, -0.05, -0.1, -0.01, -0.19, -0.26, -0.15, -0.08, -0.06, 
-0.04, -0.06, -0.04, -0.04, 0.02, 0.07, 0.14, 0.02, 0.00999999999999998, 
0.02, 0.03, 0.03, -0.06, -0.04, -0.08, -0.09, -0.08, -0.07, -0.14, 
-0.09, -0.25, -0.08, 0.03, 0.03, 0, 0.03, 0.06, 0.09, 0.05, 0.08, 
0.08, 0.03, 0.03, 0.02, -0.02, -0.02, -0.09, 0.04, 0.01, -0.05, 
0.16, 0.1, 0.03, -0.07, -0.04, 0.01, -0.02, -0.05, -0.05, -0.04, 
-0.05, -0.01, -0.02, 0.04, 0.04, 0, 0.04, 0.08, 0.02, 0, 0.05, 
0.02, 0.04, -0.06, 0.02, 0.32, 0.14, 0.15, 0.19, 0.18, 0.05, 
0.01, -0.03, -0.07, -0.12, -0.11, -0.07, -0.14, -0.05, 0.00999999999999995, 
0, 0.04, 0.07, 0.04, -0.02, -0.01, -0.01, -0.01, -0.02, -0.01, 
0, -0.1, 0.02, 0.09, -0.04, -0.01, 0.03, 0, -0.02, -0.02, -0.01, 
0.00999999999999995, 0.07, 0.02, 0, -0.02, -0.03, -0.02, 0.01, 
-0.01, -0.0499999999999999, 0, 0.1, -0.0700000000000001, -0.1, 
-0.01, -0.11, -0.13, -0.04, -0.05, -0.07, -0.05, 0.02, -0.01, 
0.02, -0.01, 0.05, 0.06, 0.04, 0.09, 0.07, 0.03, 0.11, 0.04, 
0.0800000000000001, 0.07, 0.09, 0.04, -0.01, -0.1, -0.1, -0.09, 
-0.09, -0.06, -0.08, -0.03, -0.01, -0.04, 0.02, 0.0800000000000001, 
0.04, 0.03, 0.02, 0.06, 0.05, 0.05, 0.06, 0.0499999999999999, 
0.08, -0.0199999999999999, -0.15, -0.08, -0.1, -0.1, -0.11, -0.12, 
-0.06, -0.05, -0.06, -0.05, -0.07, -0.04, 0.04, 0.09, 0.07, 0.04, 
0.05, 0.05, 0.04, 0.03, -0.07, -0.08, -0.01, -0.07, -0.07, 0.03, 
0.06, 0.11, 0.16, 0.12, 0.1, 0.05, 0.07, 0.07, 0.04, 0.01, 0.01, 
0.02, -0.05, -0.08, -0.07, -0.08, -0.11, -0.12, 0.0499999999999999, 
0.0399999999999999, 0, 0.01, -0.01, -0.13, -0.0800000000000001, 
-0.25, -0.21, -0.12, -0.08, -0.03, -0.05, -0.0700000000000001, 
-0.02, -0.01, -0.02, 0.0099999999999999, 0.0599999999999999, 
0.07, 0, 0.06, 0.07, 0.07, -0.09, -0.1, -0.16, 0.05, -0.09, -0.03, 
0.2, -0.02, 0.02, 0.04, 0.04, 0.04, 0.02, 0.04, 0.05, 0.04, 0, 
0, -0.03, -0.03, -0.02, -0.05, -0.05, -0.06, -0.05, 0.01, 0.03, 
0.03, 0.05, 0.03, 0.07, 0.17, 0.22, 0.09, 0.06, 0.06, 0.06, 0.04, 
0.07, 0.02, 0.02, -0.05, -0.11, -0.04, 0, -0.05, -0.07, -0.0700000000000001, 
0.01, 0.03, 0.03, 0.05, 0.06, 0.05, -0.21, -0.23, -0.2, -0.14, 
-0.07, -0.07, -0.02, -0.06, -0.05, -0.05, 0.01, 0.03, 0.08, 0.04, 
0.01, 0.06, 0.06, 0.08, -0.01, -0.09, -0.01, -0.02, 0.02, 0.08, 
-0.03, -0.02, 0.02, 0, 0, 0.02, 0.03, 0.04, 0.02, 0.02, 0.05, 
0.04, 0.01, -0.01, 0, -0.02, -0.03, -0.03, 0.06, 0.01, 0.01, 
0.03, -0.01, -0.02, -0.2, -0.11, -0.11, -0.07, -0.03, -0.06, 
-0.01, 0.01, -0.02, -0.02, -0.02, 0.07, 0.07, 0.04, -0.01, 0.04, 
0, 0.03, -0.05, -0.05, -0.00999999999999995, 0.07, 0.08, 0.09, 
0.08, 0.22, 0.16, 0.07, 0.08, 0.07, 0.05, 0.05, 0.08, 0.05, 0, 
-0.11, -0.1, -0.02, 0, -0.06, -0.04, -0.05, -0.04, -0.02, -0.01, 
-0.03, -0.05, -0.1, 0.2, 0.02, -0.07, 0.00999999999999995, 0.01, 
0.07, 0.02, 0.06, 0.03, 0.05, 0, 0, 0, -0.01, -0.02, -0.03, 0.04, 
-0.04, 0.05, 0.09, 0.05, 0.12, 0.04, 0.06, -0.23, -0.21, -0.2, 
-0.15, -0.18, -0.1, -0.07, -0.06, -0.06, -0.04, 0.04, 0.12, 0.14, 
0.09, 0.08, 0.1, 0.13, 0.13, -0.02, -0.1, -0.14, -0.08, -0.11, 
-0.01, 0.13, 0.16, 0.14, 0.12, 0.09, 0.05, 0, 0.01, -0.05, -0.06, 
-0.08, -0.16, -0.18, -0.15, -0.07, -0.13, -0.09, -0.02, -0.09, 
-0.08, -0.07, -0.16, -0.1, 0, 0.18, 0.15, 0.15, 0.1, 0.03, 0.01, 
0.01, -0.0199999999999999, -0.0399999999999999, -0.01, -0.0700000000000001, 
-0.11, -0.12, -0.0900000000000001, -0.0900000000000001, -0.08, 
-0.0700000000000001, -0.0700000000000001, 0.08, 0.11, 0.06, 0.01, 
-0.02, 0.05, -0.03, -0.24, -0.2, -0.14, -0.03, -0.07, -0.08, 
-0.04, -0.06, -0.02, -0.01, 0.07, 0.13, 0.06, 0.07, 0.09, 0.09, 
0.06)), row.names = c(NA, -600L), class = "data.frame")

I currently have a tilemap without dendrogram features. I want to have a dendrogram feature displayed with the current tilemap as shown below:

ggplot(hour_flow, aes(x= start_hour, y= county, fill = in_minus_out)) + 
  geom_tile() +
  scale_fill_gradient(low = "white",
                      high = "orange") +
  theme_bw()

need dendrogram on top

1

There are 1 answers

7
user12728748 On

You need to spread out your data into matrix format, transpose and get rid of start_hour. This would work:

library(d3heatmap)
library(dplyr)
library(tidyr)

hour_flow %>% 
  pivot_wider(names_from=county, values_from=in_minus_out) %>% 
  data.frame(., row.names =.$start_hour) %>% t() %>% .[-1,] %>%  
  d3heatmap(colors = "RdYlBu",
          dendrogram = "row",
          k_rows = 3)