Plotting functions of a variable in a dataset

Sometimes, we might wish to visualize the effect of applying a mathematical function to a set of values, instead of the original variable itself. In this recipe, we will learn a simple method to plot functions of variables.

Getting ready

We will use the base graphics library for this recipe, so all you need to do is run the recipe at the R prompt. It is good practice to save your code as a script for use again later.

How to do it...

Let's say we want to plot the difference in rainfall between Tokyo and London. We can do this just by passing the correct expression to the plot() function:

rain <- read.csv("cityrain.csv") plot(rain$Berlin-rain$London,type="l",lwd=2, xaxt="n",col="blue", xlab="Month",ylab="Difference ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.