[英]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代码,然后切换x和y引用以在标准笛卡尔协调器中创建水平条形图。
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_flip
, facet_grid
和scales="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_x
和free_y
,因为在翻转坐标时并不能始终清楚x
和y
含义。
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.