Put percent labels next to legend instead of in the slice

二次信任 提交于 2019-12-12 18:10:43

问题


Let me preface this by saying Pie Charts were not my choice, it is a facet of a report that was requested by a supervisor. I have a series of pie charts created from the following code:

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 

with about 50 plots resembling this:

I would like to add the percentages to the labels in legend but am unsure of how to do this. I have tried spreading the labels out around the perimeter of the graph but, due to the large number of initial types, it sometimes becomes challenging to distinguish between similar shades of a color. Adding the percentages to the legend would eliminate this.

Sample Data

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

回答1:


My condolences on being forced into a pie chart. You can just add a column with labels that include percentages, and then use that as your fill variable. Here's an example with one of the subsets of the data (I chose ServiceSite.x == 5 because this had a fair number of observations to work with.

library(tidyverse)

df_label <- df %>%
  filter(ServiceSite.x == 5) %>%
  count(InitialType) %>%
  mutate(share = round(n / sum(n), digits = 2)) %>%
  mutate(label = scales::percent(share), labeled_type = sprintf("%s (%s)", InitialType, label))

df_label
#> # A tibble: 5 x 5
#>   InitialType               n share label labeled_type               
#>   <chr>                 <int> <dbl> <chr> <chr>                      
#> 1 Contusion                 1  0.17 17%   Contusion (17%)            
#> 2 Other                     1  0.17 17%   Other (17%)                
#> 3 Pressure                  1  0.17 17%   Pressure (17%)             
#> 4 Skin                      2  0.33 33%   Skin (33%)                 
#> 5 Surgical(Non-Healing)     1  0.17 17%   Surgical(Non-Healing) (17%)

ggplot(df_label, aes(x = 1, y = n, fill = labeled_type)) +
  geom_col(width = 1) +
  geom_text(aes(label = label), position = position_stack(vjust = 0.5)) +
  coord_polar(theta = "y") +
  theme_void()

Created on 2018-07-25 by the reprex package (v0.2.0).



来源:https://stackoverflow.com/questions/51524641/put-percent-labels-next-to-legend-instead-of-in-the-slice

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!