Home > Blockchain >  How to draw lines at each panel divided by facet_wrap() in R?
How to draw lines at each panel divided by facet_wrap() in R?

Time:11-03

Here is one data

variety<- c("CV1","CV1")
trt<- c("N0","N1")
yield<- c(100,150)
dataA<- data.frame(variety,yield,trt)

and I made a graph using facet_wrap() and drew lines.

ggplot(data=dataA, aes(x=variety, y=yield)) 
  geom_bar(stat="identity", position="dodge", width=0.7, size=1)  
  coord_flip()  
  facet_wrap(~ trt)  
  geom_hline(yintercept=100, linetype = "dashed", color="Dark blue")  
  geom_hline(yintercept=150, linetype = "dashed", color="Dark blue")  
  windows(width=10, height=6)

enter image description here

when I drew two lines, it's duplicated at each panel. I'd like to draw a line at each each panel like below.

enter image description here

Could you let me know about that?

Always many thanks

CodePudding user response:

Use aes to add yintercept in the aesthetics:

ggplot(data=dataA, aes(x=variety, y=yield)) 
  geom_bar(stat="identity", position="dodge", width=0.7, size=1)  
  coord_flip()  
  facet_wrap(~ trt)  
  geom_hline(aes(yintercept = c(100, 150)), linetype = "dashed", color="Dark blue")

enter image description here

  • Related