1

I've created a bar chart in ggplot2 and added an hline, no problems there. Where I'm running into trouble is that I want to add the value for the hline somewhere on the hline (ideally just above the line on the left side, just far enough from the axis to be visible as not a tick mark, though the middle would also work) and I'm having a lot of trouble mostly because the geom_label keeps adding the label intended for the hline to the bar chart. Here's the code to produce an example of the kind of chart I'm working on:

# Stack Overflow Example

library(ggplot2)

plotData_A <- data.frame(name = c('Jim', 'Bill', 'Bob', 'Sue', 'Terry', 'Jill'), avg_leads = c(3,5,4,7,4,5))

plotData_B <- data.frame(salesman = c('Megan', 'Tim'), leads = c(7,4))

i <- as.character(plotData_B[1,1])

myPlot <- ggplot(plotData_A, aes(name, avg_leads, fill = name)) + geom_bar(stat = "identity") + theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank()) + scale_fill_brewer(palette="Spectral") + ylab("Average Leads Created per Director") + ggtitle(paste0("Leads Converted: ", i)) + geom_hline(yintercept = plotData_B[which(plotData_B$salesman == i), 2], size = 1.25, linetype = "dotdash") 

Any help you can provide is much appreciated.

Tom.Rampley
  • 402
  • 6
  • 12
  • 6
    Have you tried the answers [here](http://stackoverflow.com/questions/32425784/r-ggplot2-labeling-a-horizontal-line-without-associating-the-label-with-a-serie) and [here](http://stackoverflow.com/questions/12876501/r-ggplot2-labelling-a-horizontal-line-on-the-y-axis-with-a-numeric-value)? – aosmith Oct 19 '16 at 23:13
  • That's perfect, thanks. I have no idea why those didn't come upon my prior searches. – Tom.Rampley Oct 20 '16 at 01:32
  • For what it's worth, the other answers only work so-so because it's just one abline, and the tl;dr is to manually add a geom_text where desired. It would be sweet to be able to add labels programmatically based on some range of 0-1, where 0 is the start of the line and 1 is the end of the line. I tried to add 5 ablines and manually used trial and error to construct the appropriate dataframe for labels that would be at the perimeter of the graph (right or top) to label them. – Hendy Jan 17 '22 at 19:38
  • Added the multiple abline question [here](https://stackoverflow.com/questions/70747032/programmatically-label-multiple-ablines-in-r-ggplot2) – Hendy Jan 17 '22 at 20:17

0 Answers0