rggplot2geom-textgghighlight

gghighlight (R): Labeling bar charts


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.

############## EDIT ###########

Besides graying out certain columns (see @TarJae's answer), there is also the possibility to make them transparent (essential parts are from this post: ggplot transparency on individual bar):

subset_df <- df %>% 
  mutate(alpha.adj = as.factor(ifelse(group != "B", 1, 0.6)))


ggplot(data = subset_df, aes(x = group, y = mean, fill = value, alpha=factor(alpha.adj))) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = round(mean, digits = 2)), 
            stat= "identity", 
            vjust = -.5,
            position = position_dodge(width = .9)
  ) +
  scale_alpha_manual(values = c("0.6"=0.6, "1"=1), guide='none') 

[Bar chart with transparent bars and labels]


Solution

  • 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