Stochastic Nonsense

Put something smart here.

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:

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

data: csv

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

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

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

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

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:

1
2
> 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')