R365: Day 33 – Plotting polynomials with {ggplot2}

Plotting polynomials is really easy using {ggplot2}. I am still a novice with {ggplot2}, but the advantage of the package is that it lets you add on code as you go to specify different aspects about the graph, such as the title, whether to display lines or points, or how any linear model should be assessed. Lets use the dataset and code that I used as an example in Day 32. We were able to easily make a graph using the plot() function. However, to modify the graph using the basic {graphics} package requires some knowledge of all of the options in {par}, which are many (“we are legion”), and *importantly* need to be stated in the correct way. {ggplot2} lets you tag on extra portions of a graph just by adding a “+” sign. This can even end up as a burden if you re-use code a lot, as you can end up with tons of useless code that just clutters up the lines. But if you’re careful, it should not be too much of a problem. Once you get used to the new syntax, {ggplot2} is way easier and more flexible, and lets you make some really nice graphs.

##Lets try to plot out the curve for the graph
library(ggplot2) 
##so lets just look at just the scatterplot
qplot(x=spore.date, y=spore$spore.number, geom=c("point"))

Rplot1

 

##Now lets look at the graph with some smoothed averages
qplot(x=spore.date, y=spore$spore.number, geom=c("point","smooth"))

Rplot2

## now lets look at the graph and treat it as a quadratic
qplot(x=spore.date, y=spore$spore.number, geom=c("point","smooth"), method="lm", formula = y ~ 
 poly(x, 2))

Rplot3

##And as a X^3 function
qplot(x=spore.date, y=spore$spore.number, geom=c("point","smooth"), method="lm", formula = y ~ 
 poly(x, 3))

Rplot4

#And just to be rediculous, lets look at a x^10 function
qplot(x=spore.date, y=spore$spore.number, geom=c("point","smooth"), method="lm", formula = y ~ 
 poly(x, 10))
###......really ugly....

Rplot5

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s