user113156
user113156

Reputation: 7107

plotting an average line in ggplot

Plotting an average line in ggplot.

I have the following data;

structure(list(Region.in.country = c("Andalucia", "Aragon", "Asturias", 
"Canary Islands", "Cantabria", "Castilla-La Mancha", "Castilla y Leon", 
"Cataluna", "Comunidad Valenciana", "Extremadura", "Galicia", 
"Islas Baleares", "La Rioja", "Madrid", "Murcia", "Navarra", 
"Pais Vasco"), count = c(540L, 117L, 74L, 362L, 36L, 150L, 299L, 
952L, 797L, 72L, 283L, 353L, 39L, 1370L, 302L, 46L, 255L)), .Names = c("Region.in.country", 
"count"), row.names = c(NA, -17L), class = c("tbl_df", "tbl", 
"data.frame"), na.action = structure(18L, .Names = "18", class = "omit"))

I am trying to add an average line across the bar plot in ggplot 2. The average line is the avergae of the count column over the 17 regions.

sum(region$count) / 17

 ggplot(data = region, aes(x = Region.in.country, y = count)) +
   geom_bar(stat="identity") +
   geom_line(data = region, aes(355.7059)) +
   coord_flip()

The above code returns an error

EDIT:

enter image description here

Upvotes: 0

Views: 9876

Answers (2)

InfiniteFlash
InfiniteFlash

Reputation: 1058

This should do the job. Credit to bouncyball for suggesting aes(yintercept = mean(count)) instead of yintercept = 355.7059

  ggplot(region, aes(x= reorder(Region.in.country, count), count))+
    geom_bar(stat ="identity")+
    coord_flip()+
    xlab("Region")+
    ylab("Counts")+
    geom_hline(aes(yintercept = mean(count)))  

enter image description here

If you want to create an ordered bar plot (by the numeric value), always remember to use reorder() on the column beforehand. It'll be unsorted otherwise even if you use arrange() or sort() to sort the data before plotting it. If you don't use reorder() on it, it'll be sorted by the corresponding id variable, Region.in.country in alphabetical order (as shown in the other answer posted after this one).

Upvotes: 4

AlphaDrivers
AlphaDrivers

Reputation: 136

Use geom_hline as follows:

avg <- mean(region$count)

region %>%
  ggplot(aes(x = Region.in.country, y = count)) +
  geom_bar(stat="identity") +
  geom_hline(aes(yintercept = avg)) +
  coord_flip()

result of the code

Upvotes: 0

Related Questions