Basic Plotting in R — Part 1 of a Series

This is post #01 in a running series about plotting in R.

So, say you have some data that you would like to plot in R. In future entries I’ll discuss plotting multiple series, multiple axes, manipulating axis names and formats changing colors or glyphs, saving to disk as png or pdf, time series, highlighting weekends, but for now, let’s throw up a simple graph:

> s <- data.frame(x=1:30, y=10*runif(n=30) )
>
> plot(x=s$x, y=s$y )


(csv)

Plot 01.01

That’s nice, but we could use some better labeling:

> plot(x=s$x, y=s$y,
+	xlab='x label', ylab='y label', main='Basic Plotting Sample')


plot01.02

And while we’re messing around, let’s turn out data series blue and plot both dots and connected lines:

> plot(x=s$x, y=s$y, type='b', col='blue',
+	xlab='x label', ylab='y label', main='Basic Plotting Sample')


plot01.03

Now finally, we may wish to zoom into a particular piece of the plot. xlim and ylim allow you to override the inferred plotting limits. Let’s examine the upper right corner:

> plot(x=s$x, y=s$y, type='b', col='blue', xlim=c(20,30), ylim=c(6,10),
+	xlab='x in [20,30]', ylab='y label in [6,10]', main='Basic Plotting Sample, Filtered')


plot01.04

This entry was posted in Plotting, R, Visualization and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>