Robert Norberg & Robert Truesdale
n = 40000
x0 = 1
y0 = 1
a = 1
b = 4
c = 60
x <- c(x0, rep(NA, n - 1))
y <- c(y0, rep(NA, n - 1))
cor <- rep(0, n)
for (i in 2:n) {
x[i] = y[i - 1] - sign(x[i - 1]) * sqrt(abs(b * x[i - 1] - c))
y[i] = a - x[i - 1]
cor[i] <- round(sqrt((x[i] - x[i - 1])^2 + (y[i] - y[i - 1])^2), 0)
}
n.c <- length(unique(cor))
cores <- heat.colors(n.c)
plot(x, y, pch = ".", col = cores[cor], main = "Fractal Example")
library(lattice)
attach(mtcars)
gear.f <- factor(gear, levels = c(3, 4, 5), labels = c("3gears", "4gears", "5gears"))
cyl.f <- factor(cyl, levels = c(4, 6, 8), labels = c("4cyl", "6cyl", "8cyl"))
cloud(mpg ~ wt * qsec | cyl.f, layout = c(3, 1, 1), main = "3D Scatterplot by Cylinders")
ggplot(diamonds, aes(x = carat, y = price, color = clarity)) +
facet_grid(color ~ cut) +
scale_y_sqrt() +
geom_point(alpha = 0.3) +
geom_smooth(method = lm)
qplot() is especially quick and easygeom_point() indicates your desire to create a scatterplot, while stat_boxplot calculates the summary statistics necessary to plot a boxplot.VOC concentration measured in indoor air over the course of a year.
library(xtable) # A nice package for displaying tables in Markdown
print(xtable(head(voc.data, 8)), type = "html")
| DateTime | Location | Chloroform | Trichloroethene | Tetrachloroethene | |
|---|---|---|---|---|---|
| 156 | 2011-12-12 04:10:13 | Outside Air | 0.52 | 1.55 | |
| 157 | 2011-12-12 05:49:41 | Outside Air | 0.45 | 1.60 | |
| 166 | 2011-12-12 20:44:48 | Outside Air | 0.50 | 0.53 | |
| 167 | 2011-12-12 22:24:16 | Outside Air | 0.50 | 1.97 | |
| 168 | 2011-12-13 00:03:44 | Outside Air | 0.57 | 0.54 | 1.95 |
| 169 | 2011-12-13 01:43:11 | Outside Air | 0.53 | 0.58 | 2.09 |
| 199 | 2011-12-15 12:45:02 | Outside Air | 0.56 | 1.99 | |
| 200 | 2011-12-15 14:24:29 | Outside Air | 0.88 | 5.12 |
First, we must get this data into long form to use it in ggplot2. I use reshape2, a nice package created by the author of ggplot2 for manipulating data structures.
library(reshape2)
voc.long <- melt(data = voc.data, id.vars = c("DateTime", "Location"), measure.vars = c("Chloroform",
"Trichloroethene", "Tetrachloroethene"), value.name = "Concentration", variable.name = "Compound")
| DateTime | Location | Compound | Concentration | |
|---|---|---|---|---|
| 1 | 2011-12-12 04:10:13 | Outside Air | Chloroform | 0.52 |
| 2 | 2011-12-12 05:49:41 | Outside Air | Chloroform | 0.45 |
| 3 | 2011-12-12 20:44:48 | Outside Air | Chloroform | 0.50 |
| 4 | 2011-12-12 22:24:16 | Outside Air | Chloroform | 0.50 |
| 5 | 2011-12-13 00:03:44 | Outside Air | Chloroform | 0.57 |
| 6 | 2011-12-13 01:43:11 | Outside Air | Chloroform | 0.53 |
| 7 | 2011-12-15 12:45:02 | Outside Air | Chloroform | 0.56 |
| 8 | 2011-12-15 14:24:29 | Outside Air | Chloroform | 0.88 |
reshapeGUI package.library(reshapeGUI)
reshapeGUI()
Tip: If your dataset is large, create a temporary dataset that consists of the first few rows and manipulate this in the GUI. It will work much faster.
temporary <- voc.data[1:7, ]
Next, we must coerce DateTime to a Date/Time class recognized by R (as of right now it is seen by R as a "factor", a categorical variable):
voc.long$DateTime <- as.POSIXct(voc.long$DateTime, format = "%Y-%m-%d %H:%M:%S",
tz = "GMT")
For questions about the format argument, see:
http://stat.ethz.ch/R-manual/R-patched/library/base/html/strptime.html
R assumes the data were taken in the same time zone that your computer abides by. If this is not the case, make sure to specify!!! Take for example this code:
is.na(as.POSIXct("3/11/2012 2:10:00", format = "%m/%d/%Y %H:%M:%S"))
## [1] TRUE
This is an hour that does not exist in Eastern Standard Time. If I specify the time zone this problem is corrected:
is.na(as.POSIXct("3/11/2012 2:10:00", format = "%m/%d/%Y %H:%M:%S", tz = "GMT"))
## [1] FALSE
Now we're ready to plot the data. The basic plotting call is ggplot(). It has 2 key arguments - data and aes(), short for "aesthetics".
my.plot <- ggplot(data = voc.long, aes(x = DateTime, y = Concentration, color = Compound))
data argument I supply the long form data set we just created. aes() argument I assign mappings between the data and graphical elements.
DateTime Concentration Compound
Now lets add a layer to our plot:my.dot.plot <- my.plot + geom_point(alpha = 0.2)
my.dot.plot
Until now our plot was an abstract idea inside the computer. Now we have supplied instructions for how that abstraction should manifest itself visually.
I chose geom_point() for a scatter plot, but you could choose any of the available geoms that make sense for what you're trying to plot.
Different geoms come with different options/arguments.
geom_point() the optional argument alpha=0.1 makes each point semi transparent. Also, notice some key features of the default settings:
You can add as many layers as you like on top of one another.
Additional layers will adhere by the aesthetic mappings you have already supplied.
To demonstrate this I add a smoothed line to each compound:
my.trend.plot <- my.dot.plot + geom_smooth(lwd = 1.2)
my.trend.plot
data =" argument to the new layer.inherit.aes=FALSE to your layer and then supply another aes() argument. plot.text <- data.frame(y.coord = 200, x.coord = strptime("11/7/2011 0:0:00",
format = "%m/%d/%Y %H:%M:%S", tz = "GMT"), text = "HVAC On \n X")
my.dot.plot <- my.dot.plot + geom_text(inherit.aes = F, data = plot.text, aes(x = x.coord,
y = y.coord, label = text, guide = F))
my.dot.plot
my.panel.plot <- my.dot.plot + facet_wrap(~Location, nrow = 2)
my.panel.plot
facet_grid(Dimension1~Dimension2)Notice that the panels have been labeled for you. These can also be played with quite a bit.
Some of these panels look like they could be informative, but others look like nothing's there. Concentration data is meant to be viewed on a log scale. Lets add this.
my.panel.plot <- my.panel.plot + scale_y_log10()
my.panel.plot
Lets finish off the plot with some small aesthetic details.
my.panel.plot <- my.panel.plot + labs(x = "Date", y = "Concentration (ug/L)",
title = "VOC Vapor Intrusion")
my.panel.plot <- my.panel.plot + theme_bw(base_size = 18)
library(scales)
my.panel.plot <- my.panel.plot + scale_x_datetime(labels = date_format("%b-'%y"),
breaks = "2 months", minor_breaks = "month") + theme(axis.text.x = element_text(angle = 25,
vjust = 0.5))
# Display my code
print("Output appears below code chunk")
## [1] "Output appears below code chunk"
Then add text using markdown!
You can use markdown to create:
Switch back and forth between R code and markdown (a simple text editor language)