Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: display text labels from one group only

Tags:

r

ggplot2

My setup:

I have a tibble of basketball players and their statistics.

library(tidyverse)

df <- tibble(
  season = c(2010, 2011, 2012, 2013, 2014,
             2010, 2011, 2012, 2013, 2014),
  player = c("player_a", "player_a", "player_a", "player_a", "player_a",
             "league_avg", "league_avg", "league_avg", "league_avg", "league_avg"),
  fg_perc = c(.4912, .6083, .3095, .5525, .4289,
              .4825, .4836, .4819, .4860, .4848),
  points_game = c(20, 18, 15, 19, 18,
                  12, 12, 13, 11, 12)
)

I've displayed a certain column (fg_perc) as a geom_line() for both player_a and league_avg. I've also wrapped it in a custom function, as I'll be using the same method for other statistics.

make_chart <- function(target_column) {
  df %>% 
    ggplot(aes_string("season", target_column, label = target_column)) + 
    geom_line(aes(color = player), size = 1.33)
}

make_chart("fg_perc")

My issue:

I want to display the fg_perc values as percentages for player_a data only. I can't figure out how to do this without eliminating the legend (when I spread() league_avg). I'm looking for a solution that will also be able to keep non-percentage numbers as-is, and display them in similar fashion (e.g. points_game for player_a only). The output would look something like this (please excuse the MS Paint):

enter image description here

Thanks!

like image 571
Sean G Avatar asked Jan 05 '23 01:01

Sean G


1 Answers

Check this out: You can set your condition with ifelse inside geom_text:

  • if player==the player of interest, the label will be the % value

  • otherwise, the label will be empty " "

You can whatever condition you want, or multiple players.

df %>% 
        ggplot(aes(season,fg_perc))+
        geom_line(aes(color = player), size = 1.33)+
        geom_text(aes(label=ifelse(player=="player_a",paste0(100*fg_perc,"%")," ")))

enter image description here

EDIT

If you want to wrap the code in a function, you can use try the following, where you use aes_string inside geom_text to be able to pass the column name as a string.

In case all your target columns are percentages:

  • you can multiply the column by 100 in advance inside the function (or mutate before passing the df)

    make_chart <- function(target_column) {
    
        df[,target_column] <- 100*df[,target_column]
    
        df %>% 
                ggplot(aes_string("season", target_column)) + 
                geom_line(aes(color = player), size = 1.33) +
                geom_text(data=filter(df,player=="player_a"),
                          aes_string(label=target_column))+
                # a work around to add the % sign
                geom_text(data=filter(df,player=="player_a"),
                          aes(label="%"),
                          hjust=-1.5) }
    

enter image description here

like image 200
OmaymaS Avatar answered Jan 13 '23 10:01

OmaymaS