Home > Net >  gghighlight (R): Labeling bar charts
gghighlight (R): Labeling bar charts

Time:06-15

Alright, after a long silent read along, here's my first question. I am trying to add corresponding labels of unhighlighted items for a grouped barplot. When I insert gghighlight in front of the geom_text I get the following plot:

    library(tidyverse)
    library(gghighlight)


df <- data.frame (group = c("A", "A", "B", "B", "C", "C"),
                  value  = c("value_1", "value_2","value_1", "value_2","value_1", "value_2"),
                  mean = c(1.331, 1.931, 3.231, 3.331, 4.631, 3.331)
)

ggplot(data = df, aes(x = group, y = mean, fill = value))  
  geom_bar(stat = "identity", position = "dodge")  
  gghighlight(group != "B",
              label_key = group
              )  
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
            ) 

Bar chart with only highlighted labels

If I move gghightlight behind the geom_text I get the following plot:

ggplot(data = df, aes(x = group, y = mean, fill = value))  
  geom_bar(stat = "identity", position = "dodge")  
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
            )  
  gghighlight(group != "B",
              label_key = group)

Bar chart with double labels: highligthed and unhighligthed

Is there a way to label the unhighligthed bars like the highlighted ones?

Thanks in advance.

CodePudding user response:

Are you looking for this? This is a solution without using gghighlight package:

library(tidyverse)

subset_df <- df %>% 
  mutate(highlight = if_else(group != "B", mean, NA_real_))

ggplot(data = subset_df, aes(x = group, y = mean, group=value))  
  geom_col(fill = 'grey', alpha = 0.6, position = 'dodge')  
  geom_col(aes(y = highlight, fill = value), position = 'dodge')  
  geom_text(aes(group, label = round(mean, digits = 2)),
            position = position_dodge(width = 1))

enter image description here

CodePudding user response:

This is a solution with the gghighlight package and some limited hacky code.

When reading the vignette, I noticed that the author of the package "filters out" the data that are not highlighted. You can see that if you save your highlighted plot in p_h and then look at p_h$data, the values for group B have disappeared.

library(tidyverse)
library(gghighlight)

p_h <- ggplot(data = df, aes(x = group, y = mean, fill = value))  
  geom_bar(stat = "identity", position = "dodge")  
  gghighlight(group != "B",
              label_key = group)  
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)) 

> p_h$data
  group   value  mean
1     A value_1 1.331
2     A value_2 1.931
5     C value_1 4.631
6     C value_2 3.331

If we re-insert the data (after the call to gghighlight() has removed them), then geom_text() will be able to find the means for group B again.

One can "recover" the data and re-insert them with the following code:

### create a ggplot object with the original complete data
### you could check that with p_to_copy_data$data
p_to_copy_data <- ggplot(data = df)

### copy the complete data to your highlighted plot data section
p_h$data <- p_to_copy_data$data
p_h

This yields the following graph:

enter image description here

  • Related