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)
library(RColorBrewer)


Calculate Mean Sale Price by Year and Building Type
# show count of building types by year of sale
table(data$Yr.Sold, data$Bldg.Type)
##       
##        1Fam 2fmCon Duplex Twnhs TwnhsE
##   2006  505     17     25    27     51
##   2007  593     12     20    18     51
##   2008  509     17     19    21     56
##   2009  546      7     30    21     44
##   2010  272      9     15    14     31
# create data frame with the mean sale price for each combintation of year and type
means <- ddply(data, .(Yr.Sold, Bldg.Type), summarize,     
               mean_price = mean(SalePrice))

# show first 6 rows of new data frame
head(means)
##   Yr.Sold Bldg.Type mean_price
## 1    2006      1Fam   187539.7
## 2    2006    2fmCon   138752.9
## 3    2006    Duplex   137450.4
## 4    2006     Twnhs   126638.9
## 5    2006    TwnhsE   189787.8
## 6    2007      1Fam   188498.5
# show table of mean sale price by year and type
dcast(means, Yr.Sold ~ Bldg.Type)
## Using mean_price as value column: use value.var to override.
##   Yr.Sold     1Fam   2fmCon   Duplex    Twnhs   TwnhsE
## 1    2006 187539.7 138752.9 137450.4 126638.9 189787.8
## 2    2007 188498.5 132200.0 130990.2 142266.1 194887.9
## 3    2008 179832.7 129377.4 134392.6 131583.3 217654.0
## 4    2009 185768.6 100292.9 150456.7 146173.8 178070.1
## 5    2010 179108.5 104377.8 141063.3 136885.7 166661.6


Plot Mean Sale Price by Year and Building Type (manually assign colors)
# define colors by name

p <-
  ggplot(means, aes(Yr.Sold, mean_price, group = Bldg.Type, colour = Bldg.Type)) + 
  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")) 

p +  scale_colour_manual(values = c("red","blue", "dark green", "grey", "black"))

# define colors by hex code 
# see http://www.w3schools.com/tags/ref_colorpicker.asp & http://colorbrewer2.org

p +  scale_colour_manual(values = c("#0000FF","#197519", "#CC2900", "#4700B2", "#E6E600"))

Plot Mean Sale Price by Year and Building Type (use assign colors RColorBrewer)
# use display.brewer.all() to see all options

p + scale_colour_brewer("Colors in Set1", palette="Set1")

p + scale_colour_brewer("Colors in Paired", palette="Paired")