2

I have searched other posts on this topic but can't figure out my problem. I have budgetary data on Brazilian government agencies, and have a classification of agency types. To produce my plot, I do this:

g <- ggplot(data=totalex.df, aes(x=year, y=totalex.billions))

g + 
geom_line(aes(colour=factor(agency.type))) + 
facet_wrap(agency.type ~  unit, ncol=6) + 
opts(strip.text.x = theme_text(size=2)) + 
opts(axis.text.x=theme_text(size=4)) + 
opts(legend.position="none") + 
scale_y_sqrt("total expenditure (billions)")

My problem is, my facet labels now contain two pieces of information: the agency type and the name of the unit--this is a pdf of the graph produced with the above code. I only want it to contain the name of the unit. However, if I drop the agency.type from the facet_wrap command, ggplot loses the order of the agencies.

The output of dput(totalex.df) is quite long but here it is for the first 100 observations

> dput(totalex.df.short)
structure(list(year = c(2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 
2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 
2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 
2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 
2007, 2007, 2007, 2007, 2007), totalex = c(312685301, 46492724, 
85210069, 1478565787, 193941282, 260659307, 8327549603, 0, 18474604, 
91006939, 53538760, 91800981, 402711174, 1290377603, 978348209, 
48360776, 24676283, 89820385, 1038592122, 5364674136, 127383506, 
7552311, 3847705355, 232732854, 34317692, 351714802, 156996087, 
104782402, 0, 47741444, 475303761, 229743044, 102577783, 19106706, 
78619935, 0, 730381485, 998733938, 110785185, 37114540, 108530853, 
0, 0, 0, 2660417864, 169144966, 104350391, 1038914804, 336660855, 
11995616, 0, 0, 9085111, 0, 5281402, 138708048, 11283655, 478421026, 
221976619, 95680527, 8270558, 890375094, 0, 156563720, 198830207, 
286909569, 5525428151, 734738984, 218905808, 1120014693, 859180, 
359873525, 50214197, 95572929, 1628550454, 227051722, 286610734, 
8868199792, 17388668, 19551190, 101047436, 58123546, 101878908, 
423760647, 1019623567, 1061465081, 49559379, 24278026, 110344326, 
1145862548, 7610896352, 163919333, 8376104, 4355447941, 282679957, 
45506963, 392551882, 208594544, 122420822, 0), agency.type = structure(c(NA, 
NA, NA, 3L, 1L, 1L, 7L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 
3L, 1L, 6L, 1L, 1L, 7L, 1L, 3L, 3L, 1L, 3L, 1L, 1L, 1L, 3L, 3L, 
1L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 1L, 5L, 3L, 1L, 5L, 3L, 1L, 1L, 
1L, 1L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 2L, 
1L, 1L, 3L, 1L, 1L, 3L, NA, NA, NA, 3L, 1L, 1L, 7L, 1L, 1L, 1L, 
1L, 1L, 4L, 1L, 1L, 1L, 1L, 3L, 1L, 6L, 1L, 1L, 7L, 1L, 3L, 3L, 
1L, 3L, 1L), .Label = c("Delivery", "Guidance for policy formulation / Research", 
"Regulatory", "Regulatory / Delivery", "Regulatory/Delivery", 
"Transfer / delivery", "Transfer/Regulatory/Deliver"), class = "factor"), 
    unit = structure(c(29L, 28L, 27L, 17L, 21L, 66L, 55L, 5L, 
    69L, 25L, 33L, 36L, 44L, 45L, 9L, 34L, 68L, 13L, 6L, 61L, 
    53L, 65L, 51L, 31L, 41L, 39L, 37L, 49L, 22L, 58L, 47L, 71L, 
    40L, 60L, 19L, 18L, 64L, 10L, 62L, 59L, 42L, 7L, 11L, 4L, 
    52L, 32L, 38L, 8L, 54L, 20L, 12L, 24L, 1L, 63L, 3L, 16L, 
    57L, 70L, 43L, 15L, 2L, 46L, 30L, 35L, 56L, 23L, 48L, 26L, 
    14L, 50L, 67L, 29L, 28L, 27L, 17L, 21L, 66L, 55L, 5L, 69L, 
    25L, 33L, 36L, 44L, 45L, 9L, 34L, 68L, 13L, 6L, 61L, 53L, 
    65L, 51L, 31L, 41L, 39L, 37L, 49L, 22L), .Label = c("Administrative Council of Economic Defense (CADE)                              ", 
    "Air Force Real Estate Funding Agency (CFIAE)                                   ", 
    "Alexandre Gusmao Foundation (FUNAG)                                            ", 
    "Amazon Development Superintendency (SUDAM)                                     ", 
    "Applied Economic Research Institute (IPEA)                                     ", 
    "Brazilian Agricultural Research Corporation (EMBRAPA)                          ", 
    "Brazilian Communication Company (EBC)                                          ", 
    "Brazilian Company of Urban Railway (CBTU)                                      ", 
    "Brazilian Institute of Environment and Renewable Natural Resources (IBAMA)     ", 
    "Brazilian Institute of Geography and Statistics (IBGE)                         ", 
    "Brazilian Institute of Museums (IBRAM)                                         ", 
    "Brazilian Military Material Industry (IMBEL)                                   ", 
    "Brazilian Securities Comission (CVM)                                           ", 
    "Brazilian Space Agency (AEB)                                                   ", 
    "Brazilian Tourism Company (EMBRATUR)                                           ", 
    "Brazilian Urban Railway (TRENSUBR)                                             ", 
    "Central Bank (BACEN)                                                           ", 
    "Chico Mendes Institute (ICMBio)                                                ", 
    "Civil Aviation National Agency (ANAC)                                          ", 
    "Cultural Foundation Palmares                                                   ", 
    "Dom Pedro II School                                                            ", 
    "Energy Research Company (EPE)                                                  ", 
    "Engineering, Construction and Railway (VALEC)                                  ", 
    "Excellence center in advanced electronic technology (CEITEC)                   ", 
    "Federal Centre of Educational Technology Celso Suckow da Fonseca (CEFET-RJ)    ", 
    "Graduate students improvement coordination (CAPES)                             ", 
    "Hospital Cristo Redentor S.A.                                                  ", 
    "Hospital F\x90mina S.A                                                            ", 
    "Hospital Nossa Senhora da Concei\x8d\x8bo S.A                                        ", 
    "Housing Building Fund for Mariners (CCCPMC)                                    ", 
    "Indigenous National Foundation (FUNAI)                                         ", 
    "Institute of the Historical and Artistic National Heritage (IPHAN)             ", 
    "Joaquim Nabuco Foundation                                                      ", 
    "Jorge Duprat Figueiredo Foundation (FUNDACENTRO)                               ", 
    "Manaus Free Trade Zone Superintendency (SUFRAMA)                               ", 
    "Minas Gerais Federal Centre of Educational Technology (CEFET-MG)               ", 
    "Mineral Resources Research Institute (CPRM)                                    ", 
    "National  Agency of Land Transport (ANTT)                                      ", 
    "National  Agency of Sanitary Supervision (ANVISA)                              ", 
    "National  Agency of Supplementary Health (ANS)                                 ", 
    "National  Agency of Water Transport (ANTAQ)                                    ", 
    "National Agency of Electrical Energy (ANEEL)                                   ", 
    "National Agency of Oil, Natural Gas and Biofuel (ANP)                          ", 
    "National Commission of Nuclear Energy (CNEN)                                   ", 
    "National Company of Food Supply (CONAB)                                        ", 
    "National Council of Scientific and Technological Development (CNPq)            ", 
    "National Department of Draught Prevention (DNOCS)                              ", 
    "National Department of Infrasctructure Transport (DNIT)                        ", 
    "National Department of Mineral Production (DNPM)                               ", 
    "National Development Trust (FND)                                               ", 
    "National Health Foundation (FUNASA)                                            ", 
    "National Institute of Colonization and Land Reform (INCRA)                     ", 
    "National Institute of Industrial Property (INPI)                               ", 
    "National Institute of Metrology, Normalization and Industrial Quality (Inmetro)", 
    "National Institute of Social Security (INSS)                                   ", 
    "National Institute of Studies and Educational Research  Anisio Teixeira (INEP) ", 
    "National Institute of Technology Information (ITI)                             ", 
    "National Library Foundation                                                    ", 
    "National Movies Agency (ANCINE)                                                ", 
    "National School of Public Administration Foundation (ENAP)                     ", 
    "National Trust of Education Development (FNDE)                                 ", 
    "National Water Agency (ANA)                                                    ", 
    "Northeast Development Superintendency (SUDENE)                                 ", 
    "Oswaldo Cruz Foundation (FIOCRUZ)                                              ", 
    "Ozorio Foundation                                                              ", 
    "Porto Alegre Clinic Hospital                                                   ", 
    "Private Insurance Superintendency (SUSEP)                                      ", 
    "Rio de Janeiro Botanical Gardens Research Institute                            ", 
    "Rui Barbosa House Foundation                                                   ", 
    "Sao Francisco and Parnaiba Valleys Development Company (CODEVASF)              ", 
    "Telecom National Agency (ANATEL)                                               "
    ), class = "factor")), .Names = c("year", "totalex", "agency.type", 
"unit"), row.names = c(NA, 100L), class = "data.frame")
Andrie
  • 176,377
  • 47
  • 447
  • 496
Lindsay
  • 131
  • 1
  • 10
  • `facet_grid` has the `labeller` argument with which you can change the labels – James Jul 12 '12 at 09:15
  • Hi James. I am aware of the `labeller` argument, but cannot figure out how it works. There is some discussion on stackoverflow, for example [here](http://stackoverflow.com/questions/10151123/how-to-specify-columns-in-facet-grid-or-how-to-change-labels-in-facet-wrap) and [here](http://stackoverflow.com/questions/3472980/ggplot-how-to-change-facet-labels?rq=1) but nothing that seems directly relevant. – Lindsay Jul 12 '12 at 13:36
  • It expects a function of 2 arguments (variable, value) that will return a vector equal to the number of facets. The default is `label_value`. You could just supply an anonymous function that returns the list of labels you want and ignores the arguments. – James Jul 12 '12 at 13:45
  • What would that look like James? My initial thought is that it would be something like: – Lindsay Jul 12 '12 at 13:53
  • `expenditure_labeller <- function(value){ for(i in 1:n.units){ value[i] <- as.character(unit[i]) } return(value) }` – Lindsay Jul 12 '12 at 13:54
  • On reflection, I wondered if the following might work (it doesn't): `expenditure_labeller <- function(var, value){ value <- as.character(value) if (var=="agency.type") { value==NULL } return(value) }` – Lindsay Jul 12 '12 at 14:09
  • Some more detail on how `labeller` works at: https://github.com/hadley/ggplot2/wiki/labeller – Brian Diggs Jul 12 '12 at 18:12

2 Answers2

2

Contrary to my initial thought, I don't think that labeller will help you here because it only works for facet_grid, not facet_wrap. Rather, I think the problem can be handled in an easier manner. The order of the facets is controlled by the order of the factor levels, so reordering the levels of unit such that they are grouped by agency.type should be sufficient.

agency.unit <- unique(totalex.df[c("agency.type", "unit")])

totalex.df$unit <- factor(totalex.df$unit, 
  levels = agency.unit[order(agency.unit$agency.type, 
                             agency.unit$unit), "unit"])

Then your facet_wrap should not need the agency.type in it.

facet_wrap(~  unit, ncol=6)

In total:

ggplot(data=totalex.df, aes(x=year, y=totalex)) + 
    geom_line(aes(colour=factor(agency.type))) + 
    facet_wrap( ~  unit, ncol=6) + 
    opts(strip.text.x = theme_text(size=2)) + 
    opts(axis.text.x=theme_text(size=4)) + 
    opts(legend.position="none") + 
    scale_y_sqrt("total expenditure (billions)")

enter image description here

Even though it is impossible to read in this, the first unit is "Air Force Real Estate Funding Agency (CFIAE)" followed by "Alexandre Gusmao Foundation (FUNAG)", "Applied Economic Research Institute (IPEA)", etc.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • I think this is not quite right Brian (or else I have gone wrong somewhere else along the way). I have used line colour to denote different types of agency, using `geom_line(aes(colour=factor(agency.type)))`. So that if we had what we wanted, all the lines of the same colour would be grouped together. I think the problem is that we have 71 unique agencies, and thus 71 factors but only 7 levels(if that makes sense). – Lindsay Jul 16 '12 at 12:26
  • Incidentally, the ggplot2 help files indicate something called `aes_group_order`. Reading through the help file makes me think that I should be able to do something like `facet_wrap( ~ unit, ncol=6, aes(order=agency.type))` and that this would solve my problem. But it doesn't seem to. – Lindsay Jul 16 '12 at 12:33
  • Specifically, the error it gives is `Error in nrow * ncol : non-numeric argument to binary operator`. – Lindsay Jul 16 '12 at 12:48
  • I don't know about the `aes(order=agency.type)` error, but I fixed the code that defined the levels of `unit`; now all the colors are together as they should have been initially. – Brian Diggs Jul 16 '12 at 17:03
-2

It would help if you give us some data so it's reproducible. Can you add to your question the output of dput(totalex.df) if it isn't too long?

Do you want a facet for each unit? Or do you want a facet for each combination of unit & agency.type?

If it's just unit, try this:

facet_wrap( ~ unit, ncol=6)

If it's both, try this:

facet_grid(agency.type ~ unit, ncol=6)

FYI - To get a better understanding of how to use facet_wrap and facet_grid functions, have a look at Hadley's documentation here:

http://had.co.nz/ggplot2/facet_wrap.html

Tommy O'Dell
  • 7,019
  • 13
  • 56
  • 69
  • Thanks for taking an interest Tommy. What I want is a facet for each unit, but for the facets to be ordered by agency.type. I've added the `dput(totalex.df)` above – Lindsay Jul 12 '12 at 10:44