如何使用具有2个变量和3个子变量的ggplot创建条形图

问题描述 投票:0回答:1

迫切希望获得帮助。

原始数据来自https://www.hockey-reference.com/play-index/tiny.fcgi?id=mmDlH

看起来像这样:csv file

# A tibble: 6 x 19
  match_no Date  Tm    Opp   Outcome Time      G    PP    SH     S   PIM    GA  PPGA  SHGA
     <dbl> <chr> <chr> <chr> <chr>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1        1 6/4/… NYI   WSH   W       REG       3     0     0    24     4     0     0     0
2        2 6/4/… WSH   NYI   L       REG       0     0     0    29     2     3     0     0
3        3 6/4/… STL   VAN   W       SO        3     1     0    36     6     2     2     0
4        4 6/4/… VAN   STL   L       SO        2     2     0    25     6     3     1     0
5        5 6/4/… COL   SJS   L       REG       2     0     0    30     4     5     0     0
6        6 6/4/… SJS   COL   W       REG       5     0     0    30     4     2     0     0
# … with 5 more variables: PPO <dbl>, PPOA <dbl>, SA <dbl>, OppPIM <dbl>, DIFF <dbl>

并且我可以转换为this

A tibble: 6 x 5
# Groups:   Tm [1]
  Tm    Outcome Time      n  prob
  <chr> <chr>   <chr> <int> <dbl>
1 ANA   L       OT        7  0.09
2 ANA   L       REG      37  0.45
3 ANA   L       SO        3  0.04
4 ANA   W       OT        5  0.06
5 ANA   W       REG      27  0.33
6 ANA   W       SO        3  0.04

我用过这个

team_outcomes_regulation <-
df %>%
+ count(Tm,Outcome, Time) %>%
+ group_by(Tm) %>%
+ mutate(prob = round(prop.table(n), 2))

然后我尝试使用ggplot

team_outcomes_regulation %>%
ggplot(aes(x = Tm, y = prob, fill = Time)) 
+ geom_bar(position = "fill",stat = "identity")
+ theme(axis.text.x = element_text(angle = 90))

[And this is what I get,但我迫切希望将图表与6个总数分开(SO,Reg&OT获胜,SO,Reg&OT获胜)] 3

我现在想尝试使用原始df比较获胜与目标差异。

 # A tibble: 6 x 19
      match_no Date  Tm    Opp   Outcome Time      G    PP    SH     S   PIM    GA  PPGA  SHGA
         <dbl> <chr> <chr> <chr> <chr>   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
    1        1 6/4/… NYI   WSH   W       REG       3     0     0    24     4     0     0     0
    2        2 6/4/… WSH   NYI   L       REG       0     0     0    29     2     3     0     0
    3        3 6/4/… STL   VAN   W       SO        3     1     0    36     6     2     2     0
    4        4 6/4/… VAN   STL   L       SO        2     2     0    25     6     3     1     0
    5        5 6/4/… COL   SJS   L       REG       2     0     0    30     4     5     0     0
    6        6 6/4/… SJS   COL   W       REG       5     0     0    30     4     2     0     0
    # … with 5 more variables: PPO <dbl>, PPOA <dbl>, SA <dbl>, OppPIM <dbl>, DIFF <dbl>

所以我现在要提取:31支球队(Tm),获胜次数(结果)和目标差(DIFF的总和),请提供进一步的帮助?

ggplot2 geom-bar
1个回答
0
投票

您快到了,因为您已经在“时间”列中列出的那些值之间作了划分。如果要绘制“时间”和“结果”列的所有排列,则意味着您需要将这些值combine合并到一列中并绘制相同的图形。这里有一些选项,但也许最简单的方法如下:

team_outcomes_regulation$outcome_time <-
    paste(team_outcomes_regulation$Outcome, "by", team_outcomes_regulation$Time)

然后您的情节变成:

team_outcomes_regulation %>%
    ggplot(aes(x = Tm, y = prob, fill = outcome_time)) +
    geom_bar(position = "fill",stat = "identity") +
    theme(axis.text.x = element_text(angle = 90))
© www.soinside.com 2019 - 2024. All rights reserved.