简体   繁体   English

ggplot 0.9.3问题与facet_wrap,自由秤和coord_flip-第二次尝试

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

An old code that used to work perfectly no longer works with 0.9.3. 曾经完美运行的旧代码不再适用于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: 数据集: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. 以前,添加coord_flip()可以达到目的,但现在新的ggplot不允许一起使用自由缩放和coord_flip。 Is there any other way to turn the facet axes around? 还有其他方法可以改变刻面轴吗? The free scales are important to me. 免费秤对我很重要。 Thanks for any pointers. 感谢您的指导。

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. 这只是从ggplot2 github复制geom_bar代码,然后切换xy引用以在标准笛卡尔协调器中创建水平条形图。

Note that you must use position='identity' and possibly also stat='identity' for this to work. 请注意,您必须使用position='identity' ,也可能使用stat='identity'才能正常工作。 If you need to use a position other than identity then you will have to eddit the collide function for it to work properly. 如果您需要使用身份以外的其他职位,则必须编辑碰撞函数才能使其正常工作。

Update per late 2016: This bug with coord_flip , facet_grid and scales="free" has been fixed in the development version of ggplot2 . 2016年末更新: coord_flip的开发版本中已修复带有coord_flipfacet_gridscales="free"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. 注意, free_y根据需要尝试使用free_xfree_y ,因为在翻转坐标时并不能始终清楚xy含义。

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 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. 这是一个应该起作用的快速想法:使用facet_grid而不是“ facet_wrap”,然后coord_flip()应该起作用。 Then save the picture as a pdf (or svg) and rearrange the plots in some kind of vector graphic software - I'd suggest Inkscape... 然后将图片另存为pdf(或svg)并在某种矢量图形软件中重新布置图-我建议使用Inkscape ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ggplot2中使用coord_flip()和facet_wrap(scales =“free_y”)似乎给出了意想不到的facet轴刻度标记和刻度标签 - Using coord_flip() with facet_wrap(scales = “free_y”) in ggplot2 seems to give unexpected facet axis tick marks and tick labels ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由秤 - ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales 带有coord_flip的ggplot2中errobar的自由缩放和位置 - Free scales and position of errobar in ggplot2 with coord_flip ggplot2:以向量指定的顺序显示间隔,并使用geom_pointrange()和coord_flip()将各个组(例如facet_wrap)分开 - ggplot2: Show intervals in order specified by a vector and separate groups (e.g. with facet_wrap) using geom_pointrange() and coord_flip() 在ggplot2中,coord_flip和自由缩放不能一起使用 - In ggplot2, coord_flip and free scales don't work together ggplot - 带有自由比例 facet_wrap 的 geom_text - ggplot - geom_text with free scales facet_wrap ggplot 2:在facet_wrap面板之间组合自由和固定比例 - ggplot 2: Combining free and fixed scales among facet_wrap panels 在 ggplot2 中使用 facet_wrap 和 scales = &quot;free&quot; 设置单个轴限制 - Setting individual axis limits with facet_wrap and scales = "free" in ggplot2 当 facet_wrap() 中的比例设置为“自由”时,如何增加 ggplot 中的默认 y 轴限制 - How to increase the default y-axis limit in ggplot when scales in facet_wrap() are set to "free" ggplot:使用 facet_wrap(scales="free_x) 时如何获得连续中断 - ggplot: How to get continuous breaks when using facet_wrap(scales="free_x)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM