我想在R中绘制一个partition layout又名partition table。这种类型的图表从左到右水平绘制分层数据,节点为比例高度的空间填充瓦片,并且瓦片邻接为连接。 x轴表示层次结构级别,y轴表示使用的任何度量标准。
在Excel中拼凑出有缺陷的临时示例:
它也可以与:
进行比较这种类型的图表在D3.js中可用。 我怎么能在R?是否可以使用ggplot2?
编辑:这是我的实际数据。我需要emp_est(不是计数)作为y轴。 n级NAICS代码的emp_est等于子N-1级NAICS代码的emp_est之和。
structure(list(naics_level = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3), naics = c("62----", "44----", "54----", "72----", "61----",
"52----", "31----", "56----", "42----", "51----", "81----", "23----",
"55----", "48----", "71----", "53----", "22----", "99----", "11----",
"21----", "541///", "722///", "611///", "622///", "561///", "621///",
"624///", "445///", "551///", "623///", "238///", "423///", "524///",
"522///", "523///", "511///", "813///", "424///", "334///", "448///",
"812///", "531///", "713///", "452///", "441///", "518///", "721///",
"236///", "446///", "444///", "311///", "485///", "811///", "517///",
"332///", "454///", "325///", "453///", "333///", "519///", "484///",
"339///", "237///", "451///", "711///", "221///", "492///", "425///",
"447///", "481///", "442///", "493///", "532///", "336///", "562///",
"488///", "443///", "323///", "326///", "335///", "712///", "515///",
"512///", "327///", "337///", "313///", "322///", "312///", "533///",
"521///", "315///", "331///", "321///", "314///", "316///", "487///",
"525///", "324///", "212///", "115///", "483///", "486///", "114///",
"113///", "213///", "211///", "7225//", "6113//", "6221//", "5511//",
"4451//", "6241//", "5613//", "5415//", "5221//", "5417//", "6211//",
"5416//", "5413//", "5241//", "6231//", "2382//", "5617//", "5239//",
"5112//", "4481//", "7139//", "6111//", "6216//", "6214//", "5411//",
"5182//", "7211//", "5412//", "6244//", "3345//", "6232//", "4234//",
"5242//", "4461//", "8131//", "7223//", "8121//", "4411//", "4441//",
"4521//", "6212//", "5616//", "5313//", "6233//", "6223//", "5419//",
"4244//", "8111//", "4236//", "6116//", "5171//", "5111//", "2383//",
"5191//", "6213//", "5231//", "5614//", "2362//", "2361//", "5311//",
"5418//", "4541//", "4529//", "3344//", "3254//", "4251//", "2381//",
"8129//", "4471//", "4511//", "4921//", "5611//", "6222//", "2389//",
"4931//", "4811//", "2211//", "4431//", "4242//", "4238//", "3391//",
"3231//", "3118//", "8139//", "4841//", "3327//", "8123//", "4854//",
"7224//", "8134//", "5222//", "6215//", "6219//", "4413//", "8133//",
"2371//", "4243//", "4453//", "6243//", "3261//", "4422//", "4532//",
"4543//", "5615//", "4842//", "5619//", "3364//", "6242//", "8132//",
"3399//", "5312//", "4539//", "3332//", "7113//", "3359//", "4231//",
"4851//", "4239//", "7121//", "2373//", "4482//", "4237//", "4452//",
"5179//", "4859//", "3119//", "4853//", "5121//", "5621//", "3339//",
"6112//", "4249//", "4241//", "6117//", "5629//", "4233//", "3329//",
"3323//", "5324//", "5151//", "4248//", "4483//", "5223//", "4885//",
"5321//", "4421//", "4512//", "3328//", "5414//", "7111//", "4881//",
"4232//", "3113//", "4533//", "7112//", "5612//", "6115//", "3333//",
"6239//", "5172//", "3115//", "3116//", "3222//", "3121//", "5322//",
"8122//", "8112//", "3363//", "6114//", "4247//", "2212//", "4884//",
"3342//", "8113//", "4235//", "4246//", "5331//", "3341//", "4442//",
"3372//", "3133//", "3262//", "5211//", "3114//", "3251//", "3353//",
"8114//", "3117//", "4531//", "3252//", "3132//", "4412//", "3152//",
"3334//", "3371//", "3219//", "7131//", "5152//", "3273//", "3255//",
"3335//", "3321//", "3324//", "3259//", "3322//", "3272//", "2379//",
"4922//", "5622//", "7115//", "3315//", "3149//", "4855//", "3279//",
"3162//", "4872//", "2372//", "4812//", "3351//", "4852//", "2213//",
"3336//", "3366//", "3141//", "7212//", "3362//", "5259//", "4542//",
"3241//", "4883//", "3256//", "2123//", "3221//", "4831//", "7114//",
"3369//", "3379//", "3313//", "1152//", "3346//", "5323//", "3343//",
"7132//", "3274//", "3314//", "4862//", "4889//", "7213//", "3271//",
"5122//", "3169//", "3352//", "4871//", "3331//", "3161//", "3326//",
"3312//", "3159//", "4245//", "5174//", "1133//", "3361//", "3253//",
"3325//", "3112//", "1141//", "1153//", "2131//", "1142//", "3211//",
"3111//", "2111//", "5232//", "3365//", "1151//", "1132//", "3131//",
"3212//", "4832//", "1131//", "2122//", "3122//", "3311//", "4879//"
), emp_est = c(444491, 266068, 242031, 217912, 172492, 156366,
150651, 141859, 106269, 103451, 94438, 93862, 78951, 61390, 42552,
38517, 8786, 644, 475, 373, 242031, 193891, 172492, 145566, 134867,
132980, 92554, 79489, 78951, 73391, 63089, 59167, 56680, 53411,
44938, 42428, 41420, 38574, 35544, 35191, 34789, 29626, 29104,
27284, 25031, 24318, 24021, 20156, 19473, 19472, 18503, 18446,
18229, 17982, 16842, 14807, 13685, 12597, 11880, 11531, 11199,
10983, 10617, 10365, 9554, 8786, 8565, 8528, 8025, 7760, 7578,
7390, 7369, 7308, 6992, 6955, 6756, 6511, 6428, 5969, 3894, 3649,
3543, 2642, 2586, 2413, 2154, 1871, 1522, 1256.5, 1145, 1043,
1038, 988, 799, 645, 324, 319, 291, 282, 282, 148, 108, 85, 58,
24, 168952, 123448, 122772, 78951, 70361, 59968, 52588, 47253,
44865, 43270, 40371, 39138, 37926, 36667, 35557, 35527, 34236,
33878, 30390, 28710, 28005, 26446, 26006, 25954, 24481, 24318,
23538, 23091, 22838, 22786, 20565, 20463, 20013, 19473, 18890,
18796, 18617, 18271, 17994, 17856, 17654, 17590, 15429, 15258,
15038, 14706, 14407, 13729, 13324, 12736, 12354, 12038, 11949,
11531, 11385, 11049, 10961, 10105, 10051, 9894, 9756, 9507, 9428,
9329, 9296, 8528, 8059, 8033, 8025, 7895, 7846, 7786, 7756, 7554,
7390, 7258, 6823, 6756, 6702, 6689, 6661, 6511, 6443, 6412, 6351,
6332, 6317, 6272, 6143, 6056, 5918, 5827, 5783, 5689, 5663, 5526,
5381, 5379, 5242, 5138, 5047, 4979, 4977, 4905, 4848, 4742, 4569,
4506, 4399, 4322, 4303, 4287, 4274, 4237, 4212, 4152, 4057, 4045,
3894, 3855, 3810, 3802, 3749, 3611, 3599, 3551, 3493, 3418, 3289,
3182, 3138, 3092, 3063, 3020, 2994, 2859, 2819, 2795, 2739, 2722,
2701, 2671, 2628, 2617, 2607, 2531, 2470, 2467, 2410, 2322, 2316,
2302, 2273, 2220, 2066, 2059, 2054, 2029, 2011, 1939, 1901, 1876,
1870, 1868, 1851, 1822, 1795, 1698, 1650, 1627, 1578, 1567, 1547,
1540, 1531, 1523, 1522, 1515, 1478, 1330, 1322, 1290, 1256.5,
1251, 1174, 1171, 1165, 1112, 1111, 1109, 1084, 1071, 1067, 1035,
1021, 992, 944, 927, 925, 909, 908, 827, 823, 820, 809.5, 801,
719, 719, 709, 672, 661, 645, 630, 618, 596, 547, 517, 502, 482,
395, 385, 358, 355, 343, 342, 340, 324, 323, 319, 311, 304, 288,
286, 274, 257, 238, 235, 209, 206, 197, 172, 170, 155, 154, 148,
148, 144, 141, 133, 125, 111, 104, 96, 94, 92, 90, 80, 78, 78,
78, 77, 77, 73, 73, 68, 66, 66, 58, 41.5, 38, 28, 24, 14.5, 13,
12, 9.5, 9.5, 8, 8, 2.5, 2.5, 2.5, 2.5, 2.5)), .Names = c("naics_level",
"naics", "emp_est"), row.names = c(NA, -390L), class = c("tbl_df",
"tbl", "data.frame"))
答案 0 :(得分:1)
以下是ggplot2
中的一个简单示例:
d <- data.frame(level3 = c(rep('aaa', 4), 'aab', rep('aba', 2), 'abb', 'aca', 'acb'), stringsAsFactors = FALSE)
d$level2 <- substr(d$level3, 1, 2)
d$level1 <- substr(d$level3, 1, 1)
d$id <- 1:nrow(d)
d2 <- tidyr::gather(d, level, label, -id)
library(ggplot2)
ggplot(d2, aes(level, group = label, fill = level)) +
geom_bar(position = 'stack', col = 1, width = 1) +
geom_text(aes(label = label), position = position_stack(vjust = 0.5), stat = 'count')