# Generate data c <- ggplot(mtcars, aes(factor(cyl))) c + geom_bar() c + geom_bar(width=.5) c + geom_bar() + coord_flip() c + geom_bar(fill="white", colour="darkgreen") # Use qplot qplot(factor(cyl), data=mtcars, geom="bar") qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(cyl)) # Stacked bar charts qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(vs)) qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear)) # Stacked bar charts are easy in ggplot2, but not effective visually, # particularly when there are many different things being stacked ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar() ggplot(diamonds, aes(color, fill=cut)) + geom_bar() + coord_flip() # Faceting is a good alternative: ggplot(diamonds, aes(clarity)) + geom_bar() + facet_wrap(~ cut) # If the x axis is ordered, using a line instead of bars is another # possibility: ggplot(diamonds, aes(clarity)) + geom_freqpoly(aes(group = cut, colour = cut)) # Dodged bar charts ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar(position="dodge") # compare with ggplot(diamonds, aes(cut, fill=cut)) + geom_bar() + facet_grid(. ~ clarity) # But again, probably better to use frequency polygons instead: ggplot(diamonds, aes(clarity, colour=cut)) + geom_freqpoly(aes(group = cut)) # Often we don't want the height of the bar to represent the # count of observations, but the sum of some other variable. # For example, the following plot shows the number of diamonds # of each colour qplot(color, data=diamonds, geom="bar") # If, however, we want to see the total number of carats in each colour # we need to weight by the carat variable qplot(color, data=diamonds, geom="bar", weight=carat, ylab="carat") # A bar chart used to display means meanprice <- tapply(diamonds$price, diamonds$cut, mean) cut <- factor(levels(diamonds$cut), levels = levels(diamonds$cut)) qplot(cut, meanprice) qplot(cut, meanprice, geom="bar", stat="identity") qplot(cut, meanprice, geom="bar", stat="identity", fill = I("grey50")) # Another stacked bar chart example k <- ggplot(mpg, aes(manufacturer, fill=class)) k + geom_bar() # Use scales to change aesthetics defaults k + geom_bar() + scale_fill_brewer() k + geom_bar() + scale_fill_grey() # To change plot order of class varible # use factor() to change order of levels mpg$class <- factor(mpg$class, levels = c("midsize", "minivan", "suv", "compact", "2seater", "subcompact", "pickup")) m <- ggplot(mpg, aes(manufacturer, fill=class)) m + geom_bar()
Run the code above in your browser using DataCamp Workspace