Saturday, September 8, 2012

Graphics for Statistics - figures with ggplot - Chapter 2 Part 2 - Bar Chart Flavours

Graphics for Statistics - Chapter 2 - Bar Charts: Figures 2.3-2.10 + 2.13

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


Bar charts of the United Nations budget for 2008-2009


  • using geom_bar()
  • mapping x to item1 and y to amount1
  • set stat="identity" because of presummarised data
  • and there is the basic plot

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(stat="identity")
ggsave("fig2_3.png")



Saving 7 x 6.99 in image


But of course there is a lot to do left: you can not read the labels of the x-axis and the we have to change the axis titles


ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(stat="identity") +
  xlab("") +
  ylab("Millions of US Dollars") +
  opts(axis.text.x=theme_text(angle=90,size=12))
ggsave("fig2_3b.png")

Saving 7 x 6.99 in image



  • and there is the graph in default ggplot style
  • now we the plot the style of the plot in the book:
  • add the expand argument to the definition of the y-axis to let the drawn axis end exactly at the limits
  • the width of the bins is changed through the width argument in geom_bar(); in this case it is a bit tricky, because using the identity stat resets width so we have to put width in to the aes() argument (further information)
  • we add a hjust argument in the axis.text.x to change the alignment
  • we set fill and colour of the background to white
  • we use a simple extension by Rudolf Cardinal (source line), because we want to remove just one axis not the two of them (further information)
  • and at the end like above, we use again the hack to get rid of the ticks of the x-axis

source("http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r")
png("fig2_3c.png",height=500, width=500)
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  opts(axis.text.x=theme_text(angle=90,size=12,hjust=1),
       axis.text.y=theme_text(size=12),
       panel.background=theme_rect(fill="white",colour="white"),
       panel.border=theme_left_border()
       )
g <- grid.gget(gPath("axis-b", "", "", "", "axis.ticks.segments"))
grid.remove(g$name)
dev.off()
X11cairo 
       2


ggplot9.2 is out - and everything much easier:

  • you do not need to manipulate the grid elements directly, axis.ticks.x and axis.ticks.y are now available
  • there is also no need to use additional functions anymore: axis.line, axis.line.x and axis.line.y do a good job
  • maybe it this a bit confusing: first you have to set axis.line and then you you the axis blank you do not want to see, this is necessary because of the inheritance
  • there are also some functions renamed: use theme instead of opts and element instead of theme

## 9.2 version
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  theme(axis.text.x=element_text(angle=90,size=12,hjust=1,colour="black"),
        axis.text.y=element_text(size=12,colour="black"),
        axis.line=element_line(colour="black"),
        axis.line.x=element_blank(),
        axis.ticks.x=element_blank(),
        panel.background=element_rect(fill="white",colour="white")
       )
ggsave("fig2_3n.png")

Saving 7 x 6.99 in image




  • in figure 2.4 just the angle of the labels is changed, but therefore we have to adjust the alignment (add vjust argument)
  • also set the size of the labels to 11
  • savePlot() is a alternative to open and close a device explicitly

source("http://egret.psychol.cam.ac.uk/statistics/R/extensions/rnc_ggplot2_border_themes.r")
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800)) +
  xlab("") +
  opts(axis.text.x=theme_text(angle=45,size=12,hjust=1,vjust=1),
       axis.text.y=theme_text(size=12),
       panel.background=theme_rect(fill="white",colour="white"),
       panel.border=theme_left_border()       )
g <- grid.gget(gPath("axis-b", "", "", "", "axis.ticks.segments"))
grid.remove(g$name)
savePlot("fig2_4.png")


  • and here is also the code for ggplot v9.2

## 9.2 version
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",expand=c(0,0),limits=c(0,800)) +
  xlab("") +
  theme(axis.text.x=element_text(angle=45,size=11,hjust=1,vjust=1,colour="black"),
         axis.text.y=element_text(size=12,colour="black"),
         axis.line=element_line(colour="black"),
         axis.line.x=element_blank(),
         axis.ticks.x=element_blank(),
         panel.background=element_rect(fill="white",colour="white")
         )
ggsave("fig2_4n.png")



  • in figure 2.5 the axes are exchanged - so we can use the final code from figure 2.3
  • and do some minor changes (alignment, angle of labels)
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  opts(axis.text.x=theme_text(size=11,vjust=-1),
       axis.text.y=theme_text(hjust=1,size=12),
       panel.background=theme_rect(fill="white",colour="white"),
       panel.border=theme_bottom_border()
       )
g <- grid.gget(gPath("axis-l", "", "", "", "axis.ticks.segments"))
grid.remove(g$name)
savePlot("fig2_5.png")


  • and here is the 9.2 version

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  theme(axis.text.x=element_text(size=11,vjust=-1,colour="black"),
        axis.text.y=element_text(hjust=1,size=12,colour="black"),
        axis.line=element_line(colour="black"),
        axis.line.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_line(colour="black"),
        panel.background=element_rect(fill="white",colour="white")
       )
ggsave("fig2_5n.png")




  • for figure 2.6 we just remove the colour argument from panel.background, the panel.border option and add panel.grid.major=theme_blank() to get rid of the tracks of the grid lines
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  opts(axis.text.x=theme_text(size=11,vjust=-1),
       axis.text.y=theme_text(hjust=1,size=12),
       panel.background=theme_rect(fill="white"),
       panel.grid.major=theme_blank()
       )
g <- grid.gget(gPath("axis-l", "", "", "", "axis.ticks.segments"))
grid.remove(g$name)
savePlot("fig2_6.png")


  • and again the 9.2 version

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  theme(axis.text.x=element_text(size=11,vjust=-1,colour="black"),
       axis.text.y=element_text(hjust=1,size=12,colour="black"),
       axis.ticks.y=element_blank(),
        axis.ticks.x=element_line(colour="black"),
       panel.background=element_rect(fill="white",colour="black"),
       panel.grid.major=element_blank()
       )
ggsave("fig2_6n.png")

Saving 7 x 6.99 in image



  • from now all code is for ggplot2 version 9.2
  • figure 2.8 keep the vertical grid lines, but removes the horizontal ones: this is controlled by panel.grid.major.x and panel.grid.major.y (line elements)

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  theme(axis.text.x=element_text(size=11,vjust=-1,colour="black"),
        axis.text.y=element_text(hjust=1,size=12,colour="black"),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_line(colour="black"),
        panel.background=element_rect(fill="white",colour="black"),
        panel.grid.major.y=element_blank(),
        panel.grid.major.x=element_line(colour="black")
       )
ggsave("fig2_8.png")

Saving 7 x 6.99 in image



  • for figure 2.9 we change the colour of the borders of the bars to black (colour) and the colour of the filling to grey (fill)

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity",fill="grey",colour="black") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  theme(axis.text.x=element_text(size=12,colour="black"),
        axis.text.y=element_text(size=12,colour="black"),
        axis.ticks.x=element_line(colour="black"),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_line(colour="black"),
        axis.line=element_line(colour="black"),
        axis.line.y=element_blank(),
        panel.background=element_rect(fill="white")
        )
ggsave("fig2_9.png")

Saving 7 x 6.99 in image



  • for figure 2.10 just the filling of the bars have to be changed to white

ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity",fill="white",colour="black") +
  scale_y_continuous("Millions of US Dollars",limits=c(0,800),expand=c(0,0)) +
  xlab("") +
  coord_flip() +
  theme(axis.text.x=element_text(size=12,colour="black"),
        axis.text.y=element_text(size=12,colour="black"),
        axis.ticks.x=element_line(colour="black"),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_line(colour="black"),
        axis.line=element_line(colour="black"),
        axis.line.y=element_blank(),
        panel.background=element_rect(fill="white")
        )
ggsave("fig2_10.png")


Saving 7 x 6.99 in image




  • for figure 2.13 figure 2.4 is a good beginning
  • set the colour of the filling of the bars to grey
  • we set the breaks and labels of the y-axis manually
  • add horizontal white lines via geom_hline
## 2.13
dollars <- paste("US$",c(200,400,600),"k",sep="")
ggplot(df,aes(x=item1,y=amount1)) +
  geom_bar(aes(width=0.7),stat="identity",fill="grey") +
  scale_y_continuous(expand=c(0,0),breaks=c(0,200,400,600),labels=c("0",dollars)) +
  geom_hline(yintercept=c(200,400,600),colour="white") +
  theme(axis.text=element_text(size=11.5,colour="black"),
        axis.text.x=element_text(angle=45,hjust=1,vjust=1),
        axis.ticks=element_blank(),
        axis.title=element_blank(),
        axis.line=element_line(colour="grey"),
        axis.line.y=element_blank(),
        panel.background=element_rect(fill="white",colour="white")
         )
ggsave("fig2_13.png")
Saving 7 x 6.99 in image




No comments :

Post a Comment