交互式图表中的 Order Bars

Order Bars in interactive graph

我在 R 中使用 ggplot2 和 plotly 创建一个交互式图表,代码如下。

我想对条形图列值重新排序,以便它们按降序排列, 目前它们按字母顺序排序。

编辑:我可能没有说清楚我想表达的意思。目前,得分最多的中场球员是萨拉赫,但我的中场球员栏目中目前排在第一位的是阿里。我想对列进行排序,以便值按点的降序排列而不是按字母顺序排列。

有人能告诉我该怎么做吗?

我已将完成的图形和 csv 文件保存在以下位置:

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

CSV 文件: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

您可以使用forcats::fct_reorder更改z的顺序。见下文:

图书馆:

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

数据:

## 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_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