0

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

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15

1 Answers1

2

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

Maël
  • 45,206
  • 3
  • 29
  • 67
  • Thank you so much!! How about drawing at different positions? For example, 120 at left panel, and 140 at right panel? How can I do that? – Jin.w.Kim Nov 02 '22 at 16:53
  • 1
    Then I guess the best would be to create a vector and add it to your data frame, as it's done here with `yield` – Maël Nov 02 '22 at 16:55
  • 1
    I think adding a new frame would be in order, perhaps `geom_hline(..., data=data.frame(trt = c("N0","N1","N1"), yint = c(100, 150, 160)))` (I added 160 just to demonstrate application of multiple hlines per facet). Also, the new frame doesn't need to be defined in-place like this, likely before the plotting expression. – r2evans Nov 02 '22 at 16:56
  • 1
    Thank you for your comments!! If I do this, geom_hline(aes(yintercept=c(120,140)), linetype = "dashed", color="Dark blue"), it works well. – Jin.w.Kim Nov 02 '22 at 16:58