Skip to content

geom_bar() with facet_grid(space="free") with coord_flip() #1393

@mrdanbrooks

Description

@mrdanbrooks

In the code below, I was expecting space="free" to produce a thinner plot for 'b'.

library(ggplot2)
t <- "Group,Response,Freq,pct_freq,disp
none,1,0,0.000000000,a
low,1,5,0.018939394,a
high,1,65,0.250965251,a
none,2,0,0.000000000,a
low,2,15,0.056818182,a
high,2,89,0.343629344,a
none,3,1,0.007936508,a
low,3,83,0.314393939,a
high,3,73,0.281853282,a
none,4,14,0.111111111,b
low,4,40,0.151515152,b
high,4,14,0.054054054,b
none,5,21,0.166666667,a
low,5,32,0.121212121,a
high,5,14,0.054054054,a
none,6,55,0.436507937,a
low,6,60,0.227272727,a
high,6,4,0.015444015,a
none,7,35,0.277777778,a
low,7,29,0.109848485,a
high,7,0,0.000000000,a"
m <- read.csv(textConnection(t))
m$Response <- factor(m$Response, levels=1:7, ordered=TRUE)
ggplot() + 
    aes(x=Group, fill=Response, order=Response) + 
    geom_bar(data=m, aes(y=pct_freq), position="stack", stat="identity") + 
    facet_grid(. ~ disp, space="free") + 
    coord_flip())

geombarfacet_prob

It has been pointed out to me that the most likely reason it behaves like this is that without the coord_flip(), the plots are the same height and this property is being kept. If this is actually the "expected behavior" rather then a bug, it might be useful to add a geom_bar_horiz.

In trying to help me find a work around, @mattantaliss sent me the following code based on the accepted answer to http://stackoverflow.com/questions/12560858/using-coord-flip-with-facet-wrapscales-free-y-in-ggplot2-seems-to-give-u
This makes the size of 'b' behave the way I was expecting, but has a side effect of messing up the bars in 'a', and also requires scales="free" to be set (which I don't want).

library(ggplot2)
library(proto)

geom_bar_horz <- function (mapping = NULL, data = NULL, stat = "bin", position = "stack", ...) {
  GeomBar_horz$new(mapping = mapping, data = data, stat = stat, position = position, ...)
}

GeomBar_horz <- proto(ggplot2:::Geom, {
  objname <- "bar_horz"

  default_stat <- function(.) StatBin
  default_pos <- function(.) PositionStack
  default_aes <- function(.) aes(colour=NA, fill="grey20", size=0.5, linetype=1, weight = 1, alpha = NA)

  required_aes <- c("y")

  reparameterise <- function(., df, params) {
    df$width <- df$width %||%
      params$width %||% (resolution(df$x, FALSE) * 0.9)
    OUT <- transform(df,
                     xmin = pmin(x, 0), xmax = pmax(x, 0),
                     ymin = y - .45, ymax = y + .45, width = NULL
    )
    return(OUT)
  }

  draw_groups <- function(., data, scales, coordinates, ...) {
    GeomRect$draw_groups(data, scales, coordinates, ...)
  }
  guide_geom <- function(.) "polygon"
})

t <- "Group,Response,Freq,pct_freq,disp
none,1,0,0.000000000,a
low,1,5,0.018939394,a
high,1,65,0.250965251,a
none,2,0,0.000000000,a
low,2,15,0.056818182,a
high,2,89,0.343629344,a
none,3,1,0.007936508,a
low,3,83,0.314393939,a
high,3,73,0.281853282,a
none,4,14,0.111111111,b
low,4,40,0.151515152,b
high,4,14,0.054054054,b
none,5,21,0.166666667,a
low,5,32,0.121212121,a
high,5,14,0.054054054,a
none,6,55,0.436507937,a
low,6,60,0.227272727,a
high,6,4,0.015444015,a
none,7,35,0.277777778,a
low,7,29,0.109848485,a
high,7,0,0.000000000,a"

m <- read.csv(textConnection(t))
m$Response <- factor(m$Response, levels=1:7, ordered=TRUE)

print(ggplot(data = m, 
       aes(y = Group, x = pct_freq, fill = Response, order = Response)) + 
  geom_bar_horz(position = "identity", stat = "identity") + 
  facet_grid(. ~ disp, scales = "free", space = "free"))

geombarfacet_help

I am using

  • ggplot2 1.0.1
  • R 3.2.1
  • OSX 10.7.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions