Saturday, April 9, 2011

How did I make this plot?


To make this plot, I used R's plot(), points() and lines() commands. If you have been wanting to learn how to plot in R, watch it unfold in this video tutorial:



Also, here's the code I used:

## ---------------------------- ##
## Plotting Points *and* Lines ##
## ---------------------------- ##
## Define the Vectors I used in the video ##
pow = c(0.95, 0.6, 0.3, 0.15, 0.1, 0.05, 0.1, 0.15, 0.3, 0.6, 0.95)
pow2 = c(0.99, 0.75, 0.4, 0.2, 0.15, 0.05, 0.15, 0.2, 0.4, 0.75, 0.99)
thet = c(7:17)
plot(thet,pow) ## Plots points
plot(thet,pow2) ## Plots points on *new* plot
## But we want them on the same plot...
## Try options in plot
## Useful options in plot()
plot(thet,pow, type="l", col="red") ## Lines
plot(thet,pow, type="o", col="blue") ## Overplotted lines and points
plot(thet,pow, type="s", col="orange") ## Stair steps
## Less useful options
plot(thet,pow, type="b") ## "Both" lines and points
plot(thet,pow, type="c") ## "lines" part of "both"
plot(thet,pow, type="h", xlim=c(0, 20),ylim=c(0,1)) ## Histogram-like
plot(thet,pow, type="n",xlim=c(0, 20),ylim=c(0,1)) ## No plotting... ?
## Note: xlim = c(low,high) sets low to be the lower limit for the x-axis and high to be the upper limit.
## ylim = c(low,high) does the same for the y-axis. These can be useful for
## Points and Lines to put on top of the active plot
plot(thet,pow, type="n",xlim=c(0, 20),ylim=c(0,1)) ## No plotting... ?
points(thet,pow2, col="blue")
lines(thet,pow2, col="green")
lines(thet,pow, col="red", type="o") ## plot() options work for lines() and points()
title("My Power Plot") ## Sometimes it is nice to add a title

No comments:

Post a Comment