regents
regents

Reputation: 626

Add counts to legend labels

I am trying to add the counts/percentages to legend labels of a pie chart. Pie charts are terrible, I know, but that's not the point of this post. I would like to paste the values of "Count" to the "Wound.Type" label on the legend but can't figure out how to access the Counts for each iteration of the following code. The goal would be something along the lines of "Laceration 5" or whatever the count is. I have tried ".~Count" and ".~Wound.Type", ".$Count" and ".$Wound.Type" but I don't understand how to access the specific values I'd like.

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

Current output: enter image description here

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

Upvotes: 3

Views: 2670

Answers (1)

aosmith
aosmith

Reputation: 36086

Generally you can use your function arguments for this. Since the data argument refers to the dataset, you can refer to variables in the original dataset directly. In your example that would be data$Wound.Type.

However, you add the Count variable dynamically within the function so this isn't in the dataset you pass to the data argument. Instead of passing the dataset directly to ggplot() you can make a new object within your function. This will allow you to refer to variables in this "mutated" dataset.

Here is an example, where I make a new dataset called dat2 that is used within ggplot() and can be used for the fill names.

The key changes to the function are making a new object within the function:

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

And using this object for the fill labels:

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

The changes along with the rest of your:

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())
     })

Upvotes: 2

Related Questions