【发布时间】:2019-02-13 14:47:58
【问题描述】:
前几天我遇到了这个问题,并试图为自己重新创建它。 ggplot, facet, piechart: placing text in the middle of pie chart slices 。我的数据格式非常相似,但遗憾的是接受的答案没有帮助,因此我重新发布。
我本质上想用我自己的数据创建公认的答案,但我遇到的问题是coord_polar does not support free scale。使用第一个答案:
我尝试使用答案的第二个版本和 ddplyr 版本,但我也没有得到我想要的输出。使用第二个答案:
显然,这些都没有预期的效果。我更愿意像大小饼图一样创建一个,但仅显示四个作为示例,如下所示:。 这是我在 excel 中做的,但有一个图例,没有背景网格。
代码
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
library(tidyverse)
p1<-ggplot(data = piec, aes(x = "", y = value, fill = type)) +
geom_bar(stat = "identity") +
geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")
#facet_grid(title ~ ., scales = "free")
p1
piec <- piec %>% group_by(title) %>% mutate(pos=cumsum(value)-0.5*value)
p2<-ggplot(data = piec) +
geom_bar(aes(x = "", y = value, fill = type), stat = "identity") +
geom_text(aes(x = "", y = pos, label = value)) +
coord_polar(theta = "y")
#facet_grid(Channel ~ ., scales = "free")
p2
【问题讨论】: