Published version on RPubs: http://rpubs.com/crt34/ggplot2
Some recommended ggplot2 Documentation, Resources & Teaching Material:
* http://docs.ggplot2.org/current/index.html
* http://docs.ggplot2.org/current/vignettes/ggplot2-specs.html
* http://docs.ggplot2.org/dev/vignettes/themes.html
* http://colorbrewer2.org
* https://github.com/jennybc/ggplot2-tutorial
* http://zevross.com/blog/2014/08/04/beautiful-plotting-in-r-a-ggplot2-cheatsheet-3/
* http://shinyapps.stat.ubc.ca/r-graph-catalog/
geom_linegeom_bargeom_histogramgeom_areageom_densitygeom_pointboxplotgeom_hexgeom_tilegeom_violinlabsggtitlexlabylabgeom_vlinegeom_hlinegeom_ablinegeom_smoothannotategeom_textgeom_ruggeom_jitterreorderstat_quantilestat_binstat_smoothfacet_gridfacet_grid with theme()facet_wrapexpand_limitsscale_colour_brewer / scale_fill_brewerscale_colour_gradient / scale_fill_gradientscale_size & scale_size_areascale_x_continuous / scale_y_continuousposition = "stack" / position_stackposition = "fill" / position_stackposition = "dodge" / position_dodgetheme_%theme(plot.title=...)theme(axis.title.%=...)theme(axis.text=...)theme(axis.ticks=...)theme(legend.%=...)theme(panel.%=...)“There’s more than one way to skin a cat.”
NOTE: the same ggplot2 output can be constructed with multiple different code!! These examples represent one way of coding the desired output, and uses a more verbose style to aid readability and understanding of the syntax used.
#Install packages
pkgs <- c("ggplot2", "hexbin", "dplyr", "tidyr", "MASS", "gapminder", "RColorBrewer")
install.packages(pkgs, repos = 'http://cran.rstudio.com')
#Load packages
library(ggplot2)
library(hexbin)
library(dplyr)
library(tidyr)
library(MASS)
library(gapminder)
library(RColorBrewer)
#Load data
data(Boston)
data(diamonds)
data(gapminder)
#Load data
data(txhousing)
#Create Texas & Dallas data using dplyr commands
Texas <- txhousing %>%
group_by(city, year) %>%
summarise(annual_sales = sum(sales))
Dallas <- Texas %>%
filter(city=="Dallas")
#Load data
data(msleep)
#Create mammals & mammals_omni data using base R commands
mammals <- msleep[complete.cases(msleep), ]
mammals_omni <- mammals[mammals$vore=="omni", ]
#Load data
data(painters)
#Create painters_tile data using base R & tidyr commands
painters_tile <- painters
painters_tile$name <- rownames(painters_tile)
painters_tile <- gather(painters_tile, variable, metric, Composition:Expression)
#Show Colour Brewer palettes
display.brewer.all()
#Show base R colours
colours()
geom_line - Connected observations, ordered by x variable value#Output: basic single Time Series plot
#Desc: annual sales of houses in Dallas, Texas between 2000-2015
ggplot(data=Dallas, aes(x=year, y=annual_sales)) + geom_line()
#Output: basic multiple Time Series plot based on a group
#Desc: annual sales of houses in different cities in Texas between 2000-2015
ggplot(data=Texas, aes(x=year, y=annual_sales, group=city)) + geom_line()
#Output: multiple Time Series plot based on a group & line colour mapped to specified data variable (& legend turned-off using `show_guide`)
#Desc: annual sales of houses in different cities in Texas between 2000-2015 with individual lines coloured
ggplot(data=Texas, aes(x=year, y=annual_sales, group=city)) + geom_line(aes(colour=city), show_guide=FALSE)
geom_bar - Rectangle bars with bases on x-axisIf you have pre-summarised data, use stat=“identity” to turn off the default summary.
#Output: basic Bar Chart plotting a factor variable on the x-axis against the count of each level
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar()
#Output: basic Bar Chart plotting a factor variable on the x-axis against the count of each level & specifying bar width
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(width=0.5)
#Output: basic Bar Chart plotting a factor variable on the x-axis against the count of each level & specifying fixed bar fill and bar outline formatting
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(fill="grey", colour="blue")
#Output: Bar Chart plotting a factor variable on the x-axis against the count of each level & specifying bar fill aesthetic mapped to a chosen data variable
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(aes(fill=continent))
#Output: basic Bar Chart & specifying a rotated plot
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar() + coord_flip()
#Output: Bar Chart plotting a factor variable on the x-axis against the count of each level & specifying bar fill aesthetic mapped to a chosen data variable
#Desc: number of countries in each continent in the gapminder dataset
ggplot(data=diamonds, aes(x=cut)) + geom_bar(aes(fill=color))
geom_histogram - Bar charts where area of bar is proportional to the continuous x variable#Output: basic Histogram
#EQUIVALENT: ggplot(gapminder, aes(x=lifeExp)) + geom_histogram(aes(y=..count..))
#Desc: distribution of life expectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram()
#Output: basic Density Histogram & specifying y variable as calculated density
#Cross-ref: see geom_density section
#Desc: density distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(y=..density..))
#Output: basic Histogram & specifying fixed binwidth of bars
#Desc: distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(binwidth = 10)
#Output: basic Histogram & specifying fixed bar fill and bar outline formatting
#Desc: distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(fill="gray", colour="navy")
#Output: Histogram & specifying bar fill aesthetic mapped to a chosen data variable
#Desc: distribution of life exectancy for countries in gapminder dataset, with bars coloured depending on their area
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..))
#Output: Histogram & specifying bars weighted by chosen data variable
#Desc: distribution of life exectancy for countries in gapminder dataset, with a weighting applied to bars based on the pop variable
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(weight=pop))
#Output: Histogram & specifying stacked bars split by chosen data variable & each bar area re-calculated to represent a 100% scale
#Desc: relative distributions between bins of the proportion of continents represented/making up each life expectancy value
ggplot(data=gapminder, aes(x=lifeExp, fill=continent)) + geom_histogram(position="fill")
geom_area - Continuous analog of a stacked Bar Chart#Output: basic Area Plot
#Desc: continuous distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_area(stat="bin")
#Output: basic Area Plot & specifying y variable as calculated density
#Cross-ref: see geom_density section
#Desc: continuous distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_area(stat="bin", aes(y=..density..))
#Output: basic Area Plot & specifying fixed area fill and area outline formatting
#Desc: continuous distribution of life exectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_area(stat="bin", fill="steelblue", colour="chartreuse")
geom_density - Smooth density estimate#Output: basic Density Plot
#Desc: density distribution of life expectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_density()
#Output: basic Density Plot & specifying level of line smoothing
#Desc: smoothed density distribution of life expectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_density(adjust=10)
#Output: basic Density Plot & specifying rectangular-calculated line
#Desc: unsmoothed, rectangular-calculated density distribution of life expectancy for countries in gapminder dataset
ggplot(data=gapminder, aes(x=lifeExp)) + geom_density(kernel="rectangular")
#Output: multiple Densities Plot based on chosen data variable & specifying individual density fill mapped by the chosen data variable
#Desc: plot of density distributions of life expectancy by continent, with each plot separately coloured
ggplot(data=gapminder, aes(x=lifeExp)) + geom_density(aes(fill=continent))
#Output: multiple Densities Plot based on chosen data variable & specifying individual density fill mapped by the chosen data variable & level of fill transparency & size of density line
#Desc: plot of density distributions of life expectancy by continent, with each plot separately coloured
ggplot(data=gapminder, aes(x=lifeExp)) + geom_density(aes(fill=continent), alpha=0.4, size=1)
geom_point - Scatterplots & Bubble Charts#Output: basic Scatterplot
#Desc: plot showing the relationship between the composition and expression scores of the artists in the painters dataset
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point()
#Output: Scatterplot & specifying the point colour to be mapped to a chosen data variable
#Desc: plot showing the relationship between the composition and expression scores of the artists in the painters dataset, with points individually coloured by the school of the artist represented by that point
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(aes(colour=School))
#Output: Scatterplot & specifying the point shape to be mapped to a chosen data variable & a fixed point size
#Desc: plot showing the relationship between the composition and expression scores of the artists in the painters dataset, with shape of each point determined by the school of the artist represented by that point
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(aes(shape=School, size = 5))
#Output: Scatterplot & specifying fixed point colour & fixed fill transparency & a fixed point size
#Desc: plot showing the relationship between the composition and expression scores of the artists in the painters dataset
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(colour="chartreuse", alpha=0.7, size=5)
#Output: Scatterplot & specifying one geom layer with point colour to be mapped to a chosen data variable & an additional geom layer with fixed point colour
#Desc: plot showing the relationship between the composition and expression scores of the artists in the painters dataset, with points individually coloured (around a standard coloured core) by the school of the artist represented by that point
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(aes(colour=School, size = 10)) + geom_point(colour="grey")
#Output: basic Bubble Chart, i.e. Scatterplot & scale_size to map size of geom radius to a chosen data variable
#Desc: chart showing the relationship between the composition and expression scores of the artists in the painters dataset, with relative size of each point determined by the drawing score of the artist represented by that point
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point(aes(size=Drawing, colour=School, alpha=0.4)) + scale_size(range=c(1, 15))
boxplot - Box and Whiskers plotThe upper and lower “hinges” correspond to the first and third quartiles (the 25th and 75th percentiles).
#Output: basic Boxplot
#Desc: plot showing the distribution of the expression scores of artists in the painters dataset, grouped by school
ggplot(data=painters, aes(x=factor(School), y=Expression)) + geom_boxplot()
#Output: basic Boxplot & specifying a rotated layout
#Desc: plot showing the distribution of the expression scores of artists in the painters dataset, grouped by school
ggplot(data=painters, aes(x=factor(School), y=Expression)) + geom_boxplot() + coord_flip()
#Output: basic Boxplot & specifying fixed outlier colour
#Desc: plot showing the distribution of the expression scores of artists in the painters dataset, grouped by school
ggplot(data=painters, aes(x=factor(School), y=Expression)) + geom_boxplot(outlier.color="tomato")
#Output: basic Boxplot & specifying fixed boxplot fill and boxplot outline colour
#Desc: plot showing the distribution of the expression scores of artists in the painters dataset, grouped by school
ggplot(data=painters, aes(x=factor(School), y=Expression)) + geom_boxplot(fill="papayawhip", colour="khaki4")
#Output: basic Boxplot & specifying boxplot fill to be mapped to a chosen data variable
#Desc: plot showing the distribution of the expression scores of artists in the painters dataset, grouped and coloured by school
ggplot(data=painters, aes(x=factor(School), y=Expression)) + geom_boxplot(aes(fill=School))
geom_hex - Hexagon binning#Output: basic HexBin plot
#Desc: plot showing the counts of houses in Boston with each combination of medv (house value) and rm (no. of rooms) in the Boston dataset
ggplot(data=Boston, aes(x=rm, y=medv)) + geom_hex()
#Output: basic HexBin plot & specifying fixed size of hex bins
#Desc: plot showing the counts of houses in Boston with each combination of medv (house value) and rm (no. of rooms) in the Boston dataset
ggplot(data=Boston, aes(x=rm, y=medv)) + geom_hex(bins=15)
geom_tile - Tile plane with rectangles (Heatmap)#Output: basic Tile plane plot
#Desc: heatmap showing the colour, composition, drawing, and expression scores for each artist in the painters dataset, represented by a particular shade from a colour range
ggplot(data=painters_tile, aes(x=variable, y=name)) + geom_tile(aes(fill=metric))
#Output: basic Tile plane plot & specifying the colour range used to represent tile values
#Desc: heatmap showing the colour, composition, drawing, and expression scores for each artist in the painters dataset, represented by a particular shade from the specified colour range
ggplot(data=painters_tile, aes(x=variable, y=name)) + geom_tile(aes(fill=metric)) + scale_fill_gradient(low="lightskyblue", high="magenta")
geom_violin - Boxplot with rotated kernel density of data at the different values#Output: basic Violin plot
#Desc: plot showing the distribution of life expectancy of countries in the gapminder dataset grouped by continent
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin()
#Output: basic Violin plot & specifying fixed violin fill and violin outline colour
#Desc: plot showing the distribution of life expectancy of countries in the gapminder dataset grouped by continent
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin(fill="yellow", colour="yellow4")
#Output: basic Violin plot & specifying a rotated plot
#Desc: plot showing the distribution of life expectancy of countries in the gapminder dataset grouped by continent
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin() + coord_flip()
#Output: basic Violin plot & specifying geom bandwidth for altered density fit (default =1)
#Desc: plot showing the distribution of life expectancy of countries in the gapminder dataset grouped by continent
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin(adjust=0.2)
#Output: basic Violin plot & specifying violin fill to be mapped to a chosen data variable
#Desc: plot showing the distribution of life expectancy of countries in the gapminder dataset grouped by continent
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin(aes(fill=continent))
labs - Set plot title, axis labels & legend titles#Specifying plot title, x-axis, y-axis, and legend labels (using either 'fill=' or 'colour=' depending on the geom aesthetics used)
ggplot(gapminder, aes(factor(continent))) + geom_bar(aes(fill=continent)) + labs(title="<Title added>", x="<x-axis label added>", y="<y-axis label added>", fill="<legend label added>")
ggtitle - Set plot title#Specifying just the plot title
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(aes(fill=continent)) + ggtitle("<Title added>")
xlab - Set x-axis label#Specifying just the x-axis label
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(aes(fill=continent)) + xlab("<x-axis label added>")
ylab - Set y-axis label#Specifying just the y-axis label
ggplot(data=gapminder, aes(x=factor(continent))) + geom_bar(aes(fill=continent)) + ylab("<y-axis label added>")
geom_vline - Annotate plot with vertical lines#add vertical line specifying fixed xintercept
ggplot(gapminder, aes(x=lifeExp)) + geom_area(stat="bin") + geom_vline(aes(xintercept = 60))
#add vertical line specifying calculated xintercept
ggplot(gapminder, aes(x=lifeExp)) + geom_area(stat="bin") + geom_vline(aes(xintercept = mean(gapminder$lifeExp)))
#add vertical line specifying calculated xintercept & fixed aesthetics of line
ggplot(gapminder, aes(x=lifeExp)) + geom_area(stat="bin") + geom_vline(aes(xintercept = mean(gapminder$lifeExp)), colour="red", linetype="dashed")
geom_hline - Annotate plot with horizontal lines#Add horizontal line specifying fixed yintercept
ggplot(data=painters, aes(x=School, y=Drawing)) + geom_bar(stat="identity") + geom_hline(aes(yintercept=75))
geom_abline - Annotate plot with line with specified slope & intercept#abline
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + geom_abline(intercept=-2.5, slope=1.0)
geom_smooth - Annotate plot with fitted ‘smoother’ line#Add soother calculated using specified method & specifying the se confidence interval not to be displayed
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + geom_smooth(method="lm", se=F)
#Add soother calculated using specified method & specifying the se confidence interval to be displayed
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + geom_smooth(method="lm", se=T)
annotate - Create an Annotation layer#Annotate Scatterplot with fixed line specifying plot parameters
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point() + annotate("segment", x=0, xend=20, y=0, yend=20)
#Annotate Scatterplot with manual text geom specifying plot parameters
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point() + annotate("text", x=9, y=13.5, label="Rogue point")
#Annotate Scatterplot with manual shaded rectangular area & transparency
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point() + annotate("rect", xmin=3, xmax=7, ymin=3, ymax=7, alpha=0.3)
geom_text - Textual Annotations#Add labels to Bar chart mapping text geoms to data variable & specifying text geom colour & position using relative vertical paramter
ggplot(data=mammals_omni, aes(x=name, y=sleep_total)) + geom_bar(stat="identity") + geom_text(aes(label=name), colour="tomato", vjust=-0.5)
#Add labels to Scatterplot mapping text geoms to data variable
ggplot(data=mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() + geom_text()
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of text geom using relative horizontal and vertical parameters
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() + geom_text(hjust=0, vjust=-0.5)
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of geom using relative horizontal and vertical parameters & specifying size of geom
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() + geom_text(hjust=0, vjust=-0.5, size=5)
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of geom using relative horizontal and vertical parameters & specifying size of geom as an aesthetic mapped to a data variable
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() +geom_text(hjust=0, vjust=-0.5, aes(size=awake))
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of geom using relative horizontal and vertical parameters & specifying colour of geom as an aesthetic mapped to a data variable
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() +geom_text(hjust=0, vjust=-0.5, aes(colour=vore))
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of geom using relative horizontal and vertical parameters & specifying angle of label
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() +geom_text(hjust=0, vjust=-0.5, angle=20)
#Add labels to Scatterplot mapping text geoms to data variable & specifying position of geom using relative horizontal and vertical parameters & specifying label font
ggplot(mammals, aes(sleep_rem, sleep_total, label=mammals$name)) + geom_point() +geom_text(hjust=0, vjust=-0.5, fontface=2)
geom_rug - Annotate plot with axes tick marks denoting position of data#Add rugs to Scatterplot on x and y axes
ggplot(data=mammals, aes(x=sleep_rem, y=sleep_total)) + geom_point() + geom_rug()
#Add rugs to Scatterplot on all sides of plot using "trbl" string
ggplot(data=mammals, aes(x=sleep_rem, y=sleep_total)) + geom_point() + geom_rug(sides="trbl")
#Add rugs to Scatterplot on top and right sides of plot by specifying "t" and "r" elements of "trbl" string
ggplot(data=mammals, aes(x=sleep_rem, y=sleep_total)) + geom_point() + geom_rug(sides="tr")
geom_jitter - Add geoms arranged in a jittered style to reduce overplotting & Stripplot#Boxplot & overlay of data points arranged in a jittered style
ggplot(painters, aes(x=factor(School), y=Expression)) + geom_boxplot() + geom_jitter()
#Boxplot & overlay of data points arranged in a jittered style & adjust width and transparency
ggplot(painters, aes(x=factor(School), y=Expression)) + geom_boxplot() + geom_jitter(width=0.1, alpha=0.5)
#Violin plot & overlay of data points arranged in a jittered style & adjust width and transparency
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_violin() + geom_jitter(width=1, alpha=0.5)
#Stripplot plot i.e. single geom layer data points arranged in a jittered style & adjust width and transparency
ggplot(data=gapminder, aes(x=continent, y=lifeExp)) + geom_jitter(width=0.8, alpha=0.4)
reorder - Override the current order of variable values by re-basing on values of a specified variable#Note for character variables, plot order is based alphabetically by default, e.g.
ggplot(data=mammals_omni, aes(x=name, y=sleep_total)) + geom_bar(stat="identity")
#Specify Bar chart x variable to be plotted based on the numeric values of a specified data variable
ggplot(data=mammals_omni, aes(x=reorder(name, sleep_total), y=sleep_total)) + geom_bar(stat="identity")
#Specify bar chart x variable to be plotted based on the numeric values of a specified data variable & specify descending ordering ("-")
ggplot(data=mammals_omni, aes(x=reorder(name, -sleep_total), y=sleep_total)) + geom_bar(stat="identity")
stat_quantile - Annotate plot with continuous quantiles#Basic Scatterplot & adding quantiles of y (relative to x)
ggplot(diamonds, aes(carat, price)) + geom_point() + stat_quantile()
stat_bin - (see geom_histogram)#Basic Histogram & specifying width of bin
ggplot(data=diamonds, aes(x=price)) + geom_histogram() + stat_bin(binwidth=500)
#Basic Histogram & specifying no. of bins
ggplot(data=diamonds, aes(x=price)) + geom_histogram() + stat_bin(bins=10)
#Basic Histogram & specifying bins calculated by seq() function (<starting value>, <end value>, <total no. of bins>)
ggplot(data=diamonds, aes(x=price)) + geom_histogram() + stat_bin(breaks=seq(300, 19000, length.out=20))
stat_smooth - (see geom_smooth)#Add stat soother calculated using specified method & specifying the se confidence interval not to be displayed
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + stat_smooth(method="lm", se=F)
#Add stat soother calculated using specified method & specifying the se confidence interval to be displayed
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + stat_smooth(method="lm", se=T)
facet_grid - Display subsets of the dataset in different panels#Subset data with facets
#
ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + facet_grid(~continent)
#Scatterplot with trendline with facets
#
ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + geom_smooth(method="lm", se=F) + facet_grid(~continent)
#Subset data with facets
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point() + facet_grid(~School)
#Histogram with facets
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram() + facet_grid(~continent)
facet_grid with theme - Altering visual elements of Facets#Set facet header colour
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram() + facet_grid(~continent) + theme(strip.background=element_rect(fill="turquoise"))
#Set facet header text
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram() + facet_grid(~continent) + theme(strip.text.x = element_text(size=15))
#Set margins around facet panels
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram() + facet_grid(~continent) + theme(panel.margin=unit(1, "cm"))
facet_wrap - Same as facet_grid except able to wrap a 1d ribbon of panels into 2d#Scatterplot with facets & adding fitted smoother line
ggplot(gapminder, aes(x=gdpPercap, y=lifeExp)) + geom_point() + geom_smooth(method="lm", se=F) + facet_wrap(~continent)
expand_limits - Expand the plot limits#Set x and y axis
ggplot(painters, aes(x=Drawing)) + geom_bar(aes(fill=School)) + expand_limits(x=0, y=0)
scale_colour_brewer / scale_fill_brewer - Use colour scales from colorbrewer.orgSee display.brewer.all() for palettes.
#segment points by factor variable using colour brewer
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(aes(colour=School)) +scale_colour_brewer(palette="Accent")
#Histogram with facets & specifying fill colours from a brewer palette
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=continent)) + facet_grid(~continent) +scale_fill_brewer(palette="Set3")
scale_colour_gradient / scale_fill_gradient - Use smooth gradient between two colours#segment points by factor variable using colour brewer
ggplot(painters, aes(x=Composition, y=Expression)) + geom_point(aes(colour=Drawing)) +scale_colour_gradient(low="chartreuse", high="blue")
#Histogram with facets
ggplot(gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + facet_grid(~continent) + scale_fill_gradient(low="bisque", high="coral")
scale_size & scale_size_area - Transform geom size based on specified parameters or mapping#Bubble Chart, i.e. Scatterplot & scale_size to map radius size of geom to a specified data variable
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point(aes(size=Drawing, colour=School, alpha=0.4)) + scale_size(range=c(1, 15))
#Bubble Chart, i.e. Scatterplot & scale_size to map area size of geom to a specified data variable
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point(aes(size=Drawing, colour=School, alpha=0.4)) + scale_size_area()
scale_x_continuous / scale_y_continuous - Continuous position scales (x & y)#Modify x-axis limits
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + stat_smooth(method="lm", se=F) + scale_x_continuous(limits=c(15, 20))
#Modify y-axis limits
ggplot(data=painters, aes(x=Composition, y=Expression)) + geom_point() + stat_smooth(method="lm", se=F) + scale_y_continuous(limits=c(10, 20))
position = "stack - Stack objects on top of each other#Bar chart & adjusting objects to be stacked (Note: stacking is the default behaviour for most area plots so may not be necessary to specify)
ggplot(data=mammals, aes(x=vore)) + geom_bar(aes(fill=conservation), position="stack")
position = "fill" - Stack objects on top of each other and standardise to have equal height#Bar chart & adjusting objects to be stacked and height standardised
ggplot(data=mammals, aes(x=vore)) + geom_bar(aes(fill=conservation), position="fill")
position = "dodge" - Adjust geom position by dodging overlaps to the side#Bar chart specifying the geom bars position adjusted to dodge overlaps
ggplot(data=mammals, aes(x=vore)) + geom_bar(aes(fill=conservation), position="dodge")
Refer to the ggplot2 documentation on theme for more details: http://docs.ggplot2.org/0.9.2.1/theme.html
theme_% - Control non-data components of a plot#Apply a set theme with pre-defined theme elements (default is theme_grey)
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + theme_bw()
theme(plot.title=...) - Control plot title components#Add plot title & specify title size using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + labs(title="<Title added>") + theme(plot.title=element_text(size=20))
#Add plot title & specify title colour using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + labs(title="<Title added>") + theme(plot.title=element_text(colour="forestgreen"))
#Add plot title & specify title size using theme() based on relative size to other labels
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + labs(title="<Title added>") +theme(plot.title=element_text(size=rel(3)))
theme(axis.title.%=...) - Control axes label components#Specify axes labels using theme() to change label colour
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + labs(x="<x-axis label added>", y="<y-axis label added>") + theme(axis.title.x=element_text(colour="green")) + theme(axis.title.y=element_text(colour="green"))
#Specify blank axes labels using theme() to remove
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + theme(axis.title.x=element_blank()) + theme(axis.title.y=element_blank())
theme(axis.text=...) - Control axes marker text components#Specify axes marker text using theme() to make text bold & change size & colour
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + theme(axis.text=element_text(face="bold", size=15, colour="darkorchid"))
theme(axis.ticks=...) - Control axes tick mark components#Specify size of axes tick marks using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram() + theme(axis.ticks=element_line(size=3))
theme(legend.%=...) - Control legend components#Add specific title of legend & specify font size using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + labs(fill="<Add Legend>") + theme(legend.title=element_text(size=15))
#Specify size of legend key using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + theme(legend.key.size = unit(1.5, "cm"))
#Remove legend using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + theme(legend.position="none")
#Position legend underneath plot using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + theme(legend.position="bottom")
theme(panel.%=...) - Control panel components#Specify colour of background panel using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + theme(panel.background=element_rect(fill="honeydew"))
#Specify panel grid lines colour & linetype using theme()
ggplot(data=gapminder, aes(x=lifeExp)) + geom_histogram(aes(fill=..count..)) + theme(panel.grid.major=element_line(colour="navy", linetype="dashed"))
Happy Coding!!