Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a histogramm and add the rank above the histogram

My question may be stupid but I could not solve by my own. I have a data which show the sports activiy by year from 1995 to 2015 like this

Year Sportsactivity (h)
1995 300
1996 299
1997 315
1998 286
1999 275
2000 302
2001 304
2002 301
2003 320
2004 342
2005 333
2006 355
2007 370
2008 366
2009 364
2010 362
2011 358
2012 356
2013 354
2014 323
2015 298

Now I would like to create a histogram

ggplot(data, aes(x=Year, y=Sportsactivity)) + geom_bar(stat="identity")+
ggtitle("Sportsactivity with rank the year")

How can I add the rank above every year in the bar chart corresponding to the year?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example 2007 should have the number one above its bar line

>Solution :

A simple solution:

ggplot(data, aes(x=Year, y=Sportsactivity)) + 
  geom_bar(stat="identity")+
  geom_text(aes(label = nrow(data)-rank(Sportsactivity)+1), size = 10, vjust = 2, colour = "white") +
  ggtitle("Sportsactivity with rank the year")

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading