r语言绘制雷达图_用r绘制雷达蜘蛛图
r語言繪制雷達圖
I’ve tried several different types of NBA analytical articles within my readership who are a group of true fans of basketball. I found that the most popular articles are not those with state-of-the-art machine learning technologies, but those with straightforward and meaningful graphs.
我在讀者群中嘗試了幾種不同類型的NBA分析文章,這些文章是一群真正的籃球迷。 我發(fā)現(xiàn)最受歡迎的文章不是那些具有最新的機器學習技術(shù)的文章,而是那些具有簡單明了的圖表的文章。
At a certain stage of my career as a data scientist, I realized that delivering the information is more important than showing the fancy models. Perhaps that’s why linear regression is still one of the most popular models in the finance world.
在我作為數(shù)據(jù)科學家的職業(yè)生涯的某個階段,我意識到提供信息比展示精美的模型更為重要。 也許這就是為什么線性回歸仍然是金融界最受歡迎的模型之一的原因。
In this post, I am going to talk about a simple topic. It is how to draw the spider plot, or the radar plot, which is one of the most essential graphs in a comparative analysis. I am implementing the code in R.
在這篇文章中,我將討論一個簡單的話題。 這是繪制蜘蛛圖或雷達圖的方法 ,它是比較分析中最重要的圖之一。 我正在R中實現(xiàn)代碼。
數(shù)據(jù) (Data)
NBA players’ basic statistics and advanced statistics per game in 2019–2020 NBA playoffs. (from basketball reference)
在2019–2020 NBA季后賽中,NBA球員的每場比賽的基本統(tǒng)計數(shù)據(jù)和高級統(tǒng)計數(shù)據(jù)。 ( 參考籃球 )
碼 (Code)
Let’s first visualize James Harden’s stats in a spider plot. We only focus on five stats: PTS (points), TRB (total rebounds), AST (assists), STL (steals), and BLK (blocks).
讓我們首先在蜘蛛圖中形象化詹姆斯·哈登的統(tǒng)計數(shù)據(jù)。 我們僅關(guān)注五項統(tǒng)計數(shù)據(jù):PTS(得分),TRB(總籃板),AST(助攻),STL(搶斷)和BLK(蓋帽)。
df = read.csv("playoff_stats.csv")maxxx = apply(df[,c("PTS.","TRB","AST","STL","BLK")],2,max)
minnn = apply(df[,c("PTS.","TRB","AST","STL","BLK")],2,min)
In the code of this block, the data was read to a data frame, “df”. And the maximum and minimum values of each column were calculated because these values are useful to define the boundary of the data in the spider plot.
在該塊的代碼中,數(shù)據(jù)被讀取到數(shù)據(jù)幀“ df”。 并計算每列的最大值和最小值,因為這些值可用于定義蜘蛛圖中數(shù)據(jù)的邊界。
For example, I extracted the stats of James Harden and LeBron James for our analysis.
例如,我提取了James Harden和LeBron James的統(tǒng)計數(shù)據(jù)進行分析。
df_sel = df[c(3,10),c("PTS.","TRB","AST","STL","BLK")]rownames(df_sel) = c("Harden","Lebron")
To define the function of the spider plot, we need to load the fmsb package.
要定義蜘蛛圖的功能,我們需要加載fmsb包。
comp_plot = function(data,maxxx,minnn){library(fmsb)
data = rbind(maxxx, minnn, data)
colors_border=c( rgb(0.2,0.5,0.5,0.9), rgb(0.8,0.2,0.5,0.9) , rgb(0.7,0.5,0.1,0.9) )
colors_in=c( rgb(0.2,0.5,0.5,0.4), rgb(0.8,0.2,0.5,0.4) , rgb(0.7,0.5,0.1,0.4) )
radarchart( data, axistype=1 , pcol=colors_border , pfcol=colors_in , plwd=4 , plty=1, cglcol="grey", cglty=1, axislabcol="grey", caxislabels=rep("",5), cglwd=0.8, vlcex=0.8)
legend(x=0.5, y=1.2, legend = rownames(data[-c(1,2),]), bty = "n", pch=20 , col=colors_in , text.col = "black", cex=1, pt.cex=3)
}
In the function, “radarchart” is to draw the spider plot, some arguments of which is explained below.
在函數(shù)中,“ radarchart”用于繪制蜘蛛圖,下面將解釋其中的一些參數(shù)。
pcol and pfcol define the color of lines and the color to fill, respectively. plwd and plty give the line width and type of the spider plot, respectively. The grid line (or the web) has the color and type defined by cglcol and cglty. I don’t want to put any label in the center of the spider plot, so the caxislabels is given null strings (rep(“”,5)).
pcol和pfcol分別定義線條的顏色和要填充的顏色。 plwd和plty分別給出了蜘蛛圖的線寬和類型。 網(wǎng)格線(或網(wǎng)絡(luò))的顏色和類型由cglcol和cglty定義。 我不想在蜘蛛圖的中心放置任何標簽,因此caxislabels給出了空字符串(rep(“”,5))。
Let’s see how Harden’s stats look.
讓我們看看哈登的統(tǒng)計數(shù)據(jù)如何。
comp_plot(df_sel[1,],maxxx,minnn)Spider plot of James Harden’s playoff stats詹姆斯·哈登季后賽數(shù)據(jù)的蜘蛛情節(jié)From the plot above, we can see that Harden is not only an excellent scorer (high points) but also a playmaker (high assists). These interpretations are perfectly consistent with the James Harden we know.
從上圖可以看出,哈登不僅是出色的得分手(高分),而且是組織者(高助攻)。 這些解釋與我們所知的詹姆斯·哈登完全吻合。
To compare the stats between James Harden and LeBron James, let’s input both players’ stats to the function.
為了比較詹姆斯·哈登和勒布朗·詹姆斯之間的數(shù)據(jù),我們將兩個球員的數(shù)據(jù)輸入到該函數(shù)中。
comp_plot(df_sel,maxxx,minnn)Spider plot of comparison between James Harden and LeBron James詹姆斯·哈登與勒布朗·詹姆斯之間的比較蜘蛛圖We can see that LeBron has got better rebound and assist numbers in the stats comparing to Harden even though his scoring is not as good as Harden.
我們可以看到,勒布朗的得分和助攻數(shù)據(jù)都比哈登更好,盡管他的得分不如哈登。
Pretty straight forward, right?
很簡單吧?
Let’s do a similar comparison between Giannis Antetokounmpo and Kawhi Leonard in their advanced statistics, which include offense box plus/minus (OBPM), defense box plus/minus (DBPM), offense win share (OWS), defense win share (DWS), and true shooting percentage (TS).
讓我們在Giannis Antetokounmpo和Kawhi Leonard的高級統(tǒng)計數(shù)據(jù)中進行類似的比較,其中包括進攻框正負(OBPM),防守框正負(DBPM),進攻贏率(OWS),防守贏率(DWS),和真實拍攝百分比(TS)。
df = read.csv("playoff_stats_adv.csv")maxxx = apply(df[,c("OBPM","DBPM","OWS","DWS","TS.")],2,max)
minnn = apply(df[,c("OBPM","DBPM","OWS","DWS","TS.")],2,min)
df_sel = df[c(1,3),c("OBPM","DBPM","OWS","DWS","TS.")]
rownames(df_sel) = c("Giannis","Kawhi")
Let’s see Giannis’s stats first.
首先讓我們看看吉安尼斯的統(tǒng)計數(shù)據(jù)。
comp_plot(df_sel[1,],maxxx,minnn)Spider plot of Giannis Antetokounmpo’s playoff advanced statsGiannis Antetokounmpo的季后賽高級數(shù)據(jù)We can find that Giannis is an all-round star because he has good stats in almost every aspect. No wonder he won his second MVP for the 2019–2020 regular season.
我們可以發(fā)現(xiàn)吉安尼斯是全能明星,因為他幾乎在各個方面都有出色的數(shù)據(jù)。 難怪他贏得了2019–2020常規(guī)賽第二次MVP。
Next, let’s compare Giannis with Kawhi Leonard in their advanced statistics.
接下來,讓我們將Giannis和Kawhi Leonard的高級統(tǒng)計數(shù)據(jù)進行比較。
comp_plot(df_sel,maxxx,minnn)Spider plot of comparison between Giannis Antetokounmpo and Kawhi LeonardGiannis Antetokounmpo和Kawhi Leonard之間的比較蜘蛛圖We can see that Giannis outperformed Kawhi in all aspects of the advanced stats.
我們可以看到,吉安尼斯在所有高級統(tǒng)計數(shù)據(jù)方面都勝過了Kawhi。
You can compare any number of players with this simple function, however, I don’t recommend to use a spider plot for the comparison of more than 3 individuals.
您可以使用此簡單功能比較任意數(shù)量的玩家,但是,我不建議使用蜘蛛圖來比較3個以上的玩家。
If you do need to compare a large group of objects, a heatmap could be a better choice for visualization. Here is one of my previous posts of the best heatmap function in R.
如果確實需要比較大量對象,則熱圖可能是可視化的更好選擇。 這是我以前有關(guān)R中最佳熱圖函數(shù)的文章之一。
I hope this short article could contribute to your toolkit as a data scientist!
我希望這篇簡短的文章可以對您作為數(shù)據(jù)科學家的工具包有所幫助!
Photo by Zan on Unsplash Zan在Unsplash上的照片翻譯自: https://towardsdatascience.com/draw-a-radar-spider-plot-with-r-4af9693c3237
r語言繪制雷達圖
總結(jié)
以上是生活随笔為你收集整理的r语言绘制雷达图_用r绘制雷达蜘蛛图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为p10计算机器在哪,华为p10怎么打
- 下一篇: css动画定义,css3的动画(anim