R 将百分比标签放在图例旁边,而不是放在切片中
首先,让我说饼图不是我的选择,它是主管要求的报告的一个方面。 我使用以下代码创建了一系列饼图:R 将百分比标签放在图例旁边,而不是放在切片中,r,ggplot2,legend,R,Ggplot2,Legend,首先,让我说饼图不是我的选择,它是主管要求的报告的一个方面。 我使用以下代码创建了一系列饼图: perpie <- Full_Mod_good %>% split(.$ServiceSite) %>% imap(function(data, site) { data %>% group_by(ServiceSite, InitialType) %>% summarise(count = n()) %>% mutate(s
perpie <- Full_Mod_good %>%
split(.$ServiceSite) %>%
imap(function(data, site) {
data %>%
group_by(ServiceSite, InitialType) %>%
summarise(count = n()) %>%
mutate(share = round(count / sum(count), digits = 2)) %>%
ggplot(aes(x = "", y = share, fill = InitialType)) +
geom_col(width = 1) +
geom_text(aes(label = scales::percent(share)), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y", start = 0, direction = 1)+
ggtitle(site)+
ylab("Percentage of Healed Wounds")+
xlab("")+
theme_minimal()+
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())+
theme(plot.title = element_text(hjust = 0.5))
})
perpie
我对被迫进入饼图表示哀悼。您只需添加一个带有包含百分比的标签的列,然后将其用作填充变量。下面是一个数据子集的示例(我选择了
ServiceSite.x==5
,因为这需要处理相当多的观察结果
库(tidyverse)
df_标签%
筛选器(ServiceSite.x==5)%>%
计数(初始类型)%%>%
变异(共享=四舍五入(n/和(n),数字=2))%>%
变异(标签=比例::百分比(共享),标签为_type=sprintf(“%s(%s)”,初始类型,标签))
df_标签
#>#tibble:5 x 5
#>InitialType n共享标签标记为\u类型
#>
#>1例挫伤1例0.17%挫伤(17%)
#>2其他1 0.17%其他(17%)
#>3压力1 0.17%压力(17%)
#>4皮肤2 0.33%皮肤(33%)
#>5外科(不愈合)1 0.17 17%外科(不愈合)(17%)
ggplot(df_标签,aes(x=1,y=n,填充=标签_类型))+
几何坐标(宽度=1)+
geom_文本(aes(标签=标签),位置=位置_堆栈(vjust=0.5))+
极坐标(theta=“y”)+
主题_void()
由(v0.2.0)于2018年7月25日创建.饼图上有两个想法:@DanY正如我所说,这不是我想说的。这更像是一个风格化的回答,我同意把百分比放在标签旁边是个好主意,这会使饼图毫无价值。我建议创建一个表来展示你的数据,甚至忘记饼图的存在。不要相信我的话,阅读Ed Tufte的作品。同样,@Dave2e,这是我要求的,不是我喜欢的方法。表达得很好,“我对被迫进入饼图表示哀悼”。哈哈哈。
ServiceSite.x InitialType
2 Dermatitis
2 Diabetic
2 Pressure Injury
2 Pressure Injury
3 Pressure Injury
3 Other
3 Laceration
3 Other
4 Pressure Injury
4 MASD
4 Blister (Non-Pressure)
4 Skin Tear
4 Pressure Injury
5 Skin Tear
5 Other
5 Contusion
5 Skin Tear
5 Surgical(Non-Healing)
5 Pressure Injury
6 Pressure Injury
1 Pressure Injury
6 Pressure Injury
6 MASD
1 Surgical(Non-Healing)
1 Pressure Injury
1 Skin Tear
1 Contusion