These are a series of examples taken from the book R Graphics Cookbook

Bar Graphs

Note that stat="identity" option ensures that the y axis represents the variable value specified. geom_text(label=Weight) adds labels to each bar

ggplot(pg_mean,aes(x=group,y=weight)) + geom_bar(stat="identity") + geom_text(aes(label=weight),vjust=1.5,color="white")

To make a bar-graph of counts, simply remove the stat="identity" option. The y variable is not needed

ggplot(diamonds,aes(x=carat)) + geom_bar() 
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Continuous values can be used instead of categoricals on the x-axis. fill="lightblue" provides a light blue fill, and colour="black" ensures a black outline.

ggplot(BOD,aes(x=factor(Time),y=demand)) + geom_bar(stat="identity",fill="lightblue",colour="black") + geom_text(aes(label=demand),vjust=-0.2)

A stacked graph can be generated using a second variable. The guides(fill=guide_legend(reverse=TRUE)) reverses the legend so it is aligned with how the colors are stacked. Alternately, one can specify order=desc(Cultivar) within ggplot()

ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar)) + geom_bar(stat="identity",colour="black") + guides(fill=guide_legend(reverse=TRUE)) 

By including the position="dodge" option within geom_bar above, you can have the bars side by side. The scale_fill_brewer(palette="Pastel1") option provides a better looking color set. For the labels, we additionally need to specify positon=position_dodge(.9) for grouped bar charts, where 0.9 is the default value for the dodge. Further, size=3 is specified in order to ensure that the labels fit within the narrow bands

ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar)) + geom_bar(position="dodge",stat="identity",colour="black") + scale_fill_brewer(palette="Pastel1") + geom_text(aes(label=Weight),vjust=1.5, position=position_dodge(.9),size=3)
## ymax not defined: adjusting position using y instead

A proportional bar graph can be generated by

Different colors for bars in the graph can be used.

upc <- subset(uspopchange,rank(Change)>40)
ggplot(upc,aes(x=reorder(Abb,Change),y=Change,fill=Region))+
  geom_bar(stat="identity",colour="black")+
  scale_fill_manual(values=c("#669933","#FFCC66")) +
  xlab("State")

A proportional bar graphs can be created as follows

ce <- ddply(cabbage_exp,"Date",transform, percent_weight=Weight/sum(Weight)*100)
ggplot(ce, aes(x=Date, y=percent_weight,fill=Cultivar)) + geom_bar(stat="identity", colour="black") + guides(fill=guide_legend(reverse=TRUE))+ scale_fill_brewer(palette="Pastel1")

A Cleaveland dot plot can be generated as follows

tophit <- tophitters2001[1:25,] # Take top 25 of the top hitters
ggplot(tophit,aes(x=avg,y=reorder(name,avg))) + geom_point(size=3) + theme_bw()+
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_line(colour="grey60",linetype="dashed"))

Line Plots

Basic line plot. geom_point() is used to highlight points

ggplot(BOD,aes(x=Time,y=demand)) + geom_line() + geom_point()

If you want the y-axis in the log-scale

ggplot(worldpop,aes(x=Year,y=Population)) + geom_line() + scale_y_log10()

For plotting multiple lines, you could use one of the following approaches

tg <- ddply(ToothGrowth, c("supp","dose"),summarise, length=mean(len) )
ggplot(tg,aes(x=dose,y=length,colour=supp))+geom_line()

Alternately

ggplot(tg,aes(x=dose,y=length,colour=supp))+geom_line(linetype="dashed",size=1.5) +geom_point(shape=22,size=3,fill="white")

An example of a plot with shaded area using geom_area()

sunspotyear <- data.frame(
  Year = as.numeric(time(sunspot.year)),
  Sunspots = as.numeric(sunspot.year)
  )

ggplot(sunspotyear,aes(x=Year,y=Sunspots)) + geom_area(fill="blue",alpha=0.2) + geom_line()

A simple way to create a stacked area plot

ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup)) + geom_area() + guides(fill=guide_legend(reverse=TRUE)) 

Prettier version of the plot above

ggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup,order=desc(AgeGroup))) + geom_area(colour=NA,alpha=.4) + scale_fill_brewer(palette="Blues") + geom_line(position="stack",size=.2)
## ymax not defined: adjusting position using y instead

A proportional stacked graph

uspopage_prop <- ddply(uspopage,"Year",transform, Percent = Thousands/sum(Thousands)*100)
ggplot(uspopage_prop,aes(x=Year,y=Percent,fill=AgeGroup))+
  geom_area(colour="black",size=.2,alpha=.4) +
  scale_fill_brewer(palette="Blues",breaks=rev(levels(uspopage$AgeGroup)))

Add a confidence region using geom_ribbon()

clim <- subset(climate, Source=="Berkeley", select=c("Year","Anomaly10y","Unc10y"))
ggplot(clim,aes(x=Year, y=Anomaly10y))+ geom_ribbon(aes(ymin=Anomaly10y-Unc10y,ymax = Anomaly10y+Unc10y),alpha=0.2) + geom_line()

Scatter Plots

One can add a regression line to a scatter plot

sp <- ggplot(heightweight,aes(x=ageYear,y=heightIn))
sp + geom_point() + stat_smooth(method=lm,level=0.99)

If you don’t want to add the confidence lines

sp + geom_point() + stat_smooth(method=lm,se=FALSE)

Change the point color to gray and fix a loess. Note that the default for stat_smooth() if you don’t specify anything is a loess

sp + geom_point(colour="grey60") + stat_smooth(method=loess)

You can further split the scatter plots by a grouping variable, and add separate loess fits for each group

ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point() + scale_colour_brewer(palette="Set1") + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

### Histograms

Simple histogram

ggplot(faithful, aes(x=waiting)) + geom_histogram()
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

The above gives the same distribution as geom_bar() in ggplot(faithful, aes(x=waiting)) + geom_bar(binwidth=5,fill="white",colour="black")

Add a bin-size to the histogram

binsize <- diff(range(faithful$waiting/15))
ggplot(faithful, aes(x=waiting)) + geom_histogram(binwidth=binsize,fill="white",colour="black")

Overlay a density curve with a distribution. The ..density.. is used to scale down the histogram to the level of the density curve

ggplot(faithful, aes(x=waiting, y=..density..)) + geom_histogram(fill="cornsilk",colour="grey60",size=.2)+geom_density() + xlim(30,105) 
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Multiple density curves can be generated on grouped data. Note that alpga=.3 ensures transparency. If you change fill=smoke to colour=smoke, then you will see a density plot for each group without shaded areas

## [1] "0" "1"
ggplot(birthwt1,aes(x=bwt,fill=smoke)) + geom_density(alpha=.3)

Box plot with means. Note that shape=23 is responsible for the diamond shape for the mean.

ggplot(birthwt, aes(x=factor(race),y=bwt)) + geom_boxplot() + stat_summary(fun.y="mean",geom="point",shape=23,size=3,fill="white")