# Plotting Introduction

In this post we’ll cover some of the basic plot types in R such as scatter plots, box plots, histograms, and line graphs. Let’s start with the basic scatter plot.  We’ll use the command plot.

``````##First let's make some data
x<-c(1,2,3,4,5,6)
y<-c(2,4,6,8,10,12)

plot(x,y)

``````

Next we’ll make a box plot. For this plot we’ll use some data that is available within R already. Let’s take a look at what’s already within R using the command data(). We see that there’s lots of datasets within R that are ready to go! Let’s load ‘rivers’, which says it is the length of major rivers in North America. Use data(rivers) to load rivers into R. Type ‘rivers’ to see what the data looks like.

``````data(rivers)
rivers
##   [1]  735  320  325  392  524  450 1459  135  465  600  330  336  280  315
##  [15]  870  906  202  329  290 1000  600  505 1450  840 1243  890  350  407
##  [29]  286  280  525  720  390  250  327  230  265  850  210  630  260  230
##  [43]  360  730  600  306  390  420  291  710  340  217  281  352  259  250
##  [57]  470  680  570  350  300  560  900  625  332 2348 1171 3710 2315 2533
##  [71]  780  280  410  460  260  255  431  350  760  618  338  981 1306  500
##  [85]  696  605  250  411 1054  735  233  435  490  310  460  383  375 1270
##  [99]  545  445 1885  380  300  380  377  425  276  210  800  420  350  360
## [113]  538 1100 1205  314  237  610  360  540 1038  424  310  300  444  301
## [127]  268  620  215  652  900  525  246  360  529  500  720  270  430  671
## [141] 1770
``````

To make a box plot use the function boxplot.

``````boxplot(rivers)

``````

Now let’s make a histogram with the same ‘rivers’ dataset.

``````hist(rivers)

``````

Let’s make a line plot using some time series data. Let’s load the dataset ‘airmiles’.

``````data(airmiles)
airmiles
## Time Series:
## Start = 1937
## End = 1960
## Frequency = 1
##  [1]   412   480   683  1052  1385  1418  1634  2178  3362  5948  6109
## [12]  5981  6753  8003 10566 12528 14760 16769 19819 22362 25340 25343
## [23] 29269 30514

plot(airmiles)

``````

We’ll notice that we used the same function plot to make a line graph here as we did in the first plot using x and y to make a scatter plot. R has it’s own defaults based on the type of data it receives for the function plot.

If we want to force the plot type (a solid line versus points) we can use the parameter ‘type’, where type=”l” makes a line and type=”p” makes points. Let’s see this using the airmiles plot again.

``````plot(airmiles, type="p")

``````

Finally let’s use the data x and y from the first plot to make a plot with a line rather than points.

``````plot(x,y, type="l")

``````

That’s it for now, we’ll add more plotting options for the graphs in subsequent posts.