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:
This graphic is a traditional stacked bar chart. This graphic works on the mpg dataset, which is built into the ggplot2 library. This means that you can access it simply by ggplot(mpg, ….). There is one modification above default in this graphic, I renamed the legend for more clarity.
library(ggplot2)
mpgplot <- ggplot(mpg)
mpgplot + geom_bar(aes(class,fill=trans)) +
guides(fill=guide_legend(title = "Transmission"))
This boxplot is also built using the mpg dataset. Notice the changes in axis labels, and an altered theme_XXXX.
mpgplot2 <- ggplot(mpg)
mpgplot2 + geom_boxplot(aes(manufacturer, hwy)) +
xlab("Vehicle Manufacturer") +
ylab("Highway Fuel Efficiency (miles/gallon)") + coord_flip()
This graphic is built with another dataset diamonds a dataset also built into the ggplot2 package. For this one I used an additional package called library(ggthemes) check it out to reproduce this view.
library(ggthemes)
diamondsplot3 <- ggplot(diamonds) + geom_density(aes(price,color=cut,fill=cut), alpha=0.2, size= 0.5) +
xlab("Diamond Price USD") +
ylab("Density") +
ggtitle("Diamond Price Intensity")+ theme_economist()
diamondsplot3
For this plot we are changing vis idioms to a scatter plot framework. Additionally, I am using ggplot2 package to fit a linear model to the data all within the plot framework. Three are edited labels and theme modifications as well.
irisplot4 <- ggplot(iris,aes(Sepal.Length,Petal.Length)) + geom_point() + geom_smooth(method = lm) +
xlab("Iris Sepal Length") +
ylab("Iris Petal Length") +
ggtitle("Relationship between Petal and Sepal Length") + theme_minimal()
irisplot4
Finally, in this vis I extend on the last example, by plotting the same data but using an additional channel to communicate species level differences. Again I fit a linear model to the data but this time one for each species, and add additional theme and labeling modifications.
irisplot5 <- ggplot(iris,aes(Sepal.Length,Petal.Length,color=Species)) + geom_point() +
xlab("Iris Sepal Length") +
ylab("Iris Petal Length") +
ggtitle("Relationship Between Petal And Sepal Length") +
labs(subtitle="Species Level Comparison") + theme_pander() +
geom_smooth(method = lm,se = FALSE)
irisplot5