Put text onto each bar

Hello, Can someone please help me put text onto each bar. I am using annotate but not sure how it works to put text on each bar. Here is code:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.0.5
#> Warning: package 'tibble' was built under R version 4.0.5
#> Warning: package 'tidyr' was built under R version 4.0.4
#> Warning: package 'readr' was built under R version 4.0.5
#> Warning: package 'dplyr' was built under R version 4.0.5
#> Warning: package 'forcats' was built under R version 4.0.4
library(reshape2)
#> 
#> Attaching package: 'reshape2'
#> The following object is masked from 'package:tidyr':
#> 
#>     smiths
library(data.table)
#> Warning: package 'data.table' was built under R version 4.0.4
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:reshape2':
#> 
#>     dcast, melt
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
library(scales)
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
library(cowplot)
#> Warning: package 'cowplot' was built under R version 4.0.5
library(ggpubr)
#> 
#> Attaching package: 'ggpubr'
#> The following object is masked from 'package:cowplot':
#> 
#>     get_legend
theme_set(theme_pubr())
#1st OEQ
df <- read.csv("F:/Ch2_Figures/frequency_response.csv")
colnames(df)[1] = "modality"
colnames(df)[2] = "First_response"
colnames(df)[3] = "Second_response"
colnames(df)[4] = "Third_response"
colnames(df)[5] = "Fourth_response"
colnames(df)[6] = "Fifth_response"
dput(df)
#> structure(list(modality = c("Appearance", "Aroma", "Flavor", 
#> "Texture", "Abstract"), First_response = c(201L, 8L, 107L, 151L, 
#> 282L), Second_response = c(72L, 17L, 148L, 225L, 260L), Third_response = c(54L, 
#> 17L, 177L, 220L, 360L), Fourth_response = c(46L, 24L, 168L, 198L, 
#> 356L), Fifth_response = c(39L, 13L, 122L, 150L, 402L), Frequency = c(412L, 
#> 79L, 722L, 944L, 1660L)), class = "data.frame", row.names = c(NA, 
#> -5L))
colnames(df) <- c("modality","First_response","Second_response","Third_response",
                  "Fourth_response", "Fifth_response", "Frequency")

#Help from R-community
df2 <- select(df, -Frequency)

dat <- melt(df2)
#> Warning in melt(df2): The melt generic in data.table has been passed a
#> data.frame and will attempt to redirect to the relevant reshape2 method;
#> please note that reshape2 is deprecated, and this redirection is now
#> deprecated as well. To continue using melt methods from reshape2 while both
#> libraries are attached, e.g. melt.list, you can prepend the namespace like
#> reshape2::melt(df2). In the next version, this warning will become an error.
#> Using modality as id variables
#Using modality as id variables
p1 <- ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
  theme(legend.text = element_text(size = 7))
p1 + annotate("text", x = c(1,2,3,4,5), y = c(370,210,20,184,250),
              label = c("a", "a", "ab", "bcd", "b"), size = 2, fontface="bold")

Created on 2021-08-30 by the reprex package (v2.0.1)

Here is data:

squads <- tibble::tribble(
                   ~test, ~first_res, ~second_res, ~third_res, ~fourth_res, ~fifth_res, ~Frequency,
            "Appearance",       201L,         72L,        54L,         46L,        39L,       412L,
                 "Aroma",         8L,         17L,        17L,         24L,        13L,        79L,
                "Flavor",       107L,        148L,       177L,        168L,       122L,       722L,
               "Texture",       151L,        225L,       220L,        198L,       150L,       944L,
              "Abstract",       282L,        260L,       360L,        356L,       402L,      1660L
            )
head(squads)
#> # A tibble: 5 x 7
#>   test       first_res second_res third_res fourth_res fifth_res Frequency
#>   <chr>          <int>      <int>     <int>      <int>     <int>     <int>
#> 1 Appearance       201         72        54         46        39       412
#> 2 Aroma              8         17        17         24        13        79
#> 3 Flavor           107        148       177        168       122       722
#> 4 Texture          151        225       220        198       150       944
#> 5 Abstract         282        260       360        356       402      1660

Created on 2021-08-30 by the reprex package (v2.0.1)

you are asking for text to be placed to be placed on/ around the bars.
I use geom_text() . it can be colored in the aes() function,
I use label= n (after using a count() function)

link to full article explaining how to geom_text guide

1 Like

Thanks @unicoRn_code for the help. I tried what you suggested, but how I can change text for each bar. I am getting same for all bars, please find below the code I used:

ggplot(dat, aes(modality, value, fill=interaction(variable))) +
  geom_bar(stat='identity', position='dodge') +
  ggtitle("A: General OEQ") + 
  theme_bw() + theme(axis.text.x = element_text(angle=90, hjust=1, size = 10, face = "bold")) +
  scale_fill_brewer('Open-end', palette='RdPu') +  theme(legend.position = c(.51, .80)) +
  theme(legend.title=element_blank()) +
  xlab("") + ylab("Frequency") + theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(colour = "black")) + 
  theme(legend.key.size = unit(1.0, "cm"), legend.key.width = unit(0.5,"cm"),
        legend.key.height = unit(0.5,"cm")) + 
  theme(legend.text = element_text(size = 7)) +
  geom_text(aes(label = "a"), position = position_dodge(0.9), size = 2,
            vjust = -0.25, fontface="bold")

Screenshot 2021-08-30 212255

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.