Chapter 2 - Dot Charts
Graphics out of the book Graphics for Statistics and Data Analysis with R by Kevin Keen (book home page)
Dot charts of the United Nations budget for 2008-2009
- data:
 
item1<-factor(1:14,
             labels=c("Overall coordination",
               "Political affairs",
               "International law",
               "International cooperation",
               "Regional cooperation",
               "Human rights",
               "Public information",
               "Management",
               "Internal oversight",
               "Administrative",
               "Capital",
               "Safety & security",
               "Development",
               "Staff assessment"))
amount1<-c(718555600,626069600,87269400,398449400,
477145600,259227500,184000500,540204300,35997700,
108470900,58782600,197169300,18651300,461366000)
amount1<-amount1/1000000
df <- data.frame(item1=item1,amount1=amount1)
df
item1 amount1 1 Overall coordination 718.5556 2 Political affairs 626.0696 3 International law 87.2694 4 International cooperation 398.4494 5 Regional cooperation 477.1456 6 Human rights 259.2275 7 Public information 184.0005 8 Management 540.2043 9 Internal oversight 35.9977 10 Administrative 108.4709 11 Capital 58.7826 12 Safety & security 197.1693 13 Development 18.6513 14 Staff assessment 461.3660
- now we can build the chart using geom_point()andgeom_hline()
 
- first we build a ggplot object and map xtoamount1andytoitem1
 
- than we add the point layer (geom_point()) setting theshapeto 19 (filled circle)
 
- now we need the horizontal lines, therefore we use geom_hline()and mapas.numeric(item1)(which gives 1:14) toyintercept
 
ggplot(df,aes(x=amount1,y=item1)) +
  geom_point(shape=19) +
  geom_hline(aes(yintercept=as.numeric(item1)),linetype=3)
ggsave("fig2_1.png")
- first we reverse the order of the category using reorder()by the negative of the number of the item
 
- then we increase the size of the points a little (sizeargument ingeom_point())
 
- then we change the title of the x-axis and set the limits to c(0,800) (scale_x_continuous())
 
- setting asis.title.ytotheme_blank()gets us rid of the title of the y-axis
 
- axis.title.xis managed by- theme_text(): we set the text- sizeto 12 and adjust the vertical position (- vjust) downwards
 
- last we set the panel background to white using theme_rect()(and because there are some leftovers of the grid lines visible in the frame we set the major grid lines to blank
 
ggplot(df,aes(x=amount1,y=reorder(item1,-as.numeric(item1)))) +
  geom_point(shape=19,size=4) +
  geom_hline(aes(yintercept=as.numeric(item1)),linetype=3) +
  scale_x_continuous("Millions of US Dollars",limits=c(0,800)) +
  opts(axis.title.y=theme_blank(),
       axis.text.y=theme_text(size=12),
       axis.title.x=theme_text(size=12,vjust=-0.7),
       axis.text.x=theme_text(size=12),
       panel.background=theme_rect(fill="white"),
       panel.grid.major=theme_blank())
ggsave("fig2_1b.png")
- remains the ticks of the y-axis, again we must use the hack (as in chapter 1 - have a look there for further information)
png("fig2_1c.png",height=500, width=500)
ggplot(df,aes(x=amount1,y=reorder(item1,-as.numeric(item1)))) +
  geom_point(shape=19,size=4) +
  geom_hline(aes(yintercept=as.numeric(item1)),linetype=3) +
  scale_x_continuous("Millions of US Dollars",limits=c(0,800)) +
  opts(axis.title.y=theme_blank(),
       axis.text.y=theme_text(size=12),
       axis.title.x=theme_text(size=12,vjust=-0.7),
       axis.text.x=theme_text(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)
dev.off()
X11cairo 
       2
- to change this figure to figure 2.2 we have just to replace geom_hline()bygeom_segment()and change therefore some mappings
 
png("fig2_1d.png",height=500, width=500)
ggplot(df,aes(x=amount1,y=reorder(item1,-as.numeric(item1)))) +
  geom_point(shape=19,size=4) +
  geom_segment(aes(yend=reorder(item1,-as.numeric(item1))),xend=0,linetype=3) +
  scale_x_continuous("Millions of US Dollars",limits=c(0,800)) +
  opts(axis.title.y=theme_blank(),
       axis.text.y=theme_text(size=12),
       axis.title.x=theme_text(size=12,vjust=-0.7),
       axis.text.x=theme_text(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)
dev.off()
X11cairo 
       2




 
 
No comments :
Post a Comment