0

My code was working fine until I added another geom_hline() layer to my plot. Now I keep getting this error and I don't understand what I'm doing wrong. I want to have two horizontal lines both red but one either dotted or dashed at the yintercept provided.

Error in `[[<-.data.frame`(`*tmp*`, v, value = c(1, 2)) : 
  replacement has 2 rows, data has 1

Code:

box_whisk_graph<-ggplot(data = box_fresh_chloride, aes(x = factor(year), y = val)) +
  geom_boxplot(coef=10) +
  geom_hline(aes(yintercept = 230,color="red"),size=1.3)+
  geom_hline(aes(yintercept = 860,color="red"),size=1.3)+
scale_color_manual("",
                     values = c("red"="red","red"="red"),
                     labels=c("Freshwater Aquatic Life (chronic)\nCriteria for chloride = 230 mg/L","Freshwater Aquatic Life Criteria (acute) for chloride = 860 mg/L"),
      guide=guide_legend(override.aes=list(linetype=c(1,2), lwd=c(1,0.5))))

dput of data:

structure(list(orgid = c("USGS-NJ", "USGS-NJ", "USGS-NJ", "USGS-NJ", 
"USGS-NJ", "USGS-NJ"), locid = c("USGS-01482500", "USGS-0146453250", 
"USGS-01392150", "USGS-01411035", "USGS-01411466", "USGS-01411444"
), stdate = structure(c(16394, 16610, 16328, 16583, 16602, 16602
), class = "Date"), sttime = c("09:45:00", "11:00:00", "10:40:00", 
"09:45:00", "12:00:00", "10:30:00"), charnam = c("Chloride", 
"Chloride", "Chloride", "Chloride", "Chloride", "Chloride"), 
    val = c(23.6, 221, 144, 10.8, 10.5, 5.76), valunit = c("mg/l", 
    "mg/l", "mg/l", "mg/l", "mg/l", "mg/l"), swqs = c("FW2-NT", 
    "FW2-NT", "FW2-NT", "PL", "FW2-NT", "PL"), region = c("Lower Delaware", 
    "Lower Delaware", "Northeast", "Atlantic Coast", "Lower Delaware", 
    "Atlantic Coast"), WMA = c(17L, 20L, 4L, 15L, 17L, 16L), 
    year = c(2014, 2015, 2014, 2015, 2015, 2015)), .Names = c("orgid", 
"locid", "stdate", "sttime", "charnam", "val", "valunit", "swqs", 
"region", "WMA", "year"), row.names = c(NA, 6L), class = "data.frame")
camille
  • 16,432
  • 18
  • 38
  • 60
NBE
  • 641
  • 2
  • 11
  • 33
  • If you want to get in the habit of some basic debugging, go through taking out lines of code to see when the error occurs. It isn't from the addition of a second `geom_hline`, but from assigning multiple linetypes to a legend that only has one line in your `override.aes`. What you've got going on is why you should map values onto aesthetics instead of making redundant copies of the same geom – camille Jun 20 '18 at 19:57
  • @camille Not sure what you mean by that.. correct me if Im wrong but are you saying I should only have one geom_hline? I tried that it gave me the same error. I thought if you have the guide_legend with the two line types it would work. – NBE Jun 21 '18 at 13:28
  • 1
    Your legend only has a single value in it, because you made the legend keys the same (you used `"red"` for both horizontal lines). If you want two different legend keys so you can put different linetypes in you'll need to use different `color` values for each layer. It may help you to read [this answer](https://stackoverflow.com/a/10355844/2461552) – aosmith Jun 21 '18 at 14:15
  • @aosmith gotcha! I did not know that.. thats why I was so confused because I had the two "red", thinking it would process as two values.. Thank you! – NBE Jun 21 '18 at 14:20
  • 1
    Yes, you only need one `geom_hline`. The ggplot model is to map values onto aesthetics such as linetype. But aside from that, what I said is that the multiple `geom_hline`s *aren't* what's throwing the error – camille Jun 21 '18 at 14:32

0 Answers0