## 1 Keen Chapter 3 part 2

Graphics out of the book Graphics for Statistics and Data Analysis with R by Kevin Keen (book home page)

### 1.1 Figure 3.8 Bar-whisker chart

• we will work out the graphic on flipped axes and flip them later
• set the aesthetics:
• x to the names of the allergenes
• y to the percentage (prevalence)
• ymin to y minus the standard error and
• ymax to y plus the standard error
• using again geom_bar() with the stat="identity" option because we have already aggregated data (so the height of the bars is set by the y aesthetic)
• set the filling and the colour of the edges (fill and colour), finally adjust the width (width) of the bars
• there is no axis title on the axis with the names, so set xlab("") (remember we will flip the axis later)
• set the title of the continuous axis to Percent and
• set the limits of the axis to 0 and 50
• set expansion of it to c(0,0) - because we did not want to expand the axis, it should actually end at 0 and 50
• now we flip the axes
• and set the appearance of the text, axis and background elements

require(ggplot2)

names<-c("Epidermals","Dust Mites","Weeds","Grasses","Molds","Trees")
prevs<-c(38.2,37.8,31.1,31.1,29.3,26.7)
se<-c(3.2,3.2,3.1,3.1,3.0,2.9)

df <- data.frame(item=factor(1:6,labels=names),prevs=prevs,se=se)

ggplot(df,aes(x=item,y=prevs,ymin=prevs-se,ymax=prevs+se)) +
geom_bar(stat="identity",fill="transparent",colour="black",width=0.7) +
geom_errorbar(width=0.3) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
axis.line=element_line(colour="black"),
axis.line.y=element_blank(),
axis.text=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)


### 1.2 Figure 3.9 Bar and single whisker chart

• the same as above, only change the filling of the bars to black (you do not need the colour argument any more) and the width of the error bars to 0

ggplot(df,aes(x=item,y=prevs,ymin=prevs-se,ymax=prevs+se)) +
geom_bar(stat="identity",fill="black",width=0.7) +
geom_errorbar(width=0) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
axis.line=element_line(colour="black"),
axis.line.y=element_blank(),
axis.text=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)


### 1.3 Figure 3.10 Dot-whisker chart

• for the dot-whisker chart we replace geom_bar() by geom_point()
• in geom_point() we set the point size to four and the colour to black
• in geom_errorbar() we set the the width to 0.3
• add geom_vline() for the dotted lines and set the aesthetics xintercept to as.numeric(item) (because this aesthetic expects a numeric argument)
• then change some elements in the theme section
• set the colour in panel.border to black and do not forget to set fill to NA (you won't see anything if you don't)
• remove the axis.line.y line

ggplot(df,aes(x=item,y=prevs,ymin=prevs-se,ymax=prevs+se)) +
geom_point(colour="black",size=4) +
geom_errorbar(width=0.25) +
geom_vline(aes(xintercept=as.numeric(item)),linetype=3,size=0.4) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
panel.border=element_rect(colour="black",fill=NA),
axis.line=element_line(colour="black"),
axis.text=element_text(colour="black",size=14),
axis.title=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)


### 1.4 Figure 3.11 Dot-whisker chart

• only minor changes to the previous plot
• remove geom_vline()
• adjust the widths of the error bars

ggplot(df,aes(x=item,y=prevs,ymin=prevs-se,ymax=prevs+se)) +
geom_point(colour="black",size=4) +
geom_errorbar(width=0.1) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
panel.border=element_rect(colour="black",fill=NA),
axis.line=element_line(colour="black"),
axis.text=element_text(colour="black",size=14),
axis.title=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)


### 1.5 Figure 3.12 Dot-whisker chart

• only adjust the widths of the error bars

ggplot(df,aes(x=item,y=prevs,ymin=prevs-se,ymax=prevs+se)) +
geom_point(colour="black",size=4) +
geom_errorbar(width=0) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
panel.border=element_rect(colour="black",fill=NA),
axis.line=element_line(colour="black"),
axis.text=element_text(colour="black",size=14),
axis.title=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)


### 1.6 Figure 3.13 two-tiered dot-whisker chart

• there are several possibilities
• I decided to use two error bar layers so first
• I have to move the aesthetics for ymin and ymax to geom_errorbar(), I set the width to 0.2
• then I add a second geom_errorbar() set there also aesthetics but now ymin to prevs-1.96*se and ymax to prevs+1.96*se

ggplot(df,aes(x=item,y=prevs)) +
geom_point(colour="black",size=4) +
geom_errorbar(aes(ymin=prevs-se,ymax=prevs+se),width=0.2) +
geom_errorbar(aes(ymin=prevs-1.96*se,ymax=prevs+1.96*se),width=0) +
xlab("") +
ylab("Percent") +
scale_y_continuous(limits=c(0,50),expand=c(0,0)) +
coord_flip() +
theme(
panel.background=element_blank(),
panel.border=element_rect(colour="black",fill=NA),
axis.line=element_line(colour="black"),
axis.text=element_text(colour="black",size=14),
axis.title=element_text(colour="black",size=14),
axis.ticks.x=element_line(colour="black"),
axis.ticks.y=element_blank()
)