How to add new part to the barplot in ggplot2?

Viewed 30

I have a barplot graph in ggplot2. My dataset is imbalanced and I want to add a part in this graph as I represent in below in output.png. How can I add left 2nd grey part? Please see my code and output below.

df2 <- data.frame(Class=rep(c("A", "B"), each=1),
                  AB_Event=rep(c( "Primary Output"),2),
                  Count=c(398, 4106))
p <- ggplot(data=df2, aes(x=AB_Event, y=Count, fill=Class)) +
  geom_bar(stat="identity", color="black", position=position_dodge())+
  geom_text(aes( label=Count), vjust = -0.2, size = 5,  position = position_dodge(0.9) )+
  theme_minimal()

enter image description here

1 Answers

Use geom_rect and annotate native from ggplot2

df2 <- data.frame(Class=rep(c("A", "B"), each=1),
                  AB_Event=rep(c( "Primary Output"),2),
                  Count=c(398, 4106))

p <- ggplot(data=df2, aes(x=AB_Event, y=Count, fill=Class)) +
    geom_bar(stat="identity", color="black", position=position_dodge())+
    geom_rect(xmin = 0.55, xmax = 1, ymin = 700, ymax = 4106, fill = '#f8766d', colour = 'black')+
    annotate('text', x = 0.75, y = 4250, label = '4106')+
    annotate('text', x = 0.75, y = 3500, label = '3708')+
    geom_text(aes(label=Count), vjust = -0.2, size = 5,  position = position_dodge(0.9) )+
    theme_minimal()

p
Related