How to change colour and position of geom_text for just one bar in a barplot in ggplot2 (R)?
Asked Answered
D

2

7

I'm trying to create a bar plot with the depicted values written inside the bars using ggplot2. I still want to label the group with value "0" but in a different colour (black) and just above the x-axis. How can I change the position and colour of just this one geom_text?

I've already tried entering a vector into scale_colour_manual but it didn't work (or I didn't do it right).

data <- read.table(text = "group percentage
               group1 30
               group2 29
               group3 0
               group4 18", header=TRUE)

library(ggplot2)
ggplot(data, aes(x=group, y=percentage))+
  theme_bw()+
  geom_bar(stat = 'identity', position = "dodge", fill="#13449f")+
  geom_text(aes(label = percentage), position = position_dodge(0.9), 
  vjust=1.3, colour = "white", size=6)

With this code there is no label for group3 since there is no bar either. I'd like to still have a label in black above the x-axis.

Decongestant answered 8/1, 2019 at 16:2 Comment(1)
Welcome to Stackoverflow! This is a very well formatted and clear first question, congratulations!Stopple
S
6

Via conditional logic:

library(ggplot2)
ggplot(data, aes(x = group, y = percentage))+
    theme_bw()+
    geom_bar(stat = 'identity', position = "dodge", fill = "#13449f") +
    geom_text(aes(label = percentage), position = position_dodge(0.9), 
              vjust = ifelse(data$percentage > 3, 1.3, -0.3), 
              colour = ifelse(data$percentage > 3, "white", "black"), 
              size = 6)

1

With group3 == 3.1

2

What is comfortable about this approach:

  • it automatically takes care of values that are big and small
  • you do not need a second data frame or geom

Caveat of this approach:

  • What is hardcoded as > 3 should be calibrated for each visualization. It is possible to automatize that part if you dive deeper into how ggplot2 builds graphs, but it would be overkill for this small example.
Stopple answered 8/1, 2019 at 19:28 Comment(1)
Thank you very much, this is exactly what I've been looking for!Decongestant
E
2

Just add another geom_text layer. For example

ggplot(data, aes(x=group, y=percentage))+
  theme_bw()+
  geom_bar(stat = 'identity', position = "dodge", fill="#13449f")+
  geom_text(aes(label = percentage), position = position_dodge(0.9), 
            vjust=1.3, colour = "white", size=6) + 
  geom_text(aes(label = "0", y=1), data=subset(data, percentage==0), size=6)

Here we change the layer data to only include those groups with 0s.

enter image description here

Expertise answered 8/1, 2019 at 16:7 Comment(1)
Thank you so much, it could have been that easy...! But I have a similar problem where the bar is basically too "small" to contain the entire number (e.g. when the percentage is just 1 or 0.5). How can I then shift this single number above the bar and change its colour? Here your "trick" doesn't apply.Decongestant

© 2022 - 2024 — McMap. All rights reserved.