[R] ggplot2 ordering in a faceted dotplot.

John Kane jrkrideau at inbox.com
Wed Jun 27 17:07:37 CEST 2012


 am trying to produce two dot plot figures in ggplot2.  So far the first one (p) in the program below is working fine.  

However when I want to move to a faceted plot (p1) I seem to lose my ordering or, more likely, I'm just getting an ordering I am not expecting and I always have trouble understanding ordering in R!

What I would like is the 2011 panel to be ordered in descending order  as is the first  (p) plot where Federal Administration is on top since it is the highest value and then have the 2001 panel sorted in the same order as the 2011 panel although now I think of it, it might be just as good to have the 2001 panel ordered in descending order as well.  

Hints for either layout would be appreciated.

Can anyone suggest an approach?

sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i686-pc-linux-gnu (32-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plyr_1.7.1     reshape2_1.2.1 scales_0.2.1   ggplot2_0.9.1  plotrix_3.4-1 

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.0       
 [5] labeling_0.1       MASS_7.3-18        memoise_0.1        munsell_0.3       
 [9] proto_0.3-9.2      RColorBrewer_1.0-5 stringr_0.6        tools_2.15.0     
John Kane
Kingston ON Canada

#==========================Code=================================

library(ggplot2)
library(reshape2)

# produce a dotplot for 2001 and a faceted dotplot for 2001 & 2011

mydata  <-  structure(list(group = structure(c(7L, 10L, 24L, 19L, 12L, 3L, 
14L, 22L, 6L, 13L, 8L, 11L, 5L, 17L, 21L, 20L, 23L, 2L, 1L, 4L, 
25L, 9L, 15L, 26L, 16L, 18L), .Label = c(" Accommodation and food services", 
"Agriculture ", " Business, building & other support services", 
"Construction", "Durable goods", " Educational services", " Federal administration", 
" Finance and insurance", "Goods-producing industries ", " Health care and social assistance", 
" Information, culture and recreation", "Local and other administration ", 
"Manufacturing", "Non-Durable goods", "Other primary industries", 
" Other services", "Primary ", " Professional, scientific & technical services", 
" Provincial administration", " Real estate and leasing", " Retail trade", 
" Service-producing", " Trade", " Transportation and warehousing", 
"Utilities", " Wholesale trade"), class = "factor"), absent2011 = c(15.2, 
14, 12.3, 11.9, 10.5, 10.1, 9.8, 9.7, 9.4, 9.1, 8.7, 8.6, 8.6, 
8.3, 8.2, 8, 7.9, 7.8, 7.6, 7.6, 7.3, 7.2, 7.1, 7, 6.5, 5.8), 
    absent2001 = c(11.6, 12.8, 10.1, 9.2, 9.4, 8.1, 8.7, 8.5, 
    8.6, 8.6, 7.8, 7.5, 8.5, 8.5, 8, 6.3, 7.5, 7.3, 7.3, 8.5, 
    7.9, 8.5, 9, 6.2, 6.5, 5.1)), .Names = c("group", "absent2011", 
"absent2001"), class = "data.frame", row.names = c(NA, -26L))
mdata  <-  structure(list(group = structure(c(7L, 10L, 24L, 19L, 12L, 3L, 
14L, 22L, 6L, 13L, 8L, 11L, 5L, 17L, 21L, 20L, 23L, 2L, 1L, 4L, 
25L, 9L, 15L, 26L, 16L, 18L), .Label = c(" Accommodation and food services", 
"Agriculture ", " Business, building & other support services", 
"Construction", "Durable goods", " Educational services", " Federal administration", 
" Finance and insurance", "Goods-producing industries ", " Health care and social assistance", 
" Information, culture and recreation", "Local and other administration ", 
"Manufacturing", "Non-Durable goods", "Other primary industries", 
" Other services", "Primary ", " Professional, scientific & technical services", 
" Provincial administration", " Real estate and leasing", " Retail trade", 
" Service-producing", " Trade", " Transportation and warehousing", 
"Utilities", " Wholesale trade"), class = "factor"), absent2011 = c(15.2, 
14, 12.3, 11.9, 10.5, 10.1, 9.8, 9.7, 9.4, 9.1, 8.7, 8.6, 8.6, 
8.3, 8.2, 8, 7.9, 7.8, 7.6, 7.6, 7.3, 7.2, 7.1, 7, 6.5, 5.8), 
    absent2001 = c(11.6, 12.8, 10.1, 9.2, 9.4, 8.1, 8.7, 8.5, 
    8.6, 8.6, 7.8, 7.5, 8.5, 8.5, 8, 6.3, 7.5, 7.3, 7.3, 8.5, 
    7.9, 8.5, 9, 6.2, 6.5, 5.1)), .Names = c("group", "absent2011", 
"absent2001"), class = "data.frame", row.names = c(NA, -26L))

# produce a dotplot for 2011 
p  <-  ggplot(mydata, aes(x = reorder(group, absent2011) , y = absent2011)) + geom_point() +
            coord_flip() +
            opts(title = "Absenteeism among Canadian employees \n 2011",
                                legend.position="none") +
                                scale_y_continuous("Annual number of days absent") +
                                scale_x_discrete("Industry  Groups") 
p

# faceted dotplot for 2001 & 2011
mdata  <-  melt(mdata, id=c("group"))

# function to set facet labels.
 mf_labeller <- function(var, value){
    value <- as.character(value)
    if (var=="variable") { 
        value[value=="absent2001"] <- "2001"
        value[value=="absent2011"]   <- "2011"
    }
    return(value)
}

p1  <-  ggplot(mdata, aes(x = reorder(group, value) , y = value,, colour = variable )) + geom_point() +
            coord_flip() + facet_grid(variable ~ ., labeller=mf_labeller ) +
            opts(title = "Rates of absenteeism among Canadian employees",
             legend.position = "none") +
                                scale_y_continuous("Annual number of days absent") +
                                scale_x_discrete("Industry  Groups") 
p1

#===========================End Code=============================

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list