Ch10 Comparisons

10.1 Introduction
10.3 Making visual comparisons
10.4 Comparing group effects graphically
10.5 Comparing rates visually
10.6 Graphics for comparing many subsets
10.7 Graphics principles for comparisons

 

Introduction | 198

data(bank, package="gclus")
bank <- within(bank,
               st <- ifelse(Status==0, "genuine", "forgery"))
c1 <- ggplot(bank, aes(x=Diagonal)) +
             geom_histogram(binwidth=0.2) + facet_grid(st~.)
c2 <- ggplot(bank, aes(x=Right)) +
             geom_histogram(binwidth=0.1) + facet_grid(st~.)
grid.arrange(c1, c2, ncol=2)

 

Making visual comparisons | 202

ggplot(Michelson, aes(x=velocity)) + geom_bar(binwidth = 25) +
       geom_vline(xintercept = 734.5, colour="red",
       linetype = "longdash") +
       xlab("Speed of light in kms/sec (less 299,000)")

 

203

data(Cars93, package="MASS")
c1 <- ggplot(mtcars, aes(mpg)) + geom_bar(fill="blue") +
             xlim(10,50) + xlab("mpg for 32 cars from 1973-4")
c2 <- ggplot(Cars93, aes(MPG.city)) +
             geom_bar(fill="red") +  xlim(10,50) +
             xlab("mpg in city driving for 93 cars from 1993")
grid.arrange(c1, c2, nrow=2)

 

204

ggplot(olives, aes(Area, palmitic, colour=Region)) +
             geom_boxplot() + theme(legend.position="bottom")

 

205

o1 <- ggplot(olives, aes(Region, palmitic, colour = Region)) +
             geom_boxplot() + theme(legend.position = "none")
o2 <- ggplot(olives, aes(x=palmitic, colour = Region)) +
             geom_density() + ylab("density") +
             theme(legend.position = "none")
o3 <- ggplot(olives, aes(x=palmitic, colour = Region)) +
             stat_ecdf() + ylab("cdf") +
             theme(legend.position = "bottom")
grid.arrange(o1, arrangeGrob(o2, o3, nrow=2),
             ncol=2, widths=c(1, 2))

 

206

data(EastIndiesTrade,package="GDAdata")
c1 <- ggplot(EastIndiesTrade, aes(x=Year, y=Exports)) + 
             ylim(0,2000) + geom_line(colour="red", size=2) + 
             geom_line(aes(x=Year, y=Imports),
             colour="yellow", size=2) +  
             geom_ribbon(aes(ymin=Exports, ymax=Imports),
             fill="pink",alpha=0.5) + 
             ylab("Exports(red) and Imports(yellow)")
c2 <- ggplot(EastIndiesTrade, aes(x=Year,
             y=Exports-Imports)) + geom_line(colour="green")
c3 <- ggplot(EastIndiesTrade, aes(x=Year,
             y=(Exports-Imports)/((Exports + Imports)/2))) + 
             geom_line(colour="blue")
grid.arrange(c1, c2, c3, nrow=3)

 

Comparing group effects graphically | 208

data(barley, package="lattice")
ggplot(barley, aes(yield)) + geom_histogram(binwidth=5) +
      ylab("") + facet_wrap(~year, ncol=1)

 

209

c1 <- ggplot(barley, aes(x=variety, y=yield)) +
      geom_point() + ylim(10,70)
barl1 <- barley %>% group_by(variety) %>%
         summarise(N = n(), mean = mean(yield),
         sd = sd(yield), se = sd/sqrt(N))
lims <- aes(ymax = mean + 2*se, ymin=mean - 2*se)
p1 <- ggplot(barl1, aes(x=variety, y=mean)) +
      geom_point() + ylim(10,70) +
      geom_errorbar(lims, width=0.2)
grid.arrange(c1, p1)

 

210

barl2 <- barley %>%
         mutate(Year = factor(year,
                levels = c("1931", "1932"))) %>%
         group_by(site, Year) %>%
         summarise(N = n(), mean = mean(yield),
         sd = sd(yield), se = sd/sqrt(N))
lims <- aes(ymax = mean + 2*se, ymin=mean - 2*se)
ggplot(barl2, aes(colour=Year, x=site, y=mean)) +
       geom_point() + geom_errorbar(lims, width=0.2) +
       ylim(10,70) + theme(legend.position = 'bottom')

 

211

m1 <- lm(yield~site+year+variety, data=barley)
library(coefplot)
coefplot(m1, predictors="variety", lwdOuter=1) + ggtitle("") + 
         ylab("") + xlab("yield difference from Svansota")

 

Comparing rates visually | 212

t1 <- data.frame(Titanic)
t1m <- t1[t1$Sex=="Male",]
xt1 <- xtabs(Freq ~ Class, data=t1m[t1m$Survived=="Yes",])
xt2 <- xtabs(Freq ~ Class, data=t1m)
surv <- xt1/xt2
survS <- (surv*(1-surv)/xt2)^0.5
su <- data.frame(cbind(surv, survS))
su$Class <- rownames(su)
lims <- aes(ymax = surv + 2*survS, ymin=surv - 2*survS)
ggplot(su, aes(x=Class, y=surv)) + geom_point() +
       geom_errorbar(lims, width=0.1, colour="blue") +
       ylim(0,0.5) + ylab("Male survival rate")

 

Graphics for comparing many subsets | 214

data(olives, package="extracat")
ggplot(olives, aes(palmitic, palmitoleic)) +
       geom_point() + facet_wrap(~Area)

 

Graphics for comparing many subsets | 215

ggplot(olives, aes(palmitic, palmitoleic)) +
       geom_point(aes(colour=Area))  +
       theme(legend.position = "bottom")  +
       guides(col = guide_legend(nrow = 2))

 

Graphics principles for comparisons | 217

data(olives, package="extracat")
library(scales)
fs1 <- facetshade(data = olives,
    aes(x = palmitic, y = palmitoleic), f = .~Area)
fs1 + geom_point(colour = alpha("black", 0.05)) +
    geom_point(data = olives, colour = "red") +
    facet_wrap(f=~Area, nrow=3) + theme(legend.position="none")