Load Data
download.file("http://www.openintro.org/stat/data/ames.csv", destfile = "ames.csv")
data <- read.csv("/Users/majerus/Downloads/ames.csv") # update to file path on your computer
Load Packages
library(plyr)
library(ggplot2)
library(ggthemes)
library(scales)
library(reshape2)
Calculate Mean Sale Price by Year
mean <- ddply(data, .(Yr.Sold), summarize,
mean_price = mean(SalePrice))
Calculate Mean Sale Price by Year and Sale Condition
# show count of sale conditions by year of sale
table(data$Yr.Sold, data$Sale.Condition)
##
## Abnorml AdjLand Alloca Family Normal Partial
## 2006 57 6 2 11 463 86
## 2007 25 4 5 15 555 90
## 2008 45 2 5 8 533 29
## 2009 41 0 10 9 561 27
## 2010 22 0 2 3 301 13
# create data frame with the mean sale price for each combintation of year and condition
mean.facet<- ddply(data, .(Yr.Sold, Sale.Condition), summarize,
mean_price = mean(SalePrice))
# show first 6 rows of new data frame
head(mean.facet)
## Yr.Sold Sale.Condition mean_price
## 1 2006 Abnorml 137681.3
## 2 2006 AdjLand 106166.7
## 3 2006 Alloca 262867.0
## 4 2006 Family 143136.4
## 5 2006 Normal 171877.9
## 6 2006 Partial 272517.5
# show table of mean sale price by year and condition
dcast(mean.facet, Yr.Sold ~ Sale.Condition)
## Using mean_price as value column: use value.var to override.
## Yr.Sold Abnorml AdjLand Alloca Family Normal Partial
## 1 2006 137681.3 106166.7 262867.0 143136.4 171877.9 272517.5
## 2 2007 162380.0 126500.0 190861.6 175125.0 174506.3 260980.7
## 3 2008 149024.7 82000.0 175415.2 139125.0 176371.7 288733.8
## 4 2009 139317.6 NA 142100.0 170644.4 180332.9 285724.8
## 5 2010 106810.2 NA 53065.5 131433.3 172895.1 304931.6
Plot Mean Sale Price by Year
ggplot(mean, aes(Yr.Sold, mean_price)) +
geom_line(color="dark blue", size=2) +
scale_y_continuous("Mean Sale Price", labels = dollar) +
scale_x_continuous("Year") +
ggtitle("Mean Home Sale Price in Ames, IA") +
theme_tufte() +
theme(plot.title = element_text(size = 16, face="bold"))
Plot Mean Sale Price by Year and Home Type
ggplot(mean.facet, aes(Yr.Sold, mean_price, group = Sale.Condition, colour = Sale.Condition)) +
geom_line(size=2) +
scale_y_continuous("Mean Sale Price", labels = dollar) +
scale_x_continuous("Year") +
ggtitle("Mean Home Sale Price in Ames, IA") +
theme_tufte() +
theme(plot.title = element_text(size = 16, face="bold"))
Plot Mean Sale Price by Year and Home Type (Facets)
ggplot(mean.facet, aes(Yr.Sold, mean_price, group = Sale.Condition, colour = Sale.Condition)) +
geom_line(size=2) +
facet_wrap( ~ Sale.Condition, ncol=1) +
scale_y_continuous("Mean Sale Price", labels = dollar) +
scale_x_continuous("Year") +
ggtitle("Mean Home Sale Price in Ames, IA") +
theme_tufte() +
theme(plot.title = element_text(size = 16, face="bold"))
ggplot(mean.facet, aes(Yr.Sold, mean_price, group = Sale.Condition, colour = Sale.Condition)) +
geom_line(size=2) +
facet_wrap( ~ Sale.Condition, ncol=6) +
scale_y_continuous("Mean Sale Price", labels = dollar) +
scale_x_continuous("Year") +
ggtitle("Mean Home Sale Price in Ames, IA") +
theme_tufte() +
theme(plot.title = element_text(size = 16, face="bold"),
axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(mean.facet, aes(Yr.Sold, mean_price, group = Sale.Condition, colour = Sale.Condition)) +
geom_line(size=2) +
facet_wrap( ~ Sale.Condition, ncol=2) +
scale_y_continuous("Mean Sale Price", labels = dollar) +
scale_x_continuous("Year") +
ggtitle("Mean Home Sale Price in Ames, IA") +
theme_tufte() +
theme(plot.title = element_text(size = 16, face="bold"))