GGplot Plotting Each Point Twice

Viewed 74

I am trying to make an animated bubble chart for a baseball league I'm in. Once I create the animated graph and convert it into a gif, it plots each team twice, as shown in the picture below. The legend should only hold 14 points/teams, but it shows 28 instead.

My code is the following:

library(ggplot2)
library(gganimate)
library(readxl)
library(gifski)
library(png) 

myData <- read_excel("~/Desktop/Dynasty - Fantasy Baseball.xlsx")

# Make a ggplot, but add frame=year: one image per year
g <- ggplot(myData, aes(PF, PA, size = `W%`, color = Team)) +
  geom_point() +
  theme_bw() +
  # gganimate specific bits:
  labs(title = 'Period: {frame_time-1900}', x = 'Points For', y = 'Points Against') +
  transition_time(Year) +
  ease_aes('linear')


# Save at gif:
anim_save(filename = "~/Desktop/FantasyBaseballAnimated.gif", animation = g)

My data is stored in the following:

structure(list(Team = c("Houston Astros", "Miami Marlins", "New York Mets", 
"Atlanta Braves", "St. Louis Cardinals", "Cincinatti Reds", "Philadelphia Reds", 
"Baltimore Orioles", "Milwaukee Brewers", "Washington Nationals", 
"Montreal Expos", "Tampa Bay Rays", "Seattle Mariners", "Brooklyn Dodgers", 
"Houston Astros", "Miami Marlins", "New York Mets", "Atlanta Braves", 
"St. Louis Cardinals", "Cincinatti Reds", "Philadelphia Reds", 
"Baltimore Orioles", "Milwaukee Brewers", "Washington Nationals", 
"Montreal Expos", "Tampa Bay Rays", "Seattle Mariners", "Brooklyn Dodgers", 
"New York Mets ", "St. Louis Cardinals ", "Cincinatti Reds ", 
"Washington Nationals ", "Atlanta Braves ", "Miami Marlins ", 
"Philadelphia Phillies ", "Tampa Bay Rays ", "Houston Astros ", 
"Montreal Expos ", "Baltimore Orioles ", "Milwaukee Brewers ", 
"Seattle Mariners ", "Brooklyn Dodgers ", "St. Louis Cardinals ", 
"Washington Nationals ", "Miami Marlins ", "Cincinatti Reds ", 
"New York Mets ", "Atlanta Braves ", "Tampa Bay Rays ", "Houston Astros ", 
"Milwaukee Brewers ", "Philadelphia Phillies ", "Baltimore Orioles ", 
"Montreal Expos ", "Seattle Mariners ", "Brooklyn Dodgers ", 
"Washington Nationals ", "St. Louis Cardinals ", "Atlanta Braves ", 
"Cincinatti Reds ", "New York Mets ", "Houston Astros ", "Miami Marlins ", 
"Philadelphia Phillies ", "Tampa Bay Rays ", "Milwaukee Brewers ", 
"Baltimore Orioles ", "Montreal Expos ", "Seattle Mariners ", 
"Brooklyn Dodgers ", "St. Louis Cardinals ", "Washington Nationals ", 
"Philadelphia Phillies ", "Miami Marlins ", "Atlanta Braves ", 
"New York Mets ", "Houston Astros ", "Milwaukee Brewers ", 
"Cincinatti Reds ", "Tampa Bay Rays ", "Montreal Expos ", 
"Baltimore Orioles ", "Seattle Mariners ", "Brooklyn Dodgers ", 
"New York Mets ", "St. Louis Cardinals ", "Washington Nationals ", 
"Philadelphia Phillies ", "Miami Marlins ", "Houston Astros ", 
"Atlanta Braves ", "Milwaukee Brewers ", "Cincinatti Reds ", 
"Tampa Bay Rays ", "Montreal Expos ", "Baltimore Orioles ", 
"Seattle Mariners ", "Brooklyn Dodgers ", "St. Louis Cardinals ", 
"Washington Nationals ", "Houston Astros ", "New York Mets ", 
"Philadelphia Phillies ", "Milwaukee Brewers ", "Atlanta Braves ", 
"Miami Marlins ", "Cincinatti Reds ", "Tampa Bay Rays ", "Baltimore Orioles ", 
"Montreal Expos ", "Seattle Mariners ", "Brooklyn Dodgers "
), W = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 9, 8, 
7, 6, 6, 5, 6, 5, 4, 3, 2, 2, 2, 17, 17, 16, 14, 14, 14, 12, 
11, 13, 7, 7, 6, 3, 3, 25, 24, 22, 21, 20, 20, 18, 19, 16, 14, 
12, 9, 8, 5, 33, 32, 27, 27, 25, 26, 25, 23, 21, 21, 16, 15, 
11, 7, 37, 37, 35, 34, 33, 32, 32, 29, 29, 27, 21, 19, 17, 7, 
44, 43, 43, 40, 38, 40, 37, 37, 35, 32, 25, 23, 20, 7, 52, 50, 
50, 48, 48, 43, 42, 40, 41, 38, 34, 28, 25, 8), L = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, 4, 6, 5, 6, 5, 6, 
7, 8, 9, 10, 5, 5, 7, 7, 8, 9, 9, 9, 11, 14, 15, 15, 19, 21, 
8, 9, 11, 13, 13, 13, 14, 16, 17, 19, 21, 22, 26, 31, 11, 12, 
16, 19, 18, 19, 20, 22, 21, 22, 28, 28, 33, 40, 18, 18, 22, 22, 
22, 22, 25, 25, 28, 27, 34, 36, 38, 52, 22, 22, 22, 28, 27, 29, 
28, 28, 33, 31, 42, 42, 46, 64, 25, 27, 31, 30, 32, 33, 34, 37, 
39, 37, 43, 51, 53, 75), T = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 2, 2, 2, 0, 2, 0, 2, 2, 2, 2, 1, 0, 2, 2, 1, 
3, 2, 1, 3, 4, 0, 3, 2, 3, 2, 0, 3, 3, 3, 2, 3, 3, 4, 1, 3, 3, 
3, 5, 2, 0, 4, 4, 5, 2, 5, 3, 3, 3, 6, 5, 4, 5, 4, 1, 5, 5, 3, 
4, 5, 6, 3, 6, 3, 6, 5, 5, 5, 1, 6, 7, 7, 4, 7, 3, 7, 7, 4, 9, 
5, 7, 6, 1, 7, 7, 3, 6, 4, 8, 8, 7, 4, 9, 7, 5, 6, 1), `W%` = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.833, 0.792, 0.75, 0.667, 
0.583, 0.5, 0.5, 0.5, 0.5, 0.417, 0.333, 0.25, 0.208, 0.167, 
0.75, 0.75, 0.688, 0.646, 0.625, 0.604, 0.562, 0.542, 0.542, 
0.354, 0.333, 0.312, 0.167, 0.125, 0.736, 0.708, 0.653, 0.611, 
0.597, 0.597, 0.556, 0.542, 0.486, 0.431, 0.375, 0.319, 0.25, 
0.139, 0.729, 0.708, 0.615, 0.583, 0.573, 0.573, 0.552, 0.51, 
0.5, 0.49, 0.375, 0.365, 0.271, 0.156, 0.658, 0.658, 0.608, 0.6, 
0.592, 0.583, 0.558, 0.533, 0.508, 0.5, 0.392, 0.358, 0.325, 
0.125, 0.653, 0.646, 0.646, 0.583, 0.576, 0.576, 0.562, 0.562, 
0.514, 0.507, 0.382, 0.368, 0.319, 0.104, 0.661, 0.637, 0.613, 
0.607, 0.595, 0.56, 0.548, 0.518, 0.512, 0.506, 0.446, 0.363, 
0.333, 0.101), `Div Rec` = c("0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0-0-0", "0-0-0", "37470", 
"0-0-0", "0-0-0", "36683", "0-0-0", "36683", "0-0-0", "0-0-0", 
"0-0-0", "37295", "0-0-0", "0-0-0", "17-5-2", "0-0-0", "36683", 
"0-0-0", "36712", "36653", "0-0-0", "37295", "36594", "0-0-0", 
"36683", "0-0-0", "0-0-0", "0-0-0", "37106", "36801", "36653", 
"37207", "20-13-3", "13-10-1", "37512", "36594", "0-0-0", "36566", 
"36683", "0-0-0", "36653", "0-0-0", "19-4-1", "37106", "13-10-1", 
"37207", "25-18-5", "37541", "36754", "36843", "37512", "37381", 
"36683", "0-0-0", "37482", "36931", "13-9-2", "19-4-1", "23-13-0", 
"17-18-1", "13-10-1", "25-18-5", "37541", "37381", "13-21-2", 
"15-19-2", "36683", "36683", "14-19-3", "36943", "25-18-5", "13-9-2", 
"25-8-3", "28-19-1", "17-18-1", "18-16-2", "13-10-1", "13-8-3", 
"19-26-3", "15-19-2", "36813", "37541", "17-27-4", "36943", "22-12-2", 
"25-8-3", "18-16-2", "25-18-5", "28-19-1", "13-8-3", "13-10-1", 
"17-18-1", "19-26-3", "15-19-2", "21-13-2", "13-23-0", "17-27-4", 
"3-32-1"), GB = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0.5, 1, 2, 3, 4, 4, 4, 4, 5, 6, 7, 7.5, 8, 0, 0, 1.5, 2.5, 3, 
3.5, 4.5, 5, 5, 9.5, 10, 10.5, 14, 15, 0, 1, 3, 4.5, 5, 5, 6.5, 
7, 9, 11, 13, 15, 17.5, 21.5, 0, 1, 5.5, 7, 7.5, 7.5, 8.5, 10.5, 
11, 11.5, 17, 17.5, 22, 27.5, 0, 0, 3, 3.5, 4, 4.5, 6, 7.5, 9, 
9.5, 16, 18, 20, 32, 0, 0.5, 0.5, 5, 5.5, 5.5, 6.5, 6.5, 10, 
10.5, 19.5, 20.5, 24, 39.5, 0, 2, 4, 4.5, 5.5, 8.5, 9.5, 12, 
12.5, 13, 18, 25, 27.5, 47), PF = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 10, 9.5, 9, 8, 7, 6, 6, 6, 6, 5, 4, 3, 2.5, 2, 
18, 18, 16.5, 15.5, 15, 14.5, 13.5, 13, 13, 8.5, 8, 7.5, 4, 3, 
26.5, 25.5, 23.5, 22, 21.5, 21.5, 20, 19.5, 17.5, 15.5, 13.5, 
11.5, 9, 5, 35, 34, 29.5, 28, 27.5, 27.5, 26.5, 24.5, 24, 23.5, 
18, 17.5, 13, 7.5, 39.5, 39.5, 36.5, 36, 35.5, 35, 33.5, 32, 
30.5, 30, 23.5, 21.5, 19.5, 7.5, 47, 46.5, 46.5, 42, 41.5, 41.5, 
40.5, 40.5, 37, 36.5, 27.5, 26.5, 23, 7.5, 55.5, 53.5, 51.5, 
51, 50, 47, 46, 43.5, 43, 42.5, 37.5, 30.5, 28, 8.5), PA = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2.5, 3, 4, 5, 6, 6, 
6, 6, 7, 8, 9, 9.5, 10, 6, 6, 7.5, 8.5, 9, 9.5, 10.5, 11, 11, 
15.5, 16, 16.5, 20, 21, 9.5, 10.5, 12.5, 14, 14.5, 14.5, 16, 
16.5, 18.5, 20.5, 22.5, 24.5, 27, 31, 13, 14, 18.5, 20, 20.5, 
20.5, 21.5, 23.5, 24, 24.5, 30, 30.5, 35, 40.5, 20.5, 20.5, 23.5, 
24, 24.5, 25, 26.5, 28, 29.5, 30, 36.5, 38.5, 40.5, 52.5, 25, 
25.5, 25.5, 30, 30.5, 30.5, 31.5, 31.5, 35, 35.5, 44.5, 45.5, 
49, 64.5, 28.5, 30.5, 32.5, 33, 34, 37, 38, 40.5, 41, 41.5, 46.5, 
53.5, 56, 75.5), Period = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7), Place = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), Year = c(1900, 
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 
1900, 1900, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 1901, 
1901, 1901, 1901, 1901, 1901, 1902, 1902, 1902, 1902, 1902, 1902, 
1902, 1902, 1902, 1902, 1902, 1902, 1902, 1902, 1903, 1903, 1903, 
1903, 1903, 1903, 1903, 1903, 1903, 1903, 1903, 1903, 1903, 1903, 
1904, 1904, 1904, 1904, 1904, 1904, 1904, 1904, 1904, 1904, 1904, 
1904, 1904, 1904, 1905, 1905, 1905, 1905, 1905, 1905, 1905, 1905, 
1905, 1905, 1905, 1905, 1905, 1905, 1906, 1906, 1906, 1906, 1906, 
1906, 1906, 1906, 1906, 1906, 1906, 1906, 1906, 1906, 1907, 1907, 
1907, 1907, 1907, 1907, 1907, 1907, 1907, 1907, 1907, 1907, 1907, 
1907)), row.names = c(NA, -112L), class = c("tbl_df", "tbl", 
"data.frame"))

I thought factoring it would work, and also parsing it but neither worked:

#first thought
myData$Team <- factor(myData$Team)
summary(myData)
#second thought
myData$Team <- eval(parse(text = myData$Team))

Am I just missing something obvious? I'm drawing a blank at how I could fix this. Any help would be greatly appreciated!

2 Answers

Answer

Remove the whitespace around the names:

myData$Team <- trimws(myData$Team)

Rationale

You actually have each team in there twice. Half just contain a single space at the end of their name. You may want to look into WHY that is happening.

table(myData$Team, myData$Year)[1:2, ]
#                  1900 1901 1902 1903 1904 1905 1906 1907
#  Atlanta Braves     1    1    0    0    0    0    0    0
#  Atlanta Braves     0    0    1    1    1    1    1    1

sort(unique(myData$Team))[1:2]
#[1] "Atlanta Braves"  "Atlanta Braves "

It looks like you need to do some data cleaning:

data %>%  group_by(Team) %>% 
summarise(count = n())

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 28 x 2
   Team                 count
   <chr>                <int>
 1 "Atlanta Braves"         2
 2 "Atlanta Braves "        6
 3 "Baltimore Orioles"      2
 4 "Baltimore Orioles "     6
 5 "Brooklyn Dodgers"       2
 6 "Brooklyn Dodgers "      6
 7 "Cincinatti Reds"        2
 8 "Cincinatti Reds "       6
 9 "Houston Astros"         2
10 "Houston Astros "        6
# ... with 18 more rows

Using stringr:

data <-  data %>% 
   mutate(Team = str_trim(Team, side = c("both")))
Related