Home > Software design >  ggplot2 sorting but not displaying correctly
ggplot2 sorting but not displaying correctly

Time:09-23

I have made a data frame consisting of NFL stats. This data frame contains team name, how many yards they have allowed, if this was passing or rushing, and the total of the yards. I want to create a stacked bar chart with the x axis ordered in ascending order. I have used reorder() but for some reason the Panthers values are flipped and appear much higher than the rest (even though they have the lowest total). I have made other graphs this same way with no problems. I tried manually setting the y axis limit but that did not solve my problem. My question is why is this happening and what have I missed?

I am also open on ways to make this more efficient.

library(rvest)
library(data.table)
library(ggplot2)

#Read and organise data
def = read_html("https://www.pro-football-reference.com/years/2021/opp.htm")
defense = html_table(def)
defense = defense[[1]]
colnames(defense) = defense[1,]
defense = defense[c(-1,-34,-35,-36),]
names = c("rank", "team", "games", "points_against", "yards_conceded", "off_plays_faced", 
          "yards_per_off_play", "TO", "fumbles_recovered", "1stD_faced", "pass_cmp", 
          "pass_att", "pass_yd_conceded", "pass_td_conceded", "int_recovered", 
          "net_yd_pass_att", "pass_1stD_against", "rush_att", "rush_yards_conceded", 
          "rush_td_conceded", "rush_yards_per_att", "rush_1stD_conceded", "pen_against", 
          "pen_yards_conceded", "pen_1stD", "off_score_percent", "turnover_percent", 
          "expected_points_conceded")
colnames(defense) = names

# Get passing and rushing totals
passing_def = data.table(defense$team, defense$pass_yd_conceded, rep("pass", 32))
rushing_def = data.table(defense$team, defense$rush_yards_conceded, rep("rush", 32))
sums = rep(rowSums(cbind(as.numeric(passing_def$V2), as.numeric(rushing_def$V2))),2)

# join them and add the total yards
yds_conc = rbind(passing_def, rushing_def) 
yds_conc = data.table(yds_conc, sums)
colnames(yds_conc) = c("team", "conceded", "type", "sums")

# Create the plot so that the teams are sorted from left to right by total yards given up
defplot = ggplot(data = yds_conc, aes(fill = type, x = reorder(team, sums),
                                      y = conceded))  
  geom_bar(stat = 'identity')   
  scale_fill_manual(name = "Play Type",labels=c("Passing","Rushing"), 
                    values = c("#006aff", "#4fb350"))  
  ggtitle('2021 Season Defense: Total Yards Conceded per Play Type')   ylab("Avg Rank")   
  theme(axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 60, vjust = 1, hjust=1), 
        axis.ticks.y = element_blank(), axis.text.y = element_blank())
defplot```

CodePudding user response:

add this

yds_conc$conceded=as.numeric(yds_conc$conceded)

then run the same script will work

enter image description here

  • Related