chapter3
Table of Contents
1 Chapter 3
Graphics out of the book Graphics for Statistics and Data Analysis with R by Kevin Keen (book home page)
- here are the data
item<-c("Canada", "Mexico", "Saudi Arabia", "Venezuela", "Nigeria") amount<-c(2460,1538,1394,1273,1120) amount<-amount/1000 df <- data.frame(item=factor(1:5,labels=item),amount=amount) barrel <- read.jpeg("barrel.jpg")
1.1 Figure 3.4 - simple bar chart
- we use
geom_bar
to create the bar chart
- customizing the y-axis by using
scale_y_continuous
:limits
set the limits,expand
defines the multiplicative and additive expansion constants
coord_flip
rotates it (so we get a horizontal bar chart)
- than we set the background to white
- set the colour of the axis lines to black (we have to do this to axis.line not just axis.line.x because of inheritance)
- get rid of the vertical axis
- set colour of the ticks of the x-axis to black
- get rid of the ticks of the y-axis
- set the colour of the axis labels to black
- change the adjustment of the labels of vertical axis
- get rid of the grid lines (they are still visible if one looks carefully)
ggplot(df,aes(y=amount,x=reorder(item,-as.numeric(item)))) + geom_bar(stat="identity",fill="white",colour="black") + scale_y_continuous("Millions of Barrels per Day",limits=c(0,2.5),expand=c(0,0)) + xlab("") + coord_flip() + theme(panel.background=element_rect(fill="white"), axis.line=element_line(colour="black"), axis.line.y=element_blank(), axis.ticks.x=element_line(colour="black"), axis.ticks.y=element_blank(), axis.text=element_text(colour="black",size=11), axis.text.y=element_text(hjust=0), panel.grid=element_blank()) ggsave("fig3_4.png")
Saving 7 x 6.99 in image
1.2 Figure 3.5 - simple bar chart
- we use
geom_point
to create the chart with dots (set the size of the dots to 3)
- via
geom_segment
we add the dotted lines (linetype=3)
- customizing the x-axis by using
scale_x_continuous
:limits
set the limits,expand
defines the multiplicative and additive expansion constants
- set the colour of the axis lines to black (we have to do this to axis.line not just axis.line.x because of inheritance)
- get rid of the vertical axis
- set colour of the ticks of the x-axis to black
- get rid of the ticks of the y-axis
- set the colour of the axis labels to black
- than we set the background to transparent and the colour of the frame to black (
panel.background=element_rect
)
- get rid of the grid lines (they are still visible if one looks carefully)
- get rid of of the title of the y-axis
- set the colour and the size of the title of the x-axis to black and 11 respectively
ggplot(df,aes(x=amount,y=item)) + geom_point(size=3) + geom_segment(aes(yend=as.numeric(item)),xend=0,linetype=3) + scale_x_continuous("Millions of Barrels per Day",limits=c(0,2.5),expand=c(0,0)) + theme(axis.line=element_line(colour="black"), axis.line.y=element_blank(), axis.ticks.y=element_blank(), axis.ticks.x=element_line(colour="black"), axis.text=element_text(colour="black",size=11), panel.background=element_rect(fill="transparent",colour="black"), panel.grid=element_blank(), axis.title.y=element_blank(), axis.title.x=element_text(colour="black",size=11) ) ggsave("fig3_5.png")
Saving 7 x 6.99 in image
1.3 Figure 3.7
- the clipart can be downloaded here
- we need the ReadImages package for reading this jpeg
- we need the grid graphics package to divide the plot and insert to several parts
- first we load two additional packages (ReadImages for reading the jpeg and grid for the grid graphics functions)
- the next part (definition of the dot chart) is exactly the same as in figure 3.5
- load the jpeg with the barrel (
barrel <- read.jpeg("barrel.jpg")
)
- the next commands are part of the grid package, which is the underlying graphics system of ggplot2
grid.newpage
moves to a new page
pushViewport
adds a new viewport (plotting region) to the page (via x and y one can set the position), beginning in the top left corner, setting the width to 0.6 relative to the page and the height to 0.95;just
sets the adjustment
print(p,newpage=F)
prints the dot chart in this viewport
popViewport()
closes the viewport
- create another viewport next to the other one with width 0.4 and the same height
grid.raster
inserts the picture of the barrel
grid.text
inserts the text
library(grid) library(ReadImages) p <- ggplot(df,aes(x=amount,y=reorder(item,amount))) + geom_point(size=3) + geom_segment(aes(yend=reorder(item,amount)),xend=0,linetype=3) + scale_x_continuous("Millions of Barrels per Day",limits=c(0,2.5),expand=c(0,0)) + theme(axis.line=element_line(colour="black"), axis.line.y=element_blank(), axis.ticks.y=element_blank(), axis.text=element_text(colour="black",size=12), panel.background=element_rect(fill="white",colour="black"), panel.grid=element_blank(), axis.title.y=element_blank(), axis.title.x=element_text(colour="black",size=11) ) barrel <- read.jpeg("barrel.jpg") grid.newpage() pushViewport(viewport(x=unit(0,"line"),y=unit(1,"npc")-unit(2,"mm"),width=0.6,height=0.95,name="vp1",just=c("left","top"))) print(p,newpage=F) popViewport() pushViewport(viewport(x=unit(0.7,"npc"),y=unit(0,"npc"),width=0.4,height=0.95,name="vp1",just=c("left","bottom"))) grid.raster(barrel,width=unit(1,"npc"),just=c("centre","bottom"),x=unit(0.2,"npc"),y=unit(3,"line")) grid.text("Top Five Importing\nCountries of Crude Oil\nand Petrolium\nProducts in 2007\nfor the united States",x=unit(0.2,"npc"),y=unit(1,"npc")-unit(2,"line"),just=c("center","top")) savePlot("fig3_7.png")
No comments :
Post a Comment