Ian Murray
Ian Murray

Reputation: 87

Order Bars in interactive graph

I am creating an interactive graph using ggplot2 and plotly in R, the code is below.

I want to reorder the barchart column values so that they are sorted in descending order, currently they are sorted alphabetically.

Edit: I might not have made what I wanted clear. Currently, the midfielder with the most points is Salah, but the top row in my midfielder column is currently Alli. I would like to sort the column so that the values are in descending order of points rather than alphabetical.

Would someone please inform me how I can do this?

I have saved the finished graph & csv file at the below locations:

IG: https://ianfm94.github.io/Premier_League_Stats/Top_100_Fantasy_PL_Pointscorers.html

CSV File: https://github.com/Ianfm94/Premier_League_Stats/blob/master/CSV_Files/2020-06-01_updated_fpl_stats.csv

rm(list=ls())

# Required packages, you might need to install these
library(ggplot2)
library(dplyr)
library(plotly)
library(tibble)

## Fantasy_PL Data 

fpl_data = read.csv('2020-06-01_updated_fpl_stats.csv',
                header = T, fileEncoding = "UTF-8-BOM")
attach(fpl_data)
#View(fpl_data)

# Interactive Plot Workings

top_100_points = total_points[0:100]
top_100_player_pos = factor(player_pos)[0:100]
top_100_surnames = factor(web_name)[0:100]
top_100_team = factor(team_name)[0:100]

color_table = tibble(
  Team_Name = c("Arsenal", "Aston Villa", "Bournemouth", "Brighton & Hove Albion",
                "Burnley", "Chelsea", "Crystal Palace", "Everton", 
                "Leicester City", "Liverpool", "Manchester City",
                "Manchester United", "Newcastle United", "Norwich City",
                "Sheffield United", "Southampton", "Tottenham Hotspurs",
                "Watford", "West Ham United", "Wolverhampton Wanderers"),
  Team_Color = c("#EF0107", "#670E36", "#B50E12", "#0057B8",
                 "#6C1D45", "#034694", "#1B458F", "#003399",
                 "#003090", "#C8102E", "#6CABDD", "#DA291C",
                 "#241F20", "#FFF200", "#EE2737", "#D71920",
                 "#132257", "#FBEE23", "#7A263A", "#FDB913")
)

position_table = tibble(
  Position_Name = c("Goalkeeper", "Defender", "Midfielder", "Striker"),
)

fpl_df = data.frame(y = top_100_points, 
                    x = top_100_player_pos, 
                    z = top_100_surnames,
                    w = top_100_team,
                    stringsAsFactors = F)

fpl_df$w = factor(fpl_df$w, levels = color_table$Team_Name)
fpl_df$x = factor(fpl_df$x, levels = position_table$Position_Name)

names(fpl_df)[names(fpl_df) == "x"] = "Position_Name"
names(fpl_df)[names(fpl_df) == "y"] = "Total_Points_by_Position"
names(fpl_df)[names(fpl_df) == "z"] = "Player_Surname"
names(fpl_df)[names(fpl_df) == "w"] = "Team_Name"
#View(fpl_df)

plot_fpl_1 = ggplot(fpl_df, aes(x = Position_Name,
                                y = Total_Points_by_Position,
                                z = Player_Surname,
                                fill = Team_Name)) +
  geom_col() +
  scale_fill_manual(values = color_table$Team_Color) + 
  labs(title = "Top 100 Fantasy PL Pointscorer by Position & Team",
       y = "Total Points of Position",
       x = "Player Positions",
       fill = "Team Name") +
  theme_bw() +
  theme(plot.title = element_text(size = 14,
                                  face = "bold",
                                  color = "black"),
        legend.title = element_text(color = "navy",
                                    face = "bold",
                                    size = 10))  
plot_fpl_1 = ggplotly(plot_fpl_1)
plot_fpl_1

Upvotes: 2

Views: 159

Answers (1)

M--
M--

Reputation: 28850

You can use forcats::fct_reorder to change the order of z. See below:

Libraries:

# Required packages, you might need to install these
library(ggplot2)
library(dplyr)
library(plotly)
library(tibble)
library(RCurl)
library(forcats)

Data:

## Fantasy_PL Data 
csvurl <- getURL("https://raw.githubusercontent.com/Ianfm94/Premier_League_Stats/master/CSV_Files/2020-06-01_updated_fpl_stats.csv")
fpl_data  <- read.csv(text = csvurl)
attach(fpl_data)

# Interactive Plot Workings
top_100_points = total_points[0:100]
top_100_player_pos = factor(player_pos)[0:100]
top_100_surnames = factor(web_name)[0:100]
top_100_team = factor(team_name)[0:100]

color_table = tibble(
  Team_Name = c("Arsenal", "Aston Villa", "Bournemouth", "Brighton & Hove Albion",
                "Burnley", "Chelsea", "Crystal Palace", "Everton", 
                "Leicester City", "Liverpool", "Manchester City",
                "Manchester United", "Newcastle United", "Norwich City",
                "Sheffield United", "Southampton", "Tottenham Hotspurs",
                "Watford", "West Ham United", "Wolverhampton Wanderers"),
  Team_Color = c("#EF0107", "#670E36", "#B50E12", "#0057B8",
                 "#6C1D45", "#034694", "#1B458F", "#003399",
                 "#003090", "#C8102E", "#6CABDD", "#DA291C",
                 "#241F20", "#FFF200", "#EE2737", "#D71920",
                 "#132257", "#FBEE23", "#7A263A", "#FDB913")
)

position_table = tibble(
  Position_Name = c("Goalkeeper", "Defender", "Midfielder", "Striker"),
)

fpl_df = data.frame(y = top_100_points, 
                    x = top_100_player_pos, 
                    z = top_100_surnames,
                    w = top_100_team,
                    stringsAsFactors = F)

fpl_df$w = factor(fpl_df$w, levels = color_table$Team_Name)
fpl_df$x = factor(fpl_df$x, levels = position_table$Position_Name)

names(fpl_df)[names(fpl_df) == "x"] = "Position_Name"
names(fpl_df)[names(fpl_df) == "y"] = "Total_Points_by_Position"
names(fpl_df)[names(fpl_df) == "z"] = "Player_Surname"
names(fpl_df)[names(fpl_df) == "w"] = "Team_Name"

Plot:

plot_fpl_1 = ggplot(fpl_df, aes(x = Position_Name,
                                y = Total_Points_by_Position,
                                z = fct_reorder(Player_Surname, -Total_Points_by_Position),
                                fill = Team_Name)) +
  geom_col() +
  scale_fill_manual(values = color_table$Team_Color) + 
  labs(title = "Top 100 Fantasy PL Pointscorer by Position & Team",
       y = "Total Points of Position",
       x = "Player Positions",
       fill = "Team Name") +
  theme_bw() +
  theme(plot.title = element_text(size = 14,
                                  face = "bold",
                                  color = "black"),
        legend.title = element_text(color = "navy",
                                    face = "bold",
                                    size = 10))  

plot_fpl_2 = ggplotly(plot_fpl_1)
plot_fpl_2

Upvotes: 4

Related Questions