#r #ggplot2 #geom-bar
#r #ggplot2 #geom-bar
Вопрос:
Воспроизводимый код находится внизу.
Вот посмотрите на данные:
player year player_id position.x player_game_cou~ team_name dropbacks avg_time_to_thr~ avg_time_in_poc~
<chr> <dbl> <dbl> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
1 Phili~ 2015 1725 QB 16 SD 710 2.33 2.4
2 Tom B~ 2015 698 QB 16 NE 668 2.26 2.35
3 Ryan ~ 2015 2467 QB 16 NYJ 615 2.31 2.46
4 Alex ~ 2015 2218 QB 16 KC 572 2.32 2.68
5 Andy ~ 2015 6187 QB 13 CIN 433 2.21 2.41
6 Drew ~ 2016 802 QB 16 NO 697 2.32 2.37
И вот код ggplot:
ggplot(combined.throwing, aes(x = reorder(player, avg_time_to_throw), y = avg_time_to_throw))
geom_bar(fill = combined.throwing$team_color, width = .845)
Проблема в том, что Алекс Смит и Райан Фицпатрик появляются в данных дважды (А. Смит в 2016 и 2015 годах и Фицпатрик в 2015 и 2016 годах).
В конечном счете, я бы хотел, чтобы оба года были отдельными столбцами для каждого квотербека.
Как видно на этом рисунке, ggplot объединяет эти годы …
Вероятно, это простое решение, но я не могу его понять.
Полный воспроизводимый материал здесь:
structure(list(player = c("Philip Rivers", "Tom Brady", "Ryan Fitzpatrick",
"Alex Smith", "Andy Dalton", "Drew Brees", "Alex Smith", "Ryan Fitzpatrick",
"Case Keenum", "Ben Roethlisberger"), year = c(2015, 2015, 2015,
2015, 2015, 2016, 2016, 2016, 2016, 2020), player_id = c(1725,
698, 2467, 2218, 6187, 802, 2218, 2467, 7323, 1732), position.x = c("QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB", "QB"), player_game_count = c(16,
16, 16, 16, 13, 16, 15, 14, 10, 10), team_name = c("SD", "NE",
"NYJ", "KC", "CIN", "NO", "KC", "NYJ", "LA", "PIT"), dropbacks = c(710,
668, 615, 572, 433, 697, 540, 441, 354, 395), avg_time_to_throw = c(2.33,
2.26, 2.31, 2.32, 2.21, 2.32, 2.23, 2.3, 2.28, 2.13), avg_time_in_pocket = c(2.4,
2.35, 2.46, 2.68, 2.41, 2.37, 2.42, 2.41, 2.38, 2.18), avg_time_to_sack = c(2.93,
3.27, 3.19, 3.49, 3.24, 3.25, 3.58, 3.13, 3.2, 3.41), avg_time_to_run = c(5.44,
5.21, 4.55, 5.07, 4.76, 4.73, 5.1, 4.09, 3.84, 5.03), less_dropbacks = c(437,
438, 355, 311, 287, 402, 348, 266, 216, 283), less_percent = c(61.5,
65.6, 57.7, 54.4, 66.3, 57.7, 64.4, 60.3, 61, 71.6), less_sacks = c(9,
7, 3, 3, 4, 8, 3, 5, 6, 0), less_attempts = c(427, 431, 352,
307, 282, 393, 344, 261, 210, 281), less_completions = c(316,
306, 217, 226, 197, 294, 246, 157, 138, 201), less_completion_percent = c(74,
71, 61.6, 73.6, 69.9, 74.8, 71.5, 60.2, 65.7, 71.5), less_touchdowns = c(22,
24, 25, 15, 22, 25, 13, 10, 5, 18), less_interceptions = c(5,
5, 7, 3, 3, 7, 7, 13, 4, 2), less_qb_rating = c(104, 105.6, 95.2,
104.6, 116.3, 107.9, 93.7, 70.5, 80.4, 107.7), more_dropbacks = c(273,
230, 260, 261, 146, 295, 192, 175, 138, 112), more_percent = c(38.5,
34.4, 42.3, 45.6, 33.7, 42.3, 35.6, 39.7, 39, 28.4), more_sacks = c(31,
32, 16, 41, 15, 19, 25, 14, 17, 10), more_attempts = c(233, 191,
210, 163, 104, 273, 144, 142, 112, 99), more_completions = c(121,
96, 118, 81, 58, 177, 82, 71, 58, 54), more_completion_percent = c(51.9,
50.3, 56.2, 49.7, 55.8, 64.8, 56.9, 50, 51.8, 54.5), more_touchdowns = c(7,
12, 6, 5, 3, 12, 2, 2, 4, 6), more_interceptions = c(8, 2, 8,
4, 4, 8, 1, 4, 7, 3), more_qb_rating = c(75.5, 95.5, 75.9, 78,
79, 95.3, 85.9, 68, 68.8, 83.3), team_name.y = c("San Diego Chargers",
"New England Patriots", "New York Jets", "Kansas City Chiefs",
"Cincinnati Bengals", "New Orleans Saints", "Kansas City Chiefs",
"New York Jets", "Los Angeles Rams", "Pittsburgh Steelers"),
team_id = c("4400", "3200", "3430", "2310", "0920", "3300",
"2310", "3430", "2510", "3900"), team_nick = c("Chargers",
"Patriots", "Jets", "Chiefs", "Bengals", "Saints", "Chiefs",
"Jets", "Rams", "Steelers"), team_color = c("#002244", "#002244",
"#203731", "#e31837", "#000000", "#9f8958", "#e31837", "#203731",
"#002244", "#000000"), team_color2 = c("#0073cf", "#c60c30",
"#1c2d25", "#ffb612", "#fb4f14", "#000000", "#ffb612", "#1c2d25",
"#b3995d", "#ffb612"), team_color3 = c("#ffb612", "#b0b7bc",
NA, "#000000", "#000000", "#9f8958", "#000000", NA, "#001532",
"#c60c30"), team_color4 = c("#001532", "#001532", NA, "#e31837",
"#d32f1e", "#000000", "#e31837", NA, "#af925d", "#00539b"
), team_logo_wikipedia = c("https://upload.wikimedia.org/wikipedia/en/thumb/7/72/NFL_Chargers_logo.svg/100px-NFL_Chargers_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/b/b9/New_England_Patriots_logo.svg/100px-New_England_Patriots_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/6/6b/New_York_Jets_logo.svg/100px-New_York_Jets_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/e/e1/Kansas_City_Chiefs_logo.svg/100px-Kansas_City_Chiefs_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Cincinnati_Bengals_logo.svg/100px-Cincinnati_Bengals_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/New_Orleans_Saints_logo.svg/98px-New_Orleans_Saints_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/e/e1/Kansas_City_Chiefs_logo.svg/100px-Kansas_City_Chiefs_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/6/6b/New_York_Jets_logo.svg/100px-New_York_Jets_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/en/thumb/8/8a/Los_Angeles_Rams_logo.svg/100px-Los_Angeles_Rams_logo.svg.png",
"https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/Pittsburgh_Steelers_logo.svg/100px-Pittsburgh_Steelers_logo.svg.png"
), team_logo_espn = c("https://a.espncdn.com/i/teamlogos/nfl/500/lac.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/ne.png", "https://a.espncdn.com/i/teamlogos/nfl/500/nyj.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/kc.png", "https://a.espncdn.com/i/teamlogos/nfl/500/cin.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/no.png", "https://a.espncdn.com/i/teamlogos/nfl/500/kc.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/nyj.png", "https://a.espncdn.com/i/teamlogos/nfl/500/lar.png",
"https://a.espncdn.com/i/teamlogos/nfl/500/pit.png"), team_wordmark = structure(c("https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/LAC_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/NE_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/NYJ_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/KC_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/CIN_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/NO_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/KC_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/NYJ_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/LA_fullcolor.png",
"https://imagecomposer.nfl.com/image/fetch/q_80,h_48,c_fill/http://static.www.nfl.com/league/apps/clubs/wordmarks/PIT_fullcolor.png"
), class = c("glue", "character")), season = c(2020, 2020,
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020), team = c("IND",
"TB", "MIA", "WAS", "DAL", "NO", "WAS", "MIA", "CLE", "PIT"
), position.y = c("QB", "QB", "QB", "QB", "QB", "QB", "QB",
"QB", "QB", "QB"), depth_chart_position = c("QB", "QB", "QB",
"QB", "QB", "QB", "QB", "QB", "QB", "QB"), jersey_number = c(17L,
12L, 14L, 11L, 14L, 9L, 11L, 14L, 5L, 7L), status = c("Active",
"Active", "Active", "Active", "Active", "Injured Reserve",
"Active", "Active", "Active", "Active"), first_name = c("Philip",
"Tom", "Ryan", "Alex", "Andy", "Drew", "Alex", "Ryan", "Case",
"Ben"), last_name = c("Rivers", "Brady", "Fitzpatrick", "Smith",
"Dalton", "Brees", "Smith", "Fitzpatrick", "Keenum", "Roethlisberger"
), birth_date = structure(c(4359, 2771, 4710, 5240, 6510,
3301, 5240, 4710, 6621, 4443), class = "Date"), height = c("6-5",
"6-4", "6-2", "6-4", "6-2", "6-0", "6-4", "6-2", "6-1", "6-5"
), weight = c("228", "225", "228", "213", "220", "209", "213",
"228", "215", "240"), college = c("North Carolina State",
"Michigan", "Harvard", "Utah", "Texas Christian", "Purdue",
"Utah", "Harvard", "Houston", "Miami, O."), high_school = c("Athens (AL)",
"Junipero Serra (CA)", "Highland (AZ)", "Helix (CA)", "Katy (TX)",
"Westlake (TX)", "Helix (CA)", "Highland (AZ)", "Wylie (TX)",
"Findlay (OH)"), gsis_id = c("00-0022942", "00-0019596",
"00-0023682", "00-0023436", "00-0027973", "00-0020531", "00-0023436",
"00-0023682", "00-0028986", "00-0022924"), espn_id = c(5529L,
2330L, 8664L, 8416L, 14012L, 2580L, 8416L, 8664L, 15168L,
5536L), sportradar_id = c("e47706c7-e14d-41fb-b13b-83a835a1f3bc",
"41c44740-d0f6-44ab-8347-3b5d515e5ecf", "0742d2ea-1cf2-49a6-a150-77ba6e034d8c",
"2fda010a-8c62-4c07-b601-4ba03f57e6af", "d2a0e5af-3850-4f16-8e40-a0b1d15c2ce1",
"bb5957e6-ce7d-47ab-8036-22191ffc1c44", "2fda010a-8c62-4c07-b601-4ba03f57e6af",
"0742d2ea-1cf2-49a6-a150-77ba6e034d8c", "1b3d350a-478b-4542-a430-d12cc96adc22",
"ea357add-1a41-4a8b-8f34-bbfade7f4d98"), yahoo_id = c(6763L,
5228L, 7426L, 7177L, 24822L, 5479L, 7177L, 7426L, 26483L,
6770L), rotowire_id = c(3766L, 1350L, 4385L, 4306L, 7355L,
2178L, 4306L, 4385L, 8065L, 3764L), update_dt = structure(c(1606201834.09852,
1606201834.09852, 1606201834.09852, 1606201834.09852, 1606201834.09852,
1606201834.09852, 1606201834.09852, 1606201834.09852, 1606201834.09852,
1606201834.09852), tzone = "America/New_York", class = c("POSIXct",
"POSIXt")), headshot_url = c("https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/5529.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2330.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8664.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8416.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/14012.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/2580.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8416.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8664.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/15168.png",
"https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/5536.png"
)), row.names = c(NA, -10L), spec = structure(list(cols = list(
player = structure(list(), class = c("collector_character",
"collector")), year = structure(list(), class = c("collector_double",
"collector")), player_id = structure(list(), class = c("collector_double",
"collector")), position = structure(list(), class = c("collector_character",
"collector")), player_game_count = structure(list(), class = c("collector_double",
"collector")), team_name = structure(list(), class = c("collector_character",
"collector")), dropbacks = structure(list(), class = c("collector_double",
"collector")), avg_time_to_throw = structure(list(), class = c("collector_double",
"collector")), avg_time_in_pocket = structure(list(), class = c("collector_double",
"collector")), avg_time_to_sack = structure(list(), class = c("collector_double",
"collector")), avg_time_to_run = structure(list(), class = c("collector_double",
"collector")), less_dropbacks = structure(list(), class = c("collector_double",
"collector")), less_percent = structure(list(), class = c("collector_double",
"collector")), less_sacks = structure(list(), class = c("collector_double",
"collector")), less_attempts = structure(list(), class = c("collector_double",
"collector")), less_completions = structure(list(), class = c("collector_double",
"collector")), less_completion_percent = structure(list(), class = c("collector_double",
"collector")), less_touchdowns = structure(list(), class = c("collector_double",
"collector")), less_interceptions = structure(list(), class = c("collector_double",
"collector")), less_qb_rating = structure(list(), class = c("collector_double",
"collector")), more_dropbacks = structure(list(), class = c("collector_double",
"collector")), more_percent = structure(list(), class = c("collector_double",
"collector")), more_sacks = structure(list(), class = c("collector_double",
"collector")), more_attempts = structure(list(), class = c("collector_double",
"collector")), more_completions = structure(list(), class = c("collector_double",
"collector")), more_completion_percent = structure(list(), class = c("collector_double",
"collector")), more_touchdowns = structure(list(), class = c("collector_double",
"collector")), more_interceptions = structure(list(), class = c("collector_double",
"collector")), more_qb_rating = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
Комментарии:
1. Возможно, использовать
unite()
для объединения игрока и года в одной переменной:combined.throwing %>% unite(player, c(player, year), sep = " - ") %>% ggplot(x = reorder(player, avg_time_to_throw), y = avg_time_to_throw)) ...
2. Спасибо, Фил! Это сработало, и, кроме того, я узнал о «объединении!»