# Laura Diane Hamilton

Technical Product Manager at Groupon

# Graphing with R

R makes it easy to quickly generate attractive graphs from data in a csv file. The command-line interface is dead simple.

Here is how to get started with R plotting:

First, install R: ``` sudo apt-get update sudo apt-get install r-base ```

Then, save your data as a csv file. For this tutorial, I will use the Bike Sharing Dataset from the UCI Machine Learning Repository.

Specifically, I am using the daily data file, which you can download from my site here. Save the file wherever you like.

Navigate to the directory with the csv datafile that you just saved. ``` cd MyDataFiles ```

Start R from within that directory: ``` R ```

Import the csv file into R: ``` data <- read.csv("day.csv") ```

Now, let's plot the relationship between the temperature ("temp") in Celsius and the total number of bikes rented ("cnt"). In this dataset they have already normalized the temperature column by dividing each temperature by 41 (the "maximum" temperature), so our x-axis will be decimal values. ``` plot(data\$temp,data\$cnt) ```

You should see the plot displayed.

Not bad, but let's add some axis labels: ``` plot(data\$temp,data\$cnt,xlab="Normalized Temperature",ylab="Number of Bike Rentals") ```

Our graph needs a title: ``` plot(data\$temp,data\$cnt,xlab="Normalized Temperature",ylab="Number of Bike Rentals",main="Ambient Temperature Versus Number of Bike Rentals") ```

Finally, let's save that image to a file so that we can use it in our report: ``` plot(data\$temp,data\$cnt,xlab="Normalized Temperature",ylab="Number of Bike Rentals",main="Ambient Temperature Versus Number of Bike Rentals") dev.copy(jpeg,filename="temperature-and-bike-rentals.jpg") dev.off () ```

You should now have an image file that looks like this:

From this graph you can see that people don't like to rent bikes when it is really cold out, which is pretty much what we would expect.

Lauradhamilton.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.