将计数添加到图例标签

Add counts to legend labels

我正在尝试将 counts/percentages 添加到饼图的图例标签中。饼图很糟糕,我知道,但这不是本文 post 的重点。我想将 "Count" 的值粘贴到图例上的 "Wound.Type" 标签,但不知道如何访问以下代码每次迭代的计数。目标将是 "Laceration 5" 或任何数量的东西。我试过“.~Count”和“.~Wound.Type”、“.$Count”和“.$Wound.Type”,但我不明白如何访问我想要的特定值.

p1 <- DF %>% 
  split(.$ServiceSite) %>%
  imap(function(data, site) {
    data %>%
  group_by(ServiceSite, Wound.Type) %>%
  summarise(Count = n()) %>%
   mutate(share = round(Count / sum(Count), digits = 2)) %>%
    ggplot(aes(x = "", y = Count, fill = Wound.Type)) +
    geom_col(width = 1) +
    scale_fill_discrete(labels = paste(.$Wound.Type, .$Count))+
    facet_grid(facets = .~ServiceSite, labeller = label_value)+
    geom_text(aes(label = Count, y = ), position = position_stack(vjust = 0.5)) +
    coord_polar(theta = "y")+
    labs(caption = "Visits from 1/1/18-6/30/18")+
    ggtitle("Count of Unique Wound Occurrences")+
    theme(plot.caption = element_text(size= 8, hjust = .5))+
    theme(plot.title = element_text(hjust = 0.5))+
    theme(plot.subtitle = element_text(hjust = 0.5))+
    ylab("")+
    xlab("")+
    theme(axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid  = element_blank())
  })
p1

当前输出:

数据:

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

通常您可以为此使用函数参数。由于 data 参数引用数据集,因此您可以直接引用原始数据集中的变量。在您的示例中,这将是 data$Wound.Type.

但是,您在函数中动态添加 Count 变量,因此它不在您传递给 data 参数的数据集中。您可以在函数中创建一个新对象,而不是直接将数据集传递给 ggplot()。这将允许您引用此 "mutated" 数据集中的变量。

这是一个示例,我创建了一个名为 dat2 的新数据集,它在 ggplot() 中使用,可用于 fill 名称。

函数的关键变化是在函数中创建一个新对象:

dat2 = data %>%
            group_by(ServiceSite, Wound.Type) %>%
            summarise(Count = n()) %>%
            mutate(share = round(Count / sum(Count), digits = 2)) 

并将此对象用于 fill 标签:

scale_fill_discrete(labels = paste(dat2$Wound.Type, dat2$Count))

随着您的其他变化:

DF %>% 
     split(.$ServiceSite) %>%
     imap(function(data, site) {
          dat2 = data %>%
               group_by(ServiceSite, Wound.Type) %>%
               summarise(Count = n()) %>%
               mutate(share = round(Count / sum(Count), digits = 2)) 
          ggplot(dat2, aes(x = "", y = Count, fill = Wound.Type)) +
               geom_col(width = 1) +
               scale_fill_discrete(labels = paste(dat2$Wound.Type, dat2$Count))+
               facet_grid(facets = .~ServiceSite, labeller = label_value)+
               geom_text(aes(label = Count, y = ), position = position_stack(vjust = 0.5)) +
               coord_polar(theta = "y")+
               labs(caption = "Visits from 1/1/18-6/30/18")+
               ggtitle("Count of Unique Wound Occurrences")+
               theme(plot.caption = element_text(size= 8, hjust = .5))+
               theme(plot.title = element_text(hjust = 0.5))+
               theme(plot.subtitle = element_text(hjust = 0.5))+
               ylab("")+
               xlab("")+
               theme(axis.text = element_blank(),
                     axis.ticks = element_blank(),
                     panel.grid  = element_blank())
     })