ggplot甜甜圈图表

时间:2012-11-28 22:29:40

标签: r graphics ggplot2 tikz

嗨,我真的用Google搜索了很多,没有任何快乐。如果网站存在,很乐意获得对网站的引用。我很难理解Hadley documentation on polar coordinates,我知道馅饼/甜甜圈图表本质上是邪恶的。

那就是说,我要做的是

  1. 创建一个圆环/圆环图(所以一个空的中间的馅饼),如此处显示的tikz ring chart
  2. 在顶部添加第二个图层圆圈(使用alpha=0.5左右),显示第二个(可比较的)变量。
  3. 为什么呢?我正在寻找财务信息。第一个环是成本(分解),第二个环是总收入。然后,我们的想法是在每个审核期间添加+ facet=period,以显示收入和费用以及两者的增长趋势。

    任何想法都会非常感激

    注意:如果使用

    尝试使用MWE,则完全随意
    donut_data=iris[,2:4]
    revenue_data=iris[,1]
    facet=iris$Species
    

    这与我正在尝试做的类似..谢谢

2 个答案:

答案 0 :(得分:35)

我对您的问题没有完整的答案,但我可以提供一些代码,可以帮助您开始使用ggplot2制作戒指图。

library(ggplot2)

# Create test data.
dat = data.frame(count=c(10, 60, 30), category=c("A", "B", "C"))

# Add addition columns, needed for drawing with geom_rect.
dat$fraction = dat$count / sum(dat$count)
dat = dat[order(dat$fraction), ]
dat$ymax = cumsum(dat$fraction)
dat$ymin = c(0, head(dat$ymax, n=-1))

p1 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
     geom_rect() +
     coord_polar(theta="y") +
     xlim(c(0, 4)) +
     labs(title="Basic ring plot")

p2 = ggplot(dat, aes(fill=category, ymax=ymax, ymin=ymin, xmax=4, xmin=3)) +
     geom_rect(colour="grey30") +
     coord_polar(theta="y") +
     xlim(c(0, 4)) +
     theme_bw() +
     theme(panel.grid=element_blank()) +
     theme(axis.text=element_blank()) +
     theme(axis.ticks=element_blank()) +
     labs(title="Customized ring plot")


library(gridExtra)
png("ring_plots_1.png", height=4, width=8, units="in", res=120)
grid.arrange(p1, p2, nrow=1)
dev.off()

enter image description here

<强>思想:

  1. 如果您发布一些结构良好的样本数据,您可能会获得更多有用的答案。您已经提到使用iris数据集中的一些列(一个好的开始),但我无法看到如何使用该数据来制作环形图。例如,您链接的环形图显示了几个类别的比例,但iris[, 2:4]iris[, 1]都不是分类。
  2. 您想要“在顶部添加第二层圆圈”:您的意思是将第二个圆圈直接叠加在第一个圆圈的顶部吗?或者你想要第二个戒指在第一个戒指的内部或外部?您可以添加第二个内部环,例如geom_rect(data=dat2, xmax=3, xmin=2, aes(ymax=ymax, ymin=ymin))
  3. 如果您的data.frame有一个名为period的列,则可以使用facet_wrap(~ period)进行构面。
  4. 要最轻松地使用ggplot2,您需要“长格式”数据;来自melt()包的reshape2可能对转换数据很有用。
  5. 制作一些条形图进行比较,即使您决定不使用它们。例如,尝试:     ggplot(dat, aes(x=category, y=count, fill=category)) + geom_bar(stat="identity")

答案 1 :(得分:2)

尝试使用bdemarest's answer中的相同方法解决问题2。也使用他的代码作为脚手架。我添加了一些测试以使其更完整,但随意删除它们。

library(broom)
library(tidyverse)
# Create test data.
dat = data.frame(count=c(10,60,20,50),
                 ring=c("A", "A","B","B"),
                 category=c("C","D","C","D"))

# compute pvalue
cs.pvalue <- dat %>% spread(value = count,key=category) %>%
  ungroup() %>% select(-ring) %>% 
  chisq.test() %>% tidy()
cs.pvalue <- dat %>% spread(value = count,key=category) %>% 
  select(-ring) %>%
  fisher.test() %>% tidy() %>% full_join(cs.pvalue)

# compute fractions
#dat = dat[order(dat$count), ]
dat %<>% group_by(ring) %>% mutate(fraction = count / sum(count),
                                      ymax = cumsum(fraction),
                                      ymin = c(0,ymax[1:length(ymax)-1]))


# Add x limits
baseNum <- 4
#numCat <- length(unique(dat$ring))
dat$xmax <- as.numeric(dat$ring) + baseNum
dat$xmin = dat$xmax -1


# plot
p2 = ggplot(dat, aes(fill=category,
                     alpha = ring,
                     ymax=ymax, 
                     ymin=ymin, 
                     xmax=xmax, 
                     xmin=xmin)) +
  geom_rect(colour="grey30") +
  coord_polar(theta="y") +
  geom_text(inherit.aes = F,
            x=c(-1,1),
            y=0,
            data = cs.pvalue,aes(label = paste(method,
                                               "\n",
                                               format(p.value,
                                                      scientific = T,
                                                      digits = 2))))+
  xlim(c(0, 6)) +
  theme_bw() +
  theme(panel.grid=element_blank()) +
  theme(axis.text=element_blank()) +
  theme(axis.ticks=element_blank(),
        panel.border = element_blank()) +
  labs(title="Customized ring plot") + 
  scale_fill_brewer(palette = "Set1") +
  scale_alpha_discrete(range = c(0.5,0.9))

p2

结果:

Imgur