交互式图形中的订购栏

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

我正在使用ggplot2创建交互式图形,并在R中绘图,代码如下。

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

编辑:我可能没有清楚我想要的内容。当前,得分最高的中场是萨拉(Salah),但我的中场列中的第一行目前是阿里(Alli)。我想对列进行排序,以使值按点的降序排列,而不是字母顺序。

有人可以告诉我该怎么做吗?

我已将完成的图形和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
r ggplot2 plotly r-plotly ggplotly
1个回答
0
投票

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

Libraries:

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

] >>
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

“”

© www.soinside.com 2019 - 2024. All rights reserved.