I was introduced to plotting and exploring data in R during the online Coursera Data Science course. We covered the base plotting system, lattice plot and ggplot2 amongst others. I liked the look of ggplot2 as it allows customisation of figures. I would like to use ggplot2 more often as this is the best way to learn, but I need to grasp the basic syntax first. The following is a basic introduction to making bar charts with ggplot2.
First, set up the R working environment and load the InsectSprays dataset, which contains counts of insects following treatment with different insecticides. Get the sum of all insects for each of the five spray categories and plot as a bar chart:
Draw a simple bar plot
suppressWarnings(require(ggplot2)) # read in data df <- InsectSprays # get sum of all insects by spray df2 <- aggregate(count ~ spray, df, sum) # plot as a bar chart p <- ggplot(df2, aes(x=spray, y=count)) + geom_bar(stat="identity") p