Professional Documents
Culture Documents
CH 07
CH 07
CH 07
==================
p <- ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point()
====================================
p + annotate("text", x=3, y=48, label="Group 1", family="serif",
fontface="italic", colour="darkred", size=3) +
annotate("text", x=4.5, y=66, label="Group 2", family="serif",
fontface="italic", colour="darkred", size=3)
====================================
p + annotate("text", x=3, y=48, label="Group 1", alpha=.1) + # Normal
geom_text(x=4.5, y=66, label="Group 2", alpha=.1) # Overplotted
====================================
p + annotate("text", x=-Inf, y=Inf, label="Upper left", hjust=-.2, vjust=2) +
annotate("text", x=mean(range(faithful$eruptions)), y=-Inf, vjust=-0.4,
label="Bottom middle")
====================================
# A normal curve
p <- ggplot(data.frame(x=c(-3,3)), aes(x=x)) + stat_function(fun = dnorm)
====================================
p + annotate("text", x=0, y=0.05, parse=TRUE, size=4,
label="'Function: ' * y==frac(1, sqrt(2*pi)) * e^{-x^2/2}")
====================================
library(gcookbook) # For the data set
====================================
library(plyr) # For the ddply() function
hw_means <- ddply(heightweight, "sex", summarise, heightIn=mean(heightIn))
hw_means
sex heightIn
f 60.52613
m 62.06000
p + geom_hline(aes(yintercept=heightIn, colour=sex), data=hw_means,
linetype="dashed", size=1)
====================================
pg <- ggplot(PlantGrowth, aes(x=group, y=weight)) + geom_point()
pg + geom_vline(xintercept = 2)
pg + geom_vline(xintercept = which(levels(PlantGrowth$group)=="ctrl"))
====================================
library(gcookbook) # For the data set
====================================
library(grid)
p + annotate("segment", x=1850, xend=1820, y=-.8, yend=-.95, colour="blue",
size=2, arrow=arrow()) +
annotate("segment", x=1950, xend=1980, y=-.25, yend=-.25,
arrow=arrow(ends="both", angle=90, length=unit(.2,"cm")))
====================================
library(gcookbook) # For the data set
====================================
pg <- PlantGrowth # Make a copy of the PlantGrowth data
pg$hl <- "no" # Set all to "no"
pg$hl[pg$group=="trt2"] <- "yes" # If group is "trt2", set to "yes"
====================================
ggplot(pg, aes(x=group, y=weight, fill=hl)) + geom_boxplot() +
scale_fill_manual(values=c("grey85", "#FFDDCC"), guide=FALSE)
====================================
ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot() +
scale_fill_manual(values=c("grey85", "grey85", "#FFDDCC"), guide=FALSE)
====================================
library(gcookbook) # For the data set
# Take a subset of the cabbage_exp data for this example
ce <- subset(cabbage_exp, Cultivar == "c39")
====================================
ce
====================================
cabbage_exp
====================================
# Bad: dodge width not specified
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge") +
geom_errorbar(aes(ymin=Weight-se, ymax=Weight+se),
position="dodge", width=.2)
====================================
pd <- position_dodge(.3) # Save the dodge spec because we use it repeatedly
# Thinner error bar lines with size=0.25, and larger points with size=2.5
====================================
# The base plot
p <- ggplot(mpg, aes(x=displ, y=hwy)) + geom_point() + facet_grid(. ~ drv)
====================================
# This function returns a data frame with strings representing the regression
# equation, and the r^2 value
# These strings will be treated as R math expressions
lm_labels <- function(dat) {
mod <- lm(hwy ~ displ, data=dat)
formula <- sprintf("italic(y) == %.2f %+.2f * italic(x)",
round(coef(mod)[1], 2), round(coef(mod)[2], 2))
drv formula r2
4 italic(y) == 30.68 -2.88 * italic(x) italic(R^2) == 0.65
f italic(y) == 37.38 -3.60 * italic(x) italic(R^2) == 0.36
r italic(y) == 25.78 -0.92 * italic(x) italic(R^2) == 0.04
====================================
# Find r^2 values for each group
labels <- ddply(mpg, "drv", summarise, r2 = cor(displ, hwy)^2)
labels$r2 <- sprintf("italic(R^2) == %.2f", labels$r2)
==================