Friday, January 29, 2016

More graphing in R

Apparently, our graphs are supposed to look nice. I suppose that does matter sometimes. I had to monitor a process all evening, so I spent it messing around with R's many and varied graphing packages trying to make my homework assignment pretty. Here's an example using just the base package.

#probability and cummulative functions
p = function(x) 6 * x * (1 - x)
cdf = function(x, width) cumsum(p(x)*(width/length(x)))

#take the midpoint of 100 even-spaced intervals
x = ((0:99) + 0.5)/100

#plot the pdf
plot.new()
plot(x, p(x), type="l", col="blue", ylab = " ")

#plot the cdf with a reference line to show it goes to 1
lines(x, cdf(x, 1), type = "l", col="red")
lines(c(0,1), c(1,1), lty=2)

#pretty it up a bit
title("p(x) = 6x(1-x)")
text(0,1.3, "p(x)", pos=4, col="blue")
text(0,1.1, "cdf(x)", pos=4, col="red")


Oooh and aaaah (though I have no idea why blue and red saved to very dark blue and maroon - they were quite bright when displayed in RStudio).


No comments:

Post a Comment