Search code examples
rggplot2geom-text

custom geom text colors in ggplot2 with R


I want to visualize the tennis data using ggplot. So far I am able to visualize the data based on winner/loser data.

2015    Flavia Pennetta 81
2014    Serena Williams 65
2013    Serena Williams 109
2012    Serena Williams 94
2011    Samantha Stosur 61
2010    Kim Clijsters   58
2009    Kim Clijsters   82
2008    Serena Williams 89
2007    Justine Henin   70
2006    Maria Sharapova 66
2015    Roberta Vinci   47
2014    Caroline Wozniacki  49
2013    Victoria Azarenka   91
2012    Victoria Azarenka   87
2011    Serena Williams 41
2010    Vera Zvonareva  31
2009    Caroline Wozniacki  66
2008    Jelena Jankovic 79
2007    Svetlana Kuznetsova 54
2006    Justine Henin   58

Here is the code:

ggplot(data=f_data, aes(x=year, y=winner_totalPointWon, fill=output)) +
  geom_bar(stat="identity", position=position_dodge())+geom_text(aes(label=winner), position = position_dodge(0.9),vjust=0,angle=90)

enter image description here

How can I change the text color of the names grouped by player names so that each player can be represented by distinct color?


Solution

  • How about this (I only used the first name):

    ggplot(data=f_data, aes(x=year, y=winner_totalPointWon, fill=output)) +
          geom_bar(stat="identity", position=position_dodge())+
    geom_text(aes(label=winner, colour=winner, size=5), position = position_dodge(0.9),vjust=0,angle=90)
    

    with output

    text with different colors