teamcolors icon indicating copy to clipboard operation
teamcolors copied to clipboard

`scale_color_teams` behavior has changed (?)

Open math-mcshane opened this issue 3 years ago • 6 comments

Hi, this question is either a bug or user error or both, so I wasn't sure whether to post it as an issue. Apologies if it is user error.

As recently as May 2021, I was able to change the labels on my ggplot with scale_color_teams(labels = my_vector_of_labels).

Here's a toy example and my work-around:

library(ggplot2)
library(teamcolors)

#Example data; subset of data from bkref.com
toy_data = data.frame(
  team = rep(c("BOS", "SAS"), times = 3), 
  year = rep(2014:2016, each = 2), 
  Win_Pct = c(0.305, 0.756, 0.488, 0.671, 0.585, 0.817), 
  teamname = rep(c("Boston Celtics", "San Antonio Spurs"), times = 3)
)

# Base plot
# Original plot used 37 seasons of data and all 30 teams, so shrinking the label size was critical
p = ggplot(toy_data, aes(x = year, y = Win_Pct, color = teamname)) + 
  geom_line(size = 5) # line size for clarity of issue
p

image

# What worked as recently as May 2021, but now does not
p + scale_color_teams(1, labels = toy_data$team)

image

# How the preceding plot was intended to look
p + scale_color_discrete(labels = toy_data$team, type = league_pal(lg = "nba"))

image

math-mcshane avatar Nov 13 '21 15:11 math-mcshane

FWIW, here's the original plot (whose purpose was simply to extol the virtues of teamcolors). I simply replaced a call to the scale_color_teams function with a call to scale_color_discrete. image

math-mcshane avatar Nov 13 '21 15:11 math-mcshane

@math-mcshane can you confirm if this is still a problem? When I run your MWE:

library(ggplot2)
library(teamcolors)

#Example data; subset of data from bkref.com
toy_data = data.frame(
  team = rep(c("BOS", "SAS"), times = 3), 
  year = rep(2014:2016, each = 2), 
  Win_Pct = c(0.305, 0.756, 0.488, 0.671, 0.585, 0.817), 
  teamname = rep(c("Boston Celtics", "San Antonio Spurs"), times = 3)
)

# Base plot
# Original plot used 37 seasons of data and all 30 teams, so shrinking the label size was critical
p = ggplot(toy_data, aes(x = year, y = Win_Pct, color = teamname)) + 
  geom_line(size = 5) # line size for clarity of issue
p
# What worked as recently as May 2021, but now does not
p + scale_color_teams(1, labels = toy_data$team)

The plot looks as intended.

beanumber avatar Feb 05 '23 19:02 beanumber

@beanumber just wanted to follow up: I was not able to recreate the issue I was having. I'm having difficulty remembering what the issue was (as I had found a workaround).

I did notice that these two lines produced slightly different output: SAS has a darker shade in the latter plot.

p + scale_color_teams(1, labels = toy_data$team) p + scale_color_discrete(labels = toy_data$team, type = league_pal(lg = "nba"))

math-mcshane avatar Dec 04 '23 07:12 math-mcshane

Ah yes, I re-rendered my original code (which had no changes) and got different output. I guess my workaround was to tell students to ignore the problem?

image

Here's a file with the data (older file from bkref.com; only necessary columns) Team_Season_Since_1979_Rate.txt

Here's the code for the above plot. Notice that, e.g., the Kings are no longer purple.

all_NBA_rate = read.csv("Team_Season_Since_1979_Rate.txt")
all_NBA_rate$team = recode(all_NBA_rate$team, "SEA" = "OKC", "KCK" = "SAC",
    "WA1" = "WAS", "SDC" = "LAC", "VAN" = "MEM", "CHR" = "CHA",
    "NOO" = "NOP", "NOH" = "NOP", "NJN" = "BKN")
team_dict = data.frame(
    team = c("ATL", "BOS", "BKN", "CHA", "CHI", "CLE", "DAL", "DEN", "DET", "GSW",
    "HOU", "IND", "LAC", "LAL", "MEM", "MIA", "MIL", "MIN", "NOP", "NYK",
    "OKC", "ORL", "PHI", "PHO", "POR", "SAC", "SAS", "TOR", "UTA", "WAS"),
teamname = c("Atlanta Hawks", "Boston Celtics", "Brooklyn Nets",
    "Charlotte Hornets", "Chicago Bulls", "Cleveland Cavaliers",
    "Dallas Mavericks", "Denver Nuggets", "Detroit Pistons",
    "Golden State Warriors", "Houston Rockets", "Indiana Pacers",
    "Los Angeles Clippers", "Los Angeles Lakers", "Memphis Grizzlies",
    "Miami Heat", "Milwaukee Bucks", "Minnesota Timberwolves",
    "New Orleans Pelicans", "New York Knicks", "Oklahoma City Thunder",
    "Orlando Magic", "Philadelphia 76ers", "Phoenix Suns",
    "Portland Trail Blazers", "Sacramento Kings", "San Antonio Spurs",
    "Toronto Raptors", "Utah Jazz", "Washington Wizards")
)
all_NBA_rate = all_NBA_rate %>% left_join(team_dict)

ggplot(all_NBA_rate, aes(x = year, y = Win_Pct, color = teamname)) +
  geom_line() +
  labs(title = "Teams' Winning Percentage Over 37 Seasons",
       x = "Year", y = "Winning Percentage") +
  # scale_color_teams(1, labels = all_NBA_rate$team) + 
  scale_color_discrete(labels = all_NBA_rate$team, type = league_pal(lg = "nba"))

math-mcshane avatar Dec 04 '23 07:12 math-mcshane

The line colors stayed the same, but the label colors did not, regardless of which of the last two lines I used

math-mcshane avatar Dec 04 '23 07:12 math-mcshane

OK, I'll take a look...in January...

beanumber avatar Dec 07 '23 19:12 beanumber