|
| 1 | +--- |
| 2 | +name: Treemap Charts |
| 3 | +permalink: r/treemaps / |
| 4 | +description: How to make treemap charts in R with Plotly. |
| 5 | +layout: base |
| 6 | +thumbnail: thumbnail/treemap.png |
| 7 | +language: r |
| 8 | +display_as: basic |
| 9 | +order: 16 |
| 10 | +output: |
| 11 | + html_document: |
| 12 | + keep_md: true |
| 13 | +--- |
| 14 | + |
| 15 | +```{r, echo = FALSE, message=FALSE} |
| 16 | +knitr::opts_chunk$set(message = FALSE, warning = FALSE) |
| 17 | +``` |
| 18 | +[Treemap charts](https://en.wikipedia.org/wiki/Treemapping) visualize hierarchical data using nested rectangles. Same as [Sunburst](https://plot.ly/r/sunburst-charts/) the hierarchy is defined by [labels](https://plot.ly/r/reference/#treemap-labels), and [parents](https://plot.ly/r/reference/#treemap-parents) attributes. Click on one sector to zoom in/out, which also displays a pathbar in the upper-left corner of your treemap. To zoom out you can use the path bar as well. |
| 19 | + |
| 20 | +### Basic Treemap |
| 21 | + |
| 22 | +```{r} |
| 23 | +library(plotly) |
| 24 | +
|
| 25 | +fig <- plot_ly( |
| 26 | + type="treemap", |
| 27 | + labels=c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"), |
| 28 | + parents=c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve") |
| 29 | +) |
| 30 | +fig |
| 31 | +``` |
| 32 | +### Set Different Attributes in Treemap |
| 33 | + |
| 34 | +This example uses the following attributes: |
| 35 | + |
| 36 | +1. [values](https://plot.ly/r/reference/#treemap-values): sets the values associated with each of the sectors. |
| 37 | +2. [textinfo](https://plot.ly/r/reference/#treemap-textinfo): determines which trace information appear on the graph that can be 'text', 'value', 'current path', 'percent root', 'percent entry', and 'percent parent', or any combination of them. |
| 38 | +3. [pathbar](https://plot.ly/r/reference/#treemap-pathbar): a main extra feature of treemap to display the current path of the visible portion of the hierarchical map. It may also be useful for zooming out of the graph. |
| 39 | +4. [branchvalues](https://plot.ly/r/reference/#treemap-branchvalues): determines how the items in `values` are summed. When set to "total", items in `values` are taken to be value of all its descendants. In the example below Eva = 65, which is equal to 14 + 12 + 10 + 2 + 6 + 6 + 1 + 4. |
| 40 | + |
| 41 | +When set to "remainder", items in `values` corresponding to the root and the branches sectors are taken to be the extra part not part of the sum of the values at their leaves. |
| 42 | + |
| 43 | +```{r} |
| 44 | +library(plotly) |
| 45 | +
|
| 46 | +labels = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura") |
| 47 | +parents = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve") |
| 48 | +
|
| 49 | +fig <- plot_ly( |
| 50 | + type='treemap', |
| 51 | + labels=labels, |
| 52 | + parents=parents, |
| 53 | + values= c(10, 14, 12, 10, 2, 6, 6, 1, 4), |
| 54 | + textinfo="label+value+percent parent+percent entry+percent root", |
| 55 | + domain=list(column=0)) |
| 56 | +
|
| 57 | +fig <- fig %>% add_trace( |
| 58 | + type='treemap', |
| 59 | + branchvalues="total", |
| 60 | + labels=labels, |
| 61 | + parents=parents, |
| 62 | + values=c(65, 14, 12, 10, 2, 6, 6, 1, 4), |
| 63 | + textinfo="label+value+percent parent+percent entry", |
| 64 | + outsidetextfont=list(size=20, color= "darkblue"), |
| 65 | + marker=list(line= list(width=2)), |
| 66 | + pathbar=list(visible= FALSE), |
| 67 | + domain=list(column=1)) |
| 68 | +
|
| 69 | +fig <- fig %>% layout( |
| 70 | + grid=list(columns=2, rows=1), |
| 71 | + margin=list(l=0, r=0, b=0, t=0)) |
| 72 | +
|
| 73 | +fig |
| 74 | +``` |
| 75 | + |
| 76 | +### Set Color of Treemap Sectors |
| 77 | + |
| 78 | +There are three different ways to change the color of the sectors in Treemap: |
| 79 | + |
| 80 | +1. [marker.colors](https://plot.ly/r/reference/#treemap-marker-colors), |
| 81 | +2. [colorway](https://plot.ly/r/reference/#treemap-colorway), |
| 82 | +3. [colorscale](https://plot.ly/r/reference/#treemap-colorscale). |
| 83 | + |
| 84 | +The following examples show how to use each of them. |
| 85 | + |
| 86 | +```{r} |
| 87 | +library(plotly) |
| 88 | +
|
| 89 | +labels = c("A1", "A2", "A3", "A4", "A5", "B1", "B2") |
| 90 | +parents = c("", "A1", "A2", "A3", "A4", "", "B1") |
| 91 | +
|
| 92 | +fig <- plot_ly( |
| 93 | + type="treemap", |
| 94 | + labels=labels, |
| 95 | + parents=parents, |
| 96 | + marker=list(colors=c("pink", "royalblue", "lightgray", "purple", "cyan", "lightgray", "lightblue"))) |
| 97 | +fig |
| 98 | +``` |
| 99 | +This example uses `treemapcolorway` attribute, which should be set in layout. |
| 100 | + |
| 101 | +```{r} |
| 102 | +library(plotly) |
| 103 | +
|
| 104 | +labels = c("A1", "A2", "A3", "A4", "A5", "B1", "B2") |
| 105 | +parents = c("", "A1", "A2", "A3", "A4", "", "B1") |
| 106 | +
|
| 107 | +fig <- plot_ly( |
| 108 | + type="treemap", |
| 109 | + labels=labels, |
| 110 | + parents=parents) |
| 111 | +
|
| 112 | +fig <- fig %>% layout(treemapcolorway=c("pink","lightgray")) |
| 113 | +fig |
| 114 | +``` |
| 115 | + |
| 116 | +```{r} |
| 117 | +library(plotly) |
| 118 | +
|
| 119 | +labels = c("A1", "A2", "A3", "A4", "A5", "B1", "B2") |
| 120 | +parents = c("", "A1", "A2", "A3", "A4", "", "B1") |
| 121 | +values = c("11", "12", "13", "14", "15", "20", "30") |
| 122 | +
|
| 123 | +fig <- plot_ly( |
| 124 | + type="treemap", |
| 125 | + labels=labels, |
| 126 | + parents=parents, |
| 127 | + values=values, |
| 128 | + marker=list(colorscale='Reds')) |
| 129 | +
|
| 130 | +fig |
| 131 | +``` |
| 132 | + |
| 133 | +### Nested Layers in Treemap |
| 134 | + |
| 135 | +The following example uses hierarchical data that includes layers and grouping. Treemap and [Sunburst](https://plot.ly/r/sunburst-charts/) charts reveal insights into the data, and the format of your hierarchical data. [maxdepth](https://plot.ly/r/reference/#treemap-maxdepth) attribute sets the number of rendered sectors from the given level. |
| 136 | + |
| 137 | +```{r} |
| 138 | +library(plotly) |
| 139 | +
|
| 140 | +df1 = read.csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv') |
| 141 | +df2 = read.csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/coffee-flavors.csv') |
| 142 | +
|
| 143 | +fig <- plot_ly( |
| 144 | + type='treemap', |
| 145 | + ids=df1$ids, |
| 146 | + labels=df1$labels, |
| 147 | + parents=df1$parents, |
| 148 | + domain=list(column=0)) |
| 149 | +
|
| 150 | +fig <- fig %>% add_trace( |
| 151 | + type='treemap', |
| 152 | + ids=df2$ids, |
| 153 | + labels=df2$labels, |
| 154 | + parents=df2$parents, |
| 155 | + maxdepth=3, |
| 156 | + domain=list(column=1)) |
| 157 | +fig <- fig %>% layout(grid=list(columns=2, rows=1)) |
| 158 | +fig |
| 159 | +
|
| 160 | +
|
| 161 | +``` |
| 162 | + |
| 163 | +### Controlling text fontsize with uniformtext |
| 164 | + |
| 165 | +If you want all the text labels to have the same size, you can use the `uniformtext` layout parameter. The `minsize` attribute sets the font size, and the `mode` attribute sets what happens for labels which cannot fit with the desired fontsize: either `hide` them or `show` them with overflow. |
| 166 | + |
| 167 | +```{r} |
| 168 | +library(plotly) |
| 169 | +df = read.csv('https://raw.githubusercontent.com/plotly/datasets/718417069ead87650b90472464c7565dc8c2cb1c/sunburst-coffee-flavors-complete.csv') |
| 170 | +fig <- plot_ly( |
| 171 | + type='treemap', |
| 172 | + ids=df$ids, |
| 173 | + labels=df$labels, |
| 174 | + parents=df$parents) |
| 175 | +
|
| 176 | +fig <- fig %>% layout(uniformtext=list(minsize=10, mode='hide')) |
| 177 | +fig |
| 178 | +``` |
| 179 | + |
| 180 | +#Reference |
| 181 | + |
| 182 | +See [https://plot.ly/r/reference/#treemap](https://plot.ly/r/reference/#treemap) for more information and chart attribute options! |
0 commit comments