4.1 Introduction
4.3 Nominal data—no fixed category order
4.4 Ordinal data—fixed category order
4.5 Discrete data—counts and integers
Introduction | 55
b1 <- ggplot(btw9s, aes(Bundesland, Voters/1000000)) + geom_bar(stat="identity") + ylab("Voters (millions)") b2 <- ggplot(btw9s, aes(reorder(Bundesland, -Voters), Voters/1000000)) + geom_bar(stat="identity") + xlab("Bundesland") + ylab("Voters (millions)") b3 <- ggplot(btw9s, aes(State1, Voters/1000000)) + geom_bar(stat="identity") + xlab("Bundesland") + ylab("Voters (millions)") grid.arrange(b1, b2, b3)
Nominal data—no fixed category order | 57
data(Fleiss93, package="meta") Fleiss93 <- within(Fleiss93, { total <- n.e + n.c st <- reorder(study, -(total)) }) ggplot(Fleiss93, aes(st, total)) + geom_bar(stat="identity") + xlab("") + ylab("") + ylim(0,20000)
58
data(anorexia, package="MASS") ggplot(anorexia, aes(Treat)) + geom_bar() + xlab("Treatment")
59
Titanic1 <- data.frame(Titanic) p <- ggplot(Titanic1, aes(weight=Freq)) + ylab("") + ylim(0,2250) cs <- p + aes(Class) + geom_bar(fill="blue") sx <- p + aes(Sex) + geom_bar(fill="green") ag <- p + aes(Age) + geom_bar(fill="tan2") su <- p + aes(Survived) + geom_bar(fill="red") grid.arrange(cs, sx, ag, su, nrow=1, widths=c(3, 2, 2, 2))
61
Party <- c("Fine Gael", "Labour", "Fianna Fail", "Sinn Fein", "Indeps", "Green", "Don't know") nos <- c(181, 51, 171, 119, 91, 4, 368) IrOP <- data.frame(Party, nos) IrOP <- within(IrOP, { percwith <- nos/sum(nos) percnot <- nos/sum(nos[-7])}) par(mfrow=c(2,1), mar = c(2.1, 2.1, 2.1, 2.1)) with(IrOP, pie(percwith, labels=Party, clockwise=TRUE, col=c("blue", "red", "darkgreen", "black", "grey", "lightgreen", "white"), radius=1)) with(IrOP, pie(percnot[-7], labels=Party, clockwise=TRUE, col=c("blue", "red", "darkgreen", "black", "grey", "lightgreen"), radius=1))
Ordinal data-fixed category order | 62
data("BEPS", package="effects") a1 <- ggplot(BEPS, aes(factor(Hague))) + geom_bar(fill="blue") + ylab("") + xlab("Hague (Conservative)") + ylim(0, 900) a2 <- ggplot(BEPS, aes(factor(Blair))) + geom_bar(fill="red") + ylab("") + xlab("Blair (Labour)") + ylim(0, 900) a3 <- ggplot(BEPS, aes(factor(Kennedy))) + geom_bar(fill="yellow") + ylab("") + xlab("Kennedy (Liberal)") + ylim(0, 900) grid.arrange(a1, a2, a3, nrow=1)
63
b1 <- ggplot(BEPS, aes(factor(political.knowledge))) + geom_bar(fill="tan2") + coord_flip() + ylab("") + xlab("Knowledge of policies on Europe") b2 <- ggplot(BEPS, aes(factor(Europe))) + geom_bar(fill="lightgreen") + ylab("") + xlab("Attitudes to European integration") grid.arrange(b1, b2, nrow=1, widths=c(4, 8))
64
data(survey, package="MASS") s1 <- ggplot(survey, aes(Sex)) + geom_bar() + ylab("") s2 <- ggplot(survey, aes(W.Hnd)) + geom_bar() + xlab("Writing hand") + ylab("") s3 <- ggplot(survey, aes(Fold)) + geom_bar() + xlab("Folding arms: arm on top") + ylab("") s4 <- ggplot(survey, aes(Clap)) + geom_bar() + xlab("Clapping: hand on top") + ylab("") survey <- within(survey, ExerN <- factor(Exer, levels=c("None", "Some", "Freq"))) s5 <- ggplot(survey, aes(ExerN)) + geom_bar() + xlab("Exercise") + ylab("") s6 <- ggplot(survey, aes(M.I)) + geom_bar() + xlab("Height units") + ylab("") survey <- within(survey, SmokeN <- factor(Smoke, levels=c("Never", "Occas", "Regul", "Heavy"))) s7 <- ggplot(survey, aes(SmokeN)) + geom_bar() + xlab("Smoking") + ylab("") grid.arrange(s1, s2, s3, s4, s5, s6, s7, ncol=3)
Discrete data—counts and integers | 66
data(VonBort, package="vcd") ggplot(VonBort, aes(x=factor(deaths))) + geom_bar() + xlab("Deaths by horse kick")
67
data(VonBort, package="vcd") h1 <- ggplot(VonBort[VonBort$year=="1891", ], aes(x=factor(deaths))) + geom_bar() h2 <- ggplot(VonBort[VonBort$year=="1891", ], aes(x=factor(deaths, levels=seq(0, 4)))) + geom_bar() + scale_x_discrete(drop=FALSE) grid.arrange(h1, h2, nrow=1)
68
data(UKSoccer, package="vcd") PL <- data.frame(UKSoccer) lx <- c("0","1","2","3","4 or more") b1 <- ggplot(PL, aes(x=factor(Home), weight=Freq)) + geom_bar(fill="firebrick1") + ylab("") + xlab("Home Goals") + scale_x_discrete(labels=lx) + ylim(0,150) b2 <- ggplot(PL, aes(x=factor(Away), weight=Freq)) + geom_bar(fill="cyan1") + ylab("") + xlab("Away Goals") + scale_x_discrete(labels=lx) + ylim(0,150) grid.arrange(b1, b2, nrow=1)
69
xx <- 1:9 Ben <- data.frame(xx, pdf=log10(1+1/xx)) ggplot(Ben, aes(factor(xx), weight=pdf)) + geom_bar() + xlab("") + ylab("") + ylim(0,0.35)