LeBron James, a basketball LeBron, was born on December 30, 1984 in Akron, Ohio, American professional basketball player and small striker, now serving Cleveland Cavaliers.
LeBron James was selected by Cleveland Cavaliers in the first round of the NBA Draft in 2003. In 2003-2010, James played for 7 years in the Cavaliers, during which he won two NBA Most valuable players (MVP). The 2010 transfer to the Miami Heat, with Dwyane Wade, Chris Bosh formed the "Big Three" lineup.
Now, use the R language to analyze which team James (LBJ) was best at when he met. First, map out the geographical distribution of the NBA's 30 teams, as shown in (1).
Figure (1) geographical distribution of 30 teams in the NBA
The short name of the team:
Figure (2) The short name of the team
The city and partition where the team is located:
Figure (3) the city and partition where the team is located
First, we collect the raw data, which contains 3 variables: Opp, avgpts, state.
OPP represents the team name of the opponent, Avgpts is the average score for James against the team, State indicates that the states where the team is located, the names of the States must be consistent with the U.S. map information built into R, and the data information for the map of the United States is obtained through the function Map_data ().
Also, since NBA teams are not in every state, for states without teams, we assign a value of avgpts to 0, which is represented in gray areas when drawing a graph.
The data source is saved in the lbj.txt.
Lbj.txt
OPP avgpts Stateatl21.75Georgiabos29.25Massachusettsbrk21.67 ' New York 'CHA - ' North Carolina 'CHI -Illinoiscle27.67Ohiodal -Texasden23.5Coloradodet inMichigangsw -Californiahou *Texasind +Indianalac -Californialal35.5Californiamem +Tennesseemil27.5Wisconsinmin +Minnesotanoh -Louisiananyk27.67 ' New York 'OKC theOklahomaorl28.67Floridaphi23.75Pennsylvaniapho AArizonapor22.5Oregonsac -Californiasas atTexasuta toUtahwas24.33WashingtonNA 0AlabamaNA 0ArkansasNA 0ConnecticutNA 0DelawareNA 0 ' District of Columbia 'NA 0IdahoNA 0IowaNA 0KansasNA 0KentuckyNA 0MaineNA 0MarylandNA 0MississippiNA 0MissouriNA 0MontanaNA 0NebraskaNA 0NevadaNA 0 ' New Hampshire 'NA 0 ' New Jersey 'NA 0 ' New Mexico 'NA 0 ' North Dakota 'NA 0 ' Rhode Island 'NA 0 ' South Carolina 'NA 0 ' South Dakota 'NA 0VermontNA 0VirginiaNA 0 ' West Virginia 'NA 0Wyoming
Use the software r3.2.5+rstudio-0.99.893 to analyze the code as follows:
Deifen. R
Library (GGPLOT2) LBJ <-Read. Table ("G:/myproject/rdoc/unit2/rchap6/lbj.txt", Header = T,quote ="'") Attach (LBJ)# #查看数据前5行Head (LBJ)# #获取美国地图信息State_map <-Map_data ("State") P=ggplot (Lbj,aes (map_id= State)) +geom_map (Aes (fill=avgpts),Map=STATE_MAP) + expand_limits (x=state_map$long,y=state_map$lat) + scale_fill_continuous (Limits=c ( +, Max (avgpts)), high=' Red3 ', low=' Yellow ', guide="Colorbar") + Labs (title=' James away average score ') Attach (State_map) State. Uni=unique (region)#存放各州的名称xx=0; yy=0 #事先建立变量xx和yy, the corresponding coordinate values for each state are found in the loop below. for(I in1:length( State. Uni)) {Xx[i]=mean (long[region== State. Uni[i]]) Yy[i]=mean (lat[region== State. Uni[i]])}order=0 #按变量state. Uni to find the location of the states in the dataset LBJ, stored in the variable order for(I in1:length( State. Uni)) {Order[i]=which ( State== State. Uni[i])}labels=opp[order]#通过位置找到各州对应的球队名称P+annotate ("Text",x=XX,y=yy,label=labels)#最后绘图并添加注释
The effect is as follows:
Figure (4) James ' away score
In the figure (4), the color from yellow to red, from shallow to deep, indicating that the score from small to large growth, the more red color indicates the higher the score, the lighter the color indicates the lower the score. James in the Encounter OKC (Russian Clamart Thunder Team), the color is very red, the representative score is very high, indicating that James to OKC restraint role is very big.
Analyze James ' away score in R language