0

I have a dataset like below. I am trying to draw horizontal lines for different Band with its colors (Green, Yellow,....,Black), y-axis as Price. I am trying to use facet_grid(.~ Band).

My code below are giving me all the same lines for every Band. Seems like the facet didn't work here.

g <- ggplot(df, aes(y=Price)) +
     facet_grid(.~ Band) +
     geom_hline(yintercept=df$Price[1], colour="green") + 
     geom_hline(yintercept=df$Price[2], colour="yellow") +
     geom_hline(yintercept=df$Price[3], colour="pink") +
     geom_hline(yintercept=df$Price[4], colour="red") +
     geom_hline(yintercept=df$Price[5], colour="black")


print(g)

Thanks! Brenda

Line    Price   Band
Green   1584    A) 1--9
Yellow  1512    A) 1--9
Pink    1386    A) 1--9
Red 1152    A) 1--9
Black   1044    A) 1--9
Green   1566    B) 10--24
Yellow  1476    B) 10--24
Pink    1386    B) 10--24
Red 1152    B) 10--24
Black   1044    B) 10--24
Green   1512    C) 25--49
Yellow  1440    C) 25--49
Pink    1386    C) 25--49
Red 1152    C) 25--49
Black   1044    C) 25--49
Green   1494    D) 50--99
Yellow  1422    D) 50--99
Pink    1386    D) 50--99
Red 1080    D) 50--99
Black   972 D) 50--99
steveb
  • 5,382
  • 2
  • 27
  • 36
Brenda
  • 1
  • I don't have time to write up a full answer, but using `ggplot` effectively requires using the `aes()` function for all mappings between data and aesthetics. (You use `aes(y = Price)`, but you try to do `colour` outside `aes()`, one line at a time. This is bad.) – Gregor Thomas Jan 06 '17 at 20:07
  • The dataset didn't paste properly here. Line with levels (Green, Yellow, Pink, Red,Black) Price: one price for each line for each Band Band: A)1--9, B)10--24, C)25--49, D)50--99 – Brenda Jan 06 '17 at 20:08
  • 1
    Instead of the table view of your data, can you please paste the result of `dput(df)`? This will be copy/pasteable into R. – Gregor Thomas Jan 06 '17 at 20:10
  • structure(list(Line = structure(c(2L, 5L, 3L, 4L, 1L, 2L, 5L, 3L, 4L, 1L, 2L,5L, 3L, 4L, 1L, 2L, 5L, 3L, 4L, 1L), .Label = c("Black", "Green", "Pink", "Red", "Yellow"), class = "factor"), Price = c(1584L, 1512L, 1386L, 1152L, 1044L, 1566L, 1476L, 1386L, 1152L,1044L, 1512L, 1440L, 1386L, 1152L, 1044L, 1494L, 1422L,1386L, 1080L, 972L), Band = structure(c(1L, 1L, 1L, 1L, 1L, 2L,2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("A) 1--9", "B) 10--24", "C) 25--49", "D) 50--99"), class = "factor")), .Names = c("Line", "Price", "Band"), row.names = c(NA, -20L), class = "data.frame") – Brenda Jan 06 '17 at 20:15
  • Thanks! In the future, please edit things like that into your question, don't post them in comments. In case the duplicate isn't clear, I think the code you need is `ggplot(df, aes(y = Price)) + geom_hline(aes(yintercept = Price, color = Line)) + facet_wrap(~ Band)`. You can use `scale_color_manual` to make the colors match the labels. – Gregor Thomas Jan 06 '17 at 20:18
  • Thank you so much, Gregor. I modified my code as below, now it works!!! g <- ggplot(df,aes(y=Price))+facet_grid(.~ Band)+geom_hline(aes(yintercept=Price, color= Line))+scale_color_manual(values=c("green", "yellow", "pink","red","black")) – Brenda Jan 06 '17 at 23:24

0 Answers0