This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(RCurl)
## Warning: package 'RCurl' was built under R version 3.1.2
## Loading required package: bitops
## Warning: package 'bitops' was built under R version 3.1.2
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "Rcurl")))
#credentials$handshake()
library(Quandl)
## Warning: package 'Quandl' was built under R version 3.1.2
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(ggplot2)
library(reshape2)
## Warning: package 'reshape2' was built under R version 3.1.2
#load US crime rate data from quandl
crimedata = Quandl("FBI/CRIME11")
## Warning in Quandl("FBI/CRIME11"): It would appear you aren't using an
## authentication token. Please visit http://www.quandl.com/help/r or your
## usage may be limited.
#subset and merge data
crimes<-c(crimedata$"Robbery Rate", crimedata$"Burglary Rate", crimedata$"Motor Vehicle Theft Rate")
needdata<-data.frame(Year=crimedata$"Year",Robbery=crimedata$"Robbery Rate", Burglary=crimedata$"Burglary Rate", "Motor Vehicle Theft"=crimedata$"Motor Vehicle Theft Rate")
tempdata<-melt(needdata,id="Year")
#plot by ggplot2
Q<-ggplot(tempdata, aes(x=Year, y=value, colour=variable,group=variable)) + geom_line(size=1,fill=TRUE) + ylab("Crime Rate")
Q+labs(title="Crime Rates in Us")
#mtcars dataset continue labels
p <- ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars)))+geom_point(aes(colour=factor(cyl)))
p + geom_text(aes(colour=factor(cyl)),hjust=-0.1, vjust=-0.1)+labs(title="Milage vs. Car Weight",x="Weight", y="Mileage")
#combining plots
library(grid)
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 3.1.2
#p1
p1<-ggplot(mtcars,aes(wt, mpg))+geom_point(aes(color=factor(cyl)),size=4)
p1+labs(title="MPG VS Weight",x="Car Weight",y="Miles Per Gallon")
#p2
p2<-ggplot(mtcars,aes(wt, disp))+geom_point(aes(color=factor(cyl)),size=4)
p2+labs(title="MPG VS Weight",x="Car Weight",y="disp")
#p3
p3<-ggplot(mtcars,aes(x=wt))+geom_histogram(binwidth = 0.3,aes(fill = ..count..),colour="gray",alpha=0.7)+ scale_fill_gradient("Count", low = "green", high = "red")
p3+geom_density()+labs(title="Hist of Weight",x="Car Weight",y="Counts")
#p4
p4 <- ggplot(mtcars, aes(factor(cyl), mpg))+ geom_boxplot(aes(fill = factor(cyl)),alpha=0.7)
p4 +labs( title="Car Milage Data", x="Number of Cylinders", y="Miles Per Gallon")
grid.arrange(p1,p2,p3,p4, ncol = 2)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.