R多级pi / donut图表

时间:2015-09-17 17:12:53

标签: r graph charts ggplot2

我试图在r中复制下面链接的图表但却无法复制。我知道这可能不是可视化信息的最佳方式,但这是我的任务。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

正如两条评论所说,对SO的期望是你在一个最小的例子中分享数据和一些代码。也就是说,这是一个粗略的起点,部分基于pie charts in R

titles <- c(38, 244, 34, 20, 25)
position <- c("Adm","GC","Lawyer","Lawyer2", "Other")
piedf <- cbind(titles, position)
dfpie <- data.frame(piedf)

ggplot(data=dfpie, aes(x=factor(1), y=titles, fill = factor(position))) + 
  geom_bar(width = .6, stat = "identity") +
  labs(x = "", y = "") +
  guides(fill = FALSE) +
  coord_polar(theta="y") +
  theme_bw()

enter image description here

正如他们在这里所说的那样,我将其留作OP的练习,将圆环图叠加在较大的饼图中。

答案 1 :(得分:0)

您可以使用ggsunburst包

重现该图
# install ggsunburst package
if (!require("ggplot2")) install.packages("ggplot2")
if (!require("rPython")) install.packages("rPython")
install.packages("http://genome.crg.es/~didac/ggsunburst/ggsunburst_0.0.9.tar.gz", repos=NULL, type="source")
library(ggsunburst)

# trying to replicate the data in your plot
df <- read.table(header = T, text = "
parent node size
Non-Domestic A 30
Non-Domestic B 10
Non-Domestic C 2
Domestic A 20
Domestic B 25
Domestic C 1
Transport A 10
Transport B 15
")

write.table(df, 'df.csv', row.names = F, sep = ",")

sb <- sunburst_data('df.csv', type="node_parent", sep = ",")
sunburst(sb, leaf_labels = F, node_labels = T, node_labels.min = 30, rects.fill.aes = "name") +
  scale_fill_discrete(guide = F)

enter image description here