Home > Enterprise >  How to label under threshold line in barplot made in ggplot?
How to label under threshold line in barplot made in ggplot?

Time:10-05

I have a barplot as follows:

data <- data.frame(
  name=letters[1:5],
  value=sample(seq(4,15),5)
)
data

The data looks as follows:

name    value
a       5
b       6
c       14
d       15
e       7

I made barplots using ggplot with a line on point 10

ggplot(data)  
    geom_bar( aes(x=name, y=value), stat="identity", fill="skyblue", alpha=0.7)  
    geom_hline(yintercept = 10,color = "red", linetype = "solid")

enter image description here

I want to add label above as high and below as low and on 10 as normal

CodePudding user response:

You can use annotations:

ggplot(data)  
    geom_bar( aes(x=name, y=value), stat="identity", fill="skyblue", alpha=0.7)  
    geom_hline(yintercept = 10,color = "red", linetype = "solid")  
    annotate("text", x = "e", y = 10.5, label = "high")  
    annotate("text", x = "e", y = 10, label = "normal")  
    annotate("text", x = "e", y = 9.5, label = "low") 
  • Related