Home > other >  Horizontal line on ggplot2 barplot
Horizontal line on ggplot2 barplot

Time:07-18

I want a horizontal line to be placed over my ggplot2 bar plot at the average of all the bars:

ggplot(bar_graph, aes(x=V1, y=numBridges))   
  geom_bar(stat = "identity") 
  xlab("Agreement") 
  ylab("Number of Nodes") 
  geom_hline(yintercept=15)

Unfortunately this code gives me the error message:

Error in is.finite(x) : default method not implemented for type 'list'

Which is super odd because 15 is not a list. It's also even more odd because, just to check, I ran the following code to make a vertical line - which worked well:

ggplot(bar_graph, aes(x=V1, y=numBridges))   
  geom_bar(stat = "identity") 
  xlab("Agreement") 
  ylab("Number of Bridge Nodes") 
  geom_vline(xintercept=5)

A couple years ago there was a bug in ggplot2 that could only be resolved by installing directly from github, so in the event that this was still the problem I ran:

remove.packages('ggplot2')
devtools::install_github('hadley/ggplot2')

But that did not work either.

On Mac Monterrey:

packageVersion("ggplot2")
[1] ‘3.3.6.9000’

Does anyone have any ideas or workarounds? Would greatly appreciate any help!

CodePudding user response:

Thanks to neilfws for pointing out that the column in my dataframe was indeed a list. I just did bar_graph$numBridges %>% unlist() to fix it.

I guess a nice reminder that a dataframe can be a cleverly hidden list!

  • Related