Search code examples
rgeom-bar

How to add bottom lines to geom_bar groups in R


Is there a way to add bottom red lines to the group or bar plots like in the image below? Other than geom_hline()

Here is the code

sample <- c("AA", "AA", "AA", "BB", "BB", "BB")
location<- c("Washington", "Mongolia", "Egypt", "Washington", "Mongolia", "Egypt" )
value <- c(0.03, 0.06, 0.02, 0.0051, 0.0082, 0.003)
data <- data.frame(sample, location, value)


ggplot(data, aes(fill=location, y=value, x=sample)) + 
    geom_bar(position="dodge", stat="identity", color="black")+
theme_classic()+
 scale_fill_grey() +
  scale_x_discrete(limits=c("BB", "AA"))

enter image description here


Solution

  • One way could be like this: We just add a geom_col with the desired y:

    library(tidyverse)
    data %>% 
     # mutate(value_new = 0.0001) %>% # if desired to put in y in geom_col()
      ggplot(aes(fill=location, y=value, x=sample)) + 
      geom_bar(position="dodge", stat="identity", color="black")+
      theme_classic()+
      scale_fill_grey() +
      scale_x_discrete(limits=c("BB", "AA"))+
      geom_col(aes(x=sample, y=0.0001), fill="red")
    

    enter image description here