Created
August 5, 2013 18:41
-
-
Save aaronsaunders/6158299 to your computer and use it in GitHub Desktop.
ggplot tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ggplot2 implements the idea of a "grammar of graphics". The grammar implemented | |
# by ggplot2 could be summarized as follows: | |
# | |
# plot: coord, scale, facet(?) + layers | |
# layer: data mapping stat geom position? | |
# | |
# A plot is defined by a coordinate system (coord), one or more scales (scale), an | |
# optional faceting specification (facet), and one or more layers (layer). A layer | |
# is defined as an R data frame (data), a specification mapping columns of that | |
# frame into aesthetic properties (mapping), a statistical approach to summarize | |
# the rows of that frame (stat), a geometric object to visually represent that | |
# summary (geom), and an optional position adjustment to move overlapping | |
# geometric objects out of their way (position). | |
# | |
# axes: breaks & minor breaks are "ticks" | |
# limits | |
# name: axis title | |
# geom_bar(aes(order = desc(cut))) | |
ggplot(df.topotu, aes(x=OTU, y=prop * 100, fill=core) ) + geom_point() + | |
xlab(paste("Top", n, "OTUs in rank order")) + | |
ylab("percent abundance") + | |
ylim(0, 100) + | |
xlim(1, 1000) | |
geom_histogram(mapping = NULL, data = NULL, stat = "bin", position = "stack", ...) | |
geom_histogram(binwidth = 1) # freq | |
geom_histogram(aes(weight = votes)) # count | |
geom_bar() | |
w <- ggplot(diamonds, aes(clarity, fill = cut)) | |
w + geom_bar() | |
# ORDER | |
w + geom_bar(aes(order = desc(cut))) # order factors in descending order | |
ggplot(diamonds, aes(clarity, fill = cut, order = -as.numeric(cut))) + | |
+ geom_bar() | |
geom_point() | |
geom_jitter() | |
geom_line() | |
geom_line(aes(group = Subject)) # The group aesthetic maps a different line for each subject | |
library(nlme) | |
ggplot(Oxboys, aes(age, height, color= Subject)) + | |
geom_point(size = 1) + | |
geom_smooth(aes(group = Subject, color = Subject), method = "lm", se = FALSE) | |
geom_abline() # draws a line defined by slope and y-axis intercept. | |
geom_boxplot(mepping, data, stat = "boxplot", | |
outlier.colour = "black", outlier.shape = 16, outlier.size = 2, | |
notch = FALSE, notchwidth = 0.5, ...) | |
# http://docs.ggplot2.org/0.9.3.1/geom_boxplot.html | |
# remember that the x axis must be discrete/factor | |
geom_boxplot(aes(fill = factor(cyl))) # colors box | |
geom_boxplot(aes(color = factor(cyl))) # colors lines of box & whiskers | |
geom_boxplot(outlier.shape=3, outlier.size=1 ) # outlier sm. crosses | |
geom_boxplot() + coord_flip() # horizontal | |
geom_ribbon(aes(ymin=mean-sd, ymax=mean+sd), fill= "grey70") | |
####################################### | |
# scale axes | |
# set ticks | |
scale_y_log10(breaks=c(0.01, 0.1, 1, 10)) | |
######################################## | |
# facets | |
facet_grid(facets, margins = FALSE, scales = "fixed", space = "fixed", shrink = TRUE, | |
labeller = "label_value", as.table = TRUE, drop = TRUE) | |
facet_wrap(facets, nrow = NULL, ncol = NULL, scales = "fixed", shrink = TRUE, as.table = TRUE, | |
drop = TRUE) | |
######################################## | |
# theme() | |
# rotate axis labels | |
theme(axis.text.x = element_text(angle = 90, hjust = 1)) + | |
# no axis labels | |
theme(axis.text.y = element_blank()) | |
# https://github.com/hadley/ggplot2/wiki/Legend-Attributes | |
theme() | |
# http://docs.ggplot2.org/current/theme.html | |
axis.title = element_blank() | |
strip.text = element_text(size=18) | |
axis.line = theme_blank(), | |
axis.text.x = theme_blank(), | |
axis.text.y = theme_blank(), | |
axis.ticks = theme_blank(), | |
axis.title.x = theme_blank(), | |
axis.title.y = theme_blank(), | |
axis.ticks.length = unit(0, "lines"), | |
axis.ticks.margin = unit(0, "lines"), | |
legend.text = element_text(size = 12) | |
legend.title = element_text(size = 14) | |
legend.position = "none" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df.a <- melt(df.otu, | |
id.vars="OTU", # cols to keep | |
variable.name="samples", # colname variables (colnames in wide) | |
value.name="prop" # colname for values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment