# Category Archives: R

## All-Star Plots?

Here are some star plots for major league baseball batters, pitchers, and ball parks.  The star plots represent the outcomes of a particular at bat for a hitter, a pitcher, or at a given ball park.  For each plot, batter, pitcher, and ball park was varied, while the other two parameters were filled in with the average value.  For instance, all batters outcomes are calculated as if they were facing J. Kinney at Wrigley Field; Pitchers data was calculated as if they were facing K. Medlen at Wrigley; and Park factors were calculated as the outcome of J. Kinney vs K. Medlen at different ball parks.  The data use to calculate these were downloaded from baseball-reference.com and includes the results every single plate appearance so far this season (about 125,000 so far) and where the game was played.  Six outcomes to an at bat were considered: out, walk, single, double, triple, and home run.  The probability of each of these events was estimated creating a vectors of probabilities with six elements corresponding to each of the six outcome considered.  I’ve chosen to display this data using the star plots below.  The key to the star plot can be found in the lower left corner of each plot and displays the probabilities of each outcome relative to other batters.  For instance, a large blue pie piece on the left indicates that batter’s plate appearance ends with a HR more often relative to other players.  Likewise, a large red pie on the right indicates that the batter’s plate appearance ends in an out more often than other players.

I’ve chosen 100 batters based on their wide range of hitting styles.  In the first row, you’ll players who make outs at the lowest rates relative to other players.  These include players like Joey Votto, Andrew McCutchen, David Wight, and Mike Trout.  Further down, you’ll start to see players who you might describe as single’s hitters.  These include players like Ruben Tejada, Derek jeter, B. Revere, and Juan Pierre.  Finally, towards the bottom row, you’ll see the players who are primarily power hitters like Adam Dunn and Jose Bautista with large blue, for home run,s and orange, for walks, pie pieces, with significant red for outs.  Other players on this row like Saltalamacchia, Plouffe, and Rosario have the large blue and significant red pieces, but they lack walks.

The star plot for pitchers is below.  The first thing we need to say here is that Justin Verlander is very, very good at pitching a baseball.  Some other interesting pitchers here are Yu Darvish, Edison Volquez, and Carlos Zambrano.  They seem to give up relatively few hits, but they give up many more walks that the average pitcher.

These plots are ordered from highest to lowest probability that an out will be made in a given plate appearance.  Pittsburgh, Seattle, and San Francisco lead the way in pitching friendly parks.  These are the same as the bottom three  according to ESPNs measure of Park factor.  The most hitter friendly park is, no surprise, Coors field in Colorado.  Other hitter friendly parks include Target and Chase field in Minnesota and Arizona, respectively.  Arizona is expected here, but Minnesota is a little bit surprising.  It looks like, while it is rare to make an out, most hits are only singles, which don’t generate as many runs are their extra base counterparts.  Home run friendly parks include Coors, Chase, Camden Yards, Miller, Comisky, and Yankee Stadium.  Fenway park is solidly in the hitter category, but it gets that way, rather than by giving up many homeruns, by yielding a greater percentage of doubles than any other park.

Cheers.

## MLB Rankings – 8/6/2012

StatsInTheWild MLB rankings as of August 6, 2012 at 8:17pm.  SOS=strength of schedule

 Team Rank Change Record ESPN TeamRankings.com SOS Run Diff NYY 1 – 63-44 3 1 5 +92 Texas 2 – 63-44 4 2 13 +83 LA Angels 3 – 58-51 9 6 7 +49 Washington 4 ↑2 65-43 2 4 23 +82 ChiSox 5 ↑4 59-48 7 5 14 +64 Cincinnati 6 ↑5 66-42 1 3 29 +72 Oakland 7 ↑1 58-50 8 7 8 +28 Detroit 8 ↓1 58-50 13 9 12 +24 TampaBay 9 ↑1 56-52 14 12 3 +19 Atlanta 10 ↑5 62-46 5 8 21 +62 Boston 11 ↓6 54-55 17 13 6 +29 Toronto 12 ↓8 53-55 18 14 2 +9 St. Louis 13 ↑1 59-49 10 15 30 +110 Baltimore 14 ↓2 57-51 16 11 1 -57 Pittsburgh 15 ↓2 61-46 6 10 28 +36 Seattle 16 ↑1 51-59 20 17 4 -3 Arizona 17 ↑4 55-53 15 19 26 +42 SF 18 ↓2 59-49 11 16 27 +19 LA Dodgers 19 ↓1 59-50 12 18 25 +15 NY Mets 20 – 53-56 19 20 17 -5 Minnesota 21 ↑3 47-61 25 22 11 -79 Kansas City 22 – 45-62 26 23 10 -60 Cleveland 23 ↓4 50-58 21 21 9 -90 Milwaukee 24 ↓1 48-59 22 26 19 -13 Philadelphia 25 – 49-59 24 24 19 -29 Miami 26 – 49-60 23 25 15 -100 Chic Cubs 27 ↑1 43-63 28 27 18 -79 San Diego 28 ↓1 46-64 27 28 22 -61 Colorado 29 – 38-68 29 29 20 -117 Houston 30 – 36-73 30 30 16 -142

Past Rankings:

7/23/2012

7/9/2012

7/2/2012

6/25/2012

6/19/2012

6/9/2012

5/28/2012

5/23/2012

5/14/2012

5/7/2012

4/30/2012

4/23/2012

4/16/2012

4/13/2012

Cheers.

Cheers.

Cheers.

Cheers.

## Olympic Pie Charts

First off, for the R nerds out there, (If you don’t care about R, skip to the next paragraph) I’m quickly becoming a huge fan of ggplot2.  Below is an example of combining a facet grid with pie charts using polar coordinates.  Pretty cool.  (I know most graphics people hate pie charts, but I think it works nicely, especially to display Olympic medal counts).  My only question about ggplot2 is how can I color in the background of a plao without changing the pie chart.  I’d like to highlight the plots for a country in years where they hosted the Olympics.  I’ve tried geom_rect() with unlimited bounds,  but this seems to have problems in polar coordinates.  Then I started to try using some grobs commands, but they appear to be out of date.  Any ggplot2 experts out there have any suggestions?

Below is a plot of the Olympic medals won by year for the top 26 countries (by medal count in the 2008 Olympics) for the years 1952 through 2008.  The size of the pie chart is proportional to the number of medals won by a team on a square root scale, and each pie chart shows the break down of total medals by type (gold, silver, or bronze).  So you can see, for instance, that in 2008 China won about 100 medals (in fact, it was exactly 100) and, it’s easy to see that over half of them were gold.  You can also see that the United State won slightly more medals that China (110 to be exact), but the distribution of medals was nearly evenly distributed across the three types (36 gold, 38 silver, 36 bronze).

The graph below is the same as above, but using raw values instead of square root.  This demonstrates why I used a square root scale.

Here is a graph of the top 26 countries by medal count from the 2008 Olympics, across the years from 1896 through 2008.

And finally, if you’re interested, here is a graph of all of the countries from 1952 through 2008, but it’s hard to really see anything.
Cheers.

## Outer Product of Character Vectors in R

Last week, I posted some boxplots of Olympic athletes’ ages by sport, which I then updated using ggplot2 to look nicer.  I had a few requests for the code that I used to generate these plots, so I posted my code here.

has suggested (politely) some improvements to my code in this post, “Outer Product of Character Vectors in R”, on R-bloggers.com.  I’ve been reluctant to post my code in the past as I don’t consider myself to a very good coder, but I need to get over this.  There is too much to be gained by putting my code out there and having it critiqued by others who have more experience with coding than I do, as opposed to just keeping my crappy code to myself.

Cheers.

## Top 8 Tennis Players

Here is a graph of the ATP points for the top 8 tennis players in the world since 2009.  It’s interesting to note that since 2009, Andy Murray has been ranked above each of Nadal, Djokovic, and Federer at some point, but never above them all at once.  Also, over that same time period n0one of the players currently ranked 5-8 have ever been ranked higher than any of the top 4 players.

Cheers.

## Tennis Number 1’s

About a week ago, Roger Federer won his seventh Wimbledon title, which ties Pete Sampras, and his 17 Grand Slam title overall, extending that record even further.  Less (more?) importantly, Mr. Federer regains the ranking of number 1 in the word by the slimmest of margins (11075 to 11000) over Novak Djokovic.  Here is my updated plot of the 19 players who have reached number 1 since 1990.  Notably (still) absent is Mr. Andy Murray.  But as he said himself after his Wimbledon defeat: “I’m getting closer.”

Note: This graph has been updated as it originally included Andy Murray and Benjamin Becker instead of Boris Becker.  I apologize for the mistake.

Cheers.