Skip to main content

Posts

Showing posts with the label how-to

[How-To] How to a add custom table to a ggplot chart

For one of my recent posts I needed to add a customized and dynamic table to a ggplot chart. This can often be helpful to display information in addition to the main idea you are presenting in the plot. In my example I wanted to show free kick conversion rates by season in a table format. I found this to be far from straightforward so I will try to outlay the main concepts here in case anybody has the same problem. To make this as engaging as possible I will use an example based on the diamonds data set which comes with the usual R installation. This data set has information on the size, cut, color, prize and some other characteristics of diamonds. You can follow the steps below by copying the code into your RStudio or R environment. library (ggplot2) head (diamonds) ## # A tibble: 6 x 10 ## carat cut color clarity depth table price x y z ## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> ...

[How-To] Installation of gganimate and devtools for R in an Anaconda environment

I have recently started to play around with gganimate to create some animated graphs based on the ggplot framework. This allows you to include another dimension in your chart, e.g. a time dimension for a scatter plot and save them as gifs. You can see some cool examples on the gganimate github page which I linked before. I had some complication with installing devtools which is necessary to install gganimate through github. I think I only had these problem because I am running R/RStudio in the Anaconda environment. When trying to install devtools through install.packages("devtools") I got the below error: x86_64-apple-darwin13.4.0-ar: no such file or directory make: *** [libmygit.a] error 1 error: compilation failed for package ‘git2r’ devtools requires git2r to be installed as a dependency but somehow this failed for my R environment. The solution to my problem was to install git2r beforehand through Anaconda directly by running the following line in the ...

[How-To] How to plot a chart on top of a background image in ggplot2

This post is an instructional one on how to plot a chart on top of a background image in ggplot2 and R. The motivation for plotting on a background may just be a design choice or it may serve the purpose of supporting the message of the chart. I will show how i create my previous post's chart showing the location of successful free kicks on a football pitch. Below code creates a 10x10 matrix with normally distributed doubles, which we will use as dummy data so you can easily replicate the results. N We can convert this into a data frame and plot the initial data. library(ggplot2) df Next we need a background image which you can import with the jpeg package. In my case this is a picture of half a football pitch. library(jpeg) img We can now combine these two parts and annotate the background image to the ggplot plot. We are using ggplot's annotation_custom and the rasterGrob function of the grid package. Essentially we are saying not to scale the imported...