R Graphics Cookbook Second Edition is Available for Free

Winston Chang’s R Graphics Cookbook 2nd Edition

Winston Chang from RStudio quietly announced last week that the second edition of his popular R Graphics Cookbook: Practical Recipes for Visualizing Data is available now to buy.

Not just that, the book is also available online for free at https://r-graphics.org/.

Winston Chang’s first edition of R Graphics Cookbook was the first R book I got. The book and its online resource http://cookbook-r.com/ have been a fantastic resource for solving nitty-gritty details while making statistical visualization with ggplot, even now. Yes, it is about five years old. Yes, that was before tidyverse became the thing. Yes, it has solutions with qplot, ddply, and so on. Still very useful for a lot ggplot questions.

Always wondered if there is a second edition for it and here it is. Can’t wait to dig through the second edition of R Graphics Cookbook.

If you don’t know about R Graphics Cookbook,

it is a practical guide that provides more than 150 recipes to help you generate high-quality graphs quickly, without having to comb through all the details of R’s graphing systems. Each recipe tackles a specific problem with a solution you can apply to your own project, and includes a discussion of how and why the recipe works.

Yes, it is tidyverse proofed 🙂 It is worth the buy, even if one has the first edition.

In the spirit of the awesome R Graphics Cookbook, let us try a simple plotting example of making awesome plot using R Graphics cookbook’s recipe 🙂

How to Make a Plot with User Defined Functions with ggplot2?

Let us first load tidyverse.

library(tidyverse)

Let us say we have a function that computes something and we want to plot the output of the function for each of the input.

In the example the user defined function is a simple function that computes the square of a number.

# function computing cubed
my_fun <- function(x){
    x^2
}

We can plot the user defined function with stat_function and use the function as argument to it. And we supply input to the function using data frame with single column.

# use stat_function to plot 
ggplot(data.frame(x = c(-10, 10)), aes(x = x)) +
  stat_function(fun = my_fun) + theme_bw()  

We get a nice parabola as we intended.

Plotting User Defined Function with ggplot
Plotting User Defined Function with ggplot

Now get over to https://r-graphics.org/ to read online or to Amazon to get a physical copy.