ggplot 0.9.3 issue with facet_wrap, free scales and coord_flip - 2nd try

核能气质少年 提交于 2019-12-11 19:01:33

问题


An old code that used to work perfectly no longer works with 0.9.3. The issue is related to the use of facets, free scales and coord flip.

Here is a way to reproduce:

data set: d.csv:

"Priority","Owner","Project"
"Medium","owner7","Team4"
"Medium","owner1","Team1"
"Low","","Team3"
"High","owner6","Team3"
"Medium","","Team4"
"Medium","owner3","Team1"
"Medium","owner2","Team1"
"Medium","owner5","Team2"
"Low","owner4","Team2"
"Critical","","Team2"
"Medium","owner2","Team1"
"High","","Team4"

Code:

data <- read.csv(file="d.csv",head=TRUE)
attach(data)

p3 <- ggplot(data,aes(x=Owner,fill=Priority))+
geom_bar(aes(y=..count..)) + 
facet_wrap(~ Project, nrow=2, scales="free") +
opts(legend.position="none") 

This creates a faceted plot but I need the axes flipped. Previously, adding a coord_flip() did the trick but now the new ggplot does not permit using free scales and coord_flip together. Is there any other way to turn the facet axes around? The free scales are important to me. Thanks for any pointers.


回答1:


This is the second or third time I have run into this problem myself. I have found that I can hack my own solution by defining a custom geom.

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"
})

This is just copying the geom_bar code from the ggplot2 github and then switching the x and y references to make a horizontal barplot in the standard Cartesian coordinators.

Note that you must use position='identity' and possibly also stat='identity' for this to work. If you need to use a position other than identity then you will have to eddit the collide function for it to work properly.




回答2:


Update per late 2016: This bug with coord_flip, facet_grid and scales="free" has been fixed in the development version of ggplot2. You can install it with

install.packages("devtools")
devtools::install_github("hadley/ggplot2")

Note, try both free_x and free_y depending on your needs, because it is not always clear what x and y mean when you have flipped the coordinates.




回答3:


It seems like what you are requesting (if I understand the question correctly) has been raised to the developers before and they will not implement it. See here:

https://github.com/hadley/ggplot2/issues/95

So I guess you need to find a workaround. Here's a quick idea that should work: Use facet_grid instead of "facet_wrap", then coord_flip() should work. Then save the picture as a pdf (or svg) and rearrange the plots in some kind of vector graphic software - I'd suggest Inkscape...



来源:https://stackoverflow.com/questions/16574841/ggplot-0-9-3-issue-with-facet-wrap-free-scales-and-coord-flip-2nd-try

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!