[R] geom_bar with missing data in package ggplot

Aidan Corcoran aidan.corcoran11 at gmail.com
Wed Nov 16 10:55:09 CET 2011


Dear all,

I was hoping someone could help with a ggplot question. I would like
to generate a faceted bar chart, but missing data are causing
problems.

g<-structure(list(Date = structure(c(11322, 11687, 12052, 11322,
11687, 12052, 11322, 11687, 12052, 11322, 11687, 12052), class = "Date"),
    variable = c("Govt Revenues to GDP", "Govt Revenues to GDP",
    "Govt Revenues to GDP", "Govt Revenues to GDP", "Govt Revenues to GDP",
    "Govt Revenues to GDP", "Structural Budget Position", "Structural
Budget Position",
    "Structural Budget Position", "Structural Budget Position",
    "Structural Budget Position", "Structural Budget Position"
    ), var2 = c("United States", "United States", "United States",
    "Japan", "Japan", "Japan", "United States", "United States",
    "United States", "Japan", "Japan", "Japan"), value = c(NA,
    34.288, 31.831, 29.636, 30.539, 29.093, NA, 0, -2.7, -7.4,
    -5.7, -7)), .Names = c("Date", "variable", "var2", "value"
), row.names = c(21L, 22L, 23L, 169L, 170L, 171L, 206L, 207L,
208L, 354L, 355L, 356L), class = "data.frame")

gp <- ggplot(g, aes(Date, value))
gp<- gp + geom_line()
gp <-gp + facet_grid(var2 ~ variable)
gp

this works, but trying to get a bar chart version

gp <- ggplot(g, aes(Date, value))
gp<- gp + geom_bar(stat="identity")
gp <-gp + facet_grid(var2 ~ variable)
gp

gives the error
Error in if (!is.null(data$ymin) && !all(data$ymin == 0))
warning("Stacking not well defined when ymin != 0",  :
  missing value where TRUE/FALSE needed

Is there something I can do to have a gap for missing data, as happens
with the line version?

More generally, I may also have missing data between present data: e.g.

is.na(g[5,4])<-TRUE

and I would like if possible to simply see gaps at these points.

Thanks for any help.

Aidan



More information about the R-help mailing list