tableau-重绘沃罗诺伊树图
Xwyturbo / 2022-11-25
使用voronoiTreemap画图很快,但是有很多参数难以调整。
library(voronoiTreemap)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.5
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(writexl)
library(readxl)
library(ggpubr)
df <- read_xlsx("D:/wenyu/Rrojects/vTreemap/df.xlsx")
df %>%
knitr::kable()
h1 | h2 | h3 | color | weight | codes | province | prod |
---|---|---|---|---|---|---|---|
Total | East | Anhui | #CED7BA | 4.35 | AH | AH | 95.20 |
Total | North | Beijing | #009593 | 5.05 | BJ | BJ | 110.30 |
Total | South | Chongqing | #E4D1B3 | 3.80 | CQ | CQ | 82.90 |
Total | South | Fujian | #E4D1B3 | 4.88 | FJ | FJ | 106.60 |
Total | South | Guangdong | #E4D1B3 | 4.66 | GD | GD | 101.70 |
Total | North | Gansu | #009593 | 1.72 | GS | GS | 37.60 |
Total | South | Guangxi | #E4D1B3 | 4.04 | GX | GX | 88.10 |
Total | South | Guizhou | #E4D1B3 | 3.11 | GZ | GZ | 67.90 |
Total | East | Henan | #CED7BA | 3.55 | HA | HA | 77.60 |
Total | East | Hubei | #CED7BA | 4.19 | HB | HB | 91.40 |
Total | North | Hebei | #009593 | 3.25 | HE | HE | 71.00 |
Total | South | Hainan | #E4D1B3 | 3.56 | HI | HI | 77.80 |
Total | Nodata | Hongkonng | #D35C79 | 0.01 | HK | HK | 0.01 |
Total | North | Heilongjiang | #009593 | 0.52 | HL | HL | 11.40 |
Total | South | Hunan | #E4D1B3 | 3.75 | HN | HN | 81.90 |
Total | North | Jilin | #009593 | 0.65 | JL | JL | 14.10 |
Total | East | Jiangsu | #CED7BA | 4.47 | JS | JS | 97.50 |
Total | South | Jiangxi | #E4D1B3 | 4.03 | JX | JX | 88.00 |
Total | North | Liaoning | #009593 | 1.44 | LN | LN | 31.50 |
Total | Nodata | Macao | #D35C79 | 0.01 | MO | MO | 0.01 |
Total | North | InnerMongoriaIM | #009593 | 1.36 | NM | NM | 29.60 |
Total | North | Ningxia | #009593 | 2.49 | NX | NX | 54.30 |
Total | North | Qinghai | #009593 | 1.95 | QH | QH | 42.60 |
Total | South | Sichuan | #E4D1B3 | 3.81 | SC | SC | 83.10 |
Total | East | Shandong | #CED7BA | 4.13 | SD | SD | 90.10 |
Total | East | Shanghai | #CED7BA | 4.20 | SH | SH | 91.60 |
Total | North | Shanxi | #009593 | 2.60 | SN | SX | 29.30 |
Total | North | Shaanxi | #009593 | 1.34 | SX | SX | 56.80 |
Total | North | Tianjing | #009593 | 4.37 | TJ | TJ | 95.50 |
Total | Nodata | Taiwan | #D35C79 | 0.01 | TW | TW | 0.01 |
Total | North | Xingjiang | #009593 | 3.76 | XJ | XJ | 82.10 |
Total | South | Xizang | #E4D1B3 | 0.55 | XZ | XZ | 11.90 |
Total | South | Yunnan | #E4D1B3 | 3.87 | YN | YN | 84.40 |
Total | East | Zhejiang | #CED7BA | 4.54 | ZJ | ZJ | 99.10 |
solar_json <- vt_export_json(vt_input_from_df(df,
scaleToPerc = FALSE,
hierachyVar0 = "h1",
hierachyVar1 = "h2",
hierachyVar2 = "h3",
colorVar = "color",
weightVar="prod",
labelVar = "codes"))
p <- vt_d3(solar_json,label = T,
color_border = "#000000",width = 1000,
legend = TRUE, legend_title = "Urban Economic Region",
seed = 3,
size_border = "1px")
p
使用tableau绘制沃罗诺伊树图需要先转换数据,登录下面这个网站,然后转换数据。
网址:https://observablehq.com/@ladataviz/wip-voronoi-data-generator
注意,上传的数据最好是csv格式,调整对应字段,然后下载数据!
split by相当于观测值的编码,对应示例数据的province字段. size by相当于具体的变量值,对应示例数据的prod字段. group by字段相当于分组字段,对应示例数据的h2字段.。
打开tableau,添加数据,转到工作表。
首先将path字段拖拽至维度窗口,接着在标记窗口,选择多边形,从维度窗口,依次将group、split、path字段拖拽到标记窗口下,前两个字段调整为颜色,path字段调整为路径。 将度量窗口的x、y,分布拖拽至列功能区、在行功能区,将度量修改为平均值,这个时候图就出来了。 下面需要增加标签,再将将度量窗口的x拖拽至列功能区,将度量修改为平均值,同时修改为双轴。 修改标记窗口下,平均值(x)(2)相关参数,选择文本,窗口下只保留split字段、value字段(value字段是从度量窗口拖拽过来的,还需要调整为平均值)。至此,图的基本雏形就有了,下面调整字体啥的,不做过多赘述,试试就可以了。