溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜

發(fā)布時間:2021-11-25 09:34:25 來源:億速云 閱讀:210 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜”,在日常操作中,相信很多人在R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

import math
import requests
from bs4 import BeautifulSoup

headers = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}

fw = open('NBA_salary_3.txt','w')
fw.write('%s\t%s\t%s\t%s\t%s\t%s\n'%("year","rk","player","position","team","salary"))

years = list(range(2000,2021))


for year in years:
   URL = "http://www.espn.com/nba/salaries/_/year/" + str(year)
   print(URL)
   page = requests.get(URL,headers=headers)
   soup = BeautifulSoup(page.content,'html.parser')
   
   df = soup.find_all('tr')
   j = 0
   for aa in df:
       j = j + 1
       if j == 1:
           continue
       elif j > 1 and j < 12:
           all_td = aa.find_all('td')
           rk = all_td[0].get_text()
           player_position = all_td[1].get_text()
           #print(player_position)
           player = player_position.split(",")[0]
           position = player_position.split(",")[1]
           team = all_td[2].get_text()
           salary = all_td[3].get_text().replace("$","").replace(",","")
           
           fw.write('%s\t%s\t%s\t%s\t%s\t%s\n'%(str(year),rk,player,position,team,salary))
       
       else:
           break
           

fw.close()
   首先來看看2020年薪資排行榜top10
library(ggplot2)
ggplot(df1,aes(x=rk,y=salary))+
 geom_col(aes(fill=player))+
 geom_label(aes(label=paste0(salary,"萬"),hjust=1))+
 theme_bw()+
 scale_x_continuous(breaks = 1:10,labels = df1$player)+
 coord_flip()+labs(x="",y="")+
 theme(legend.position = "none")
 
R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜  
image.png
 接下來制作動態(tài)的柱形圖

使用的工具是R語言的gganimate包 


用到的代碼是


df<-read.csv("NBA_salary_3.txt",header=T,sep="\t",stringsAsFactors = F)
df$salary<-df$salary/10000
head(df)

library(tidyquant)
x1<-palette_dark()
colors<-matrix(x1)[,1][1:10]
colors<-sample(colors,69,replace = T)
colors

library(ggplot2)
staticplot = ggplot(df, aes(rk, group = player,
                           fill = as.factor(player),
                           color = as.factor(player))) +
 geom_tile(aes(y = salary/2,
               height = salary,
               width = 0.9), alpha = 0.8, color = NA) +
 geom_text(aes(y = 0, label = paste(player, " ")), vjust = 0.2,
           hjust = 1,color="black") +
 geom_text(aes(y=salary,label = salary, hjust=0),color="black") +
 scale_fill_manual(values = colors)+
 coord_flip(clip = "off", expand = FALSE) +
 scale_y_continuous(labels = scales::comma) +
 scale_x_reverse() +
 guides(color = FALSE, fill = FALSE) +
 theme(axis.line=element_blank(),
       axis.text.x=element_blank(),
       axis.text.y=element_blank(),
       axis.ticks=element_blank(),
       axis.title.x=element_blank(),
       axis.title.y=element_blank(),
       legend.position="none",
       panel.background=element_blank(),
       panel.border=element_blank(),
       panel.grid.major=element_blank(),
       panel.grid.minor=element_blank(),
       panel.grid.major.x = element_line( size=.1, color="grey" ),
       panel.grid.minor.x = element_line( size=.1, color="grey" ),
       plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
       plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
       plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
       plot.background=element_blank(),
       plot.margin = margin(2,2, 2, 4, "cm"))

staticplot
library(gganimate)
help(package="gganimate")
anim = staticplot + transition_states(year,
                                     transition_length = 4,
                                     state_length = 1) +
 view_follow(fixed_x = TRUE)  +
 labs(title = 'NBA player Salary : {closest_state}',
      subtitle  =  "Top 10 Players",
      caption  = "Data Source: http://www.espn.com/nba/salaries")



animate(anim, 300, fps = 10, duration = 20,
       width = 1200, height = 1000,
       renderer = ffmpeg_renderer()) -> for_mp4

anim_save("animation_6.mp4", animation = for_mp4 )

到此,關于“R數(shù)據(jù)可視化怎么實現(xiàn)NBA球員薪水排行榜”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI