Home > Net >  Using ggplot2, Why is it returning NULL when i am trying to add an average line through my bar chart
Using ggplot2, Why is it returning NULL when i am trying to add an average line through my bar chart

Time:03-07

options(repr.plot.width=8, repr.plot.height=3)

    ggplot(filtered, aes(x =reorder(CountyName,Added_Column), y = Added_Column))  
    geom_bar(stat = "identity")  
    coord_flip()   scale_y_continuous(name="total cases per 100k by county 2021-12-21 ")  
    geom_text(aes(label=Added_Column), hjust=2.9, color="white", size=3.5)
   
    geom_line()  
    geom_hline(xintercept = mean(Added_Column), color="blue")
  
    ggtitle("Covid Cases per 100k by county ")  
  
  
    theme(axis.text.x = element_text(face="bold", color="#008000",
                                   size=8, angle=0),
        axis.text.y = element_text(face="bold", color="#008000",
                                   size=8, angle=0))

enter image description here

here is what the graph looks like before i add the following :

geom_line()  
geom_hline(yintercept = mean(Added_Column), color="blue")

when i add this line of code trying to get an average line to run through my graph , it returns NULL. what can i do to fix this??

CodePudding user response:

try:

geom_hline(aes(yintercept=mean(Added_Column, na.rm=T)), color="blue)

CodePudding user response:

I updated your code and I think this is what you are after.

Sample data:

filtered<-structure(list(CountyName = c("Monaghan", "Donegal", "Louth", 
"Dublin", "Cavan", "Carlow", "Limerick", "Meath", "Waterford", 
"Longford", "Westmeath", "Kildare", "Laois", "Offaly", "Mayo", 
"Cork", "Galway", "Roscommon", "Tipperary", "Leitrim", "Kilkenny", 
"Wexford", "Kerry", "Calre", "Sligo", "Wicklow"), Added_Column = c(18286, 
18107, 17870, 16361, 15937, 15406, 14520, 14374, 14312, 14266, 
14196, 14199, 13237, 12976, 12409, 12091, 11987, 11893, 11660, 
11428, 11414, 11384, 11357, 11278, 10636, 10342)), spec = structure(list(
    cols = list(CountyName = structure(list(), class = c("collector_character", 
    "collector")), Added_Column = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x1b70d488>, row.names = c(NA, 
-26L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))

Sample code:

library(ggplot2)

ggplot(filtered, aes(x = reorder(CountyName, Added_Column), y = Added_Column))  
  geom_bar(stat = "identity")  
  geom_line()  
  geom_hline(yintercept = mean(filtered$Added_Column), size=1, color="blue")  
  geom_text(aes(label=Added_Column), hjust=2.9, color="white", size=3.5) 
  coord_flip()   
  labs( x="", y="Total cases per 100k by county 2021-12-21", title="Covid Cases per 100k by county ") 
  theme_bw() 
  theme(axis.text.x = element_text(face="bold", color="#008000",size=12, angle=0),
        axis.text.y = element_text(face="bold", color="#008000",size=12, angle=0),
        axis.title.x = element_text(face="bold", size=12, color="black"),
        plot.title  = element_text( face = "bold", colour = "black", size = 20,, hjust = 0.5)) 
  scale_y_continuous(expand=c(0,0))

Plot:

enter image description here

  • Related