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

R.Van

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.

Roman

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ggplot2 - Change `geom_rect` colour in a stacked barplot

How to change legend title and key order and colour to a multi-stacked barplot in R with ggplot2

How to change colour for barplot in ggplot2 using viridis package?

Change bar plot colour in geom_bar with ggplot2 in r

ggplot2: geom_bar fill colour; how to change to different grouping of data

How to change font color in geom_text in ggplot2 in R?

ggplot2 geom_text position text on horizontal grouped barplot

Manually change colour of one factor level in ggplot2 stacked barplot

ggplot2 barplot bar colour can't be changed properly

How to segregate by one factor but colour by another in ggplot2 R?

Geom_text() in R - how to change a label position of a specific dot in geom_point

How to change the text in the colour legend of ggplot2

R: ggplot2 geom_text data labels for binned geom_bar

r ggplot2 round off geom_text values for geom_bar

ggplot2: Value labels in stacked barplot with geom_bar(position = 'fill')

geom_text how to position the text on bar as I want?

R Change position of text in geom_bar()

Geom_bar with position dodge on one categorical variable and colour on another

ggplot2: how to change the width of colour aesthetic in a bar/col geometry?

How to change barplot in ggplot for R

How to round off data labels for a bar plot in geom_text in ggplot2?

R ggplot2 change geom_bar to a single color

R: ggplot2 let the characters of geom_text exactly cover one X unit

How can I have different geom_text() labels in a faceted, stacked bar graph in R with ggplot?

How to position a geom_text beneath each group's name in the margin of a ggplot in R?

In ggplot2/plotly ,when I use `geom_bar(stat='identity',position='fill')`,how to change number tip to percent format

How to change the default font size in ggplot2 - including geom_text

Change the colour of a subset of labels in barplot R

R: Can I use scale_color_gradient to change the colour of geom_hline in ggplot2?