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(ggplot2)
#density plots with groups
p<-ggplot(mtcars, aes(x=mpg)) + geom_density(aes(group=cyl, colour=cyl,fill=cyl),alpha=0.7)
p+labs(title="MPG Distribution by Car Cylinders",x="Miles Per Gallon")
You can also embed plots, for example:
#boxplots 1
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(aes(fill = factor(cyl)),alpha=0.5)+labs( title="Car Milage Data",
x="Number of Cylinders", y="Miles Per Gallon")
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_boxplot(notch = TRUE,aes(fill = factor(cyl)),alpha=0.5)+labs( title="Car Milage Data",
x="Number of Cylinders", y="Miles Per Gallon")
## notch went outside hinges. Try setting notch=FALSE.
## notch went outside hinges. Try setting notch=FALSE.
#barplot 1
p<- ggplot(mtcars, aes(x=factor(gear)))
p+geom_bar(aes(fill = factor(gear)))+labs( title="Car Distribution",
x="Number of Gears", y="Counts")
#barplot 2
p<- ggplot(mtcars, aes(factor(gear)))
p+geom_bar(aes(fill = factor(gear)))+labs( title="Car Distribution",
x="Number of Gears", y="Counts")+coord_flip()
#barplot 3 Stacked Bar Plot
p<- ggplot(mtcars, aes(x=factor(gear)))
p+geom_bar(aes(fill=factor(vs)))+labs(title="Car Distribution by Gears and VS",
x="Number of Gears")
#barplot 4 Grouped Bar Plot
p<- ggplot(mtcars, aes(x=factor(gear)))
p+geom_bar(aes(fill=factor(vs)),position="dodge")+labs(title="Car Distribution by Gears and VS",
x="Number of Gears")
#line chart
p<- ggplot(mtcars, aes(x=wt,y=mpg))+geom_line(aes(group=cyl, colour=cyl,fill=factor(cyl)),size=1)
p+labs(title="Relationship between Weight and Mpg",
x="Weight(klb)",y="Miles Per Gallon")
#pie chart
pppp<-tapply(mtcars$cyl,mtcars$cyl,sum)
slices<-c()
for(i in 1:length(pppp)){
slices[i]<-matrix(pppp)[i]
}
lbls<-dimnames(pppp)[[1]]
lbls<-paste(lbls, "-cyl ")
pct <- round(slices/sum(slices)*100)
lbls <- paste(lbls, pct) # add percents to labels
lbls <- paste(lbls,"%",sep="") # ad % to labels
pie(slices,labels = lbls, col=rainbow(length(lbls)),
main="Pie Chart of Cars")
#dotplot
x <- mtcars[order(mtcars$mpg),] # sort by mpg
x$cyl <- factor(x$cyl) # it must be a factor
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,labels=row.names(x),cex=.7,groups= x$cyl,
main="Gas Milage for Car Models\ngrouped by cylinder",
xlab="Miles Per Gallon", gcolor="black", color=x$color)
x <- mtcars[order(mtcars$mpg),] # sort by mpg
ggplot(x, aes(x = mpg, y = reorder(row.names(x),mpg),colour = cyl)) +
geom_point(size=4,stackgroups = TRUE) + labs(title = "Gas Milage for Car Models\ngrouped by cylinder",x="Miles Per Gallon",y="Vehicles")+facet_wrap(~cyl)
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.