在ggplot2中使用coord_flip()和facet_wrap(scales =“free_y”)似乎会给出意想不到的刻面轴刻度标记和刻度标签 [英] Using coord_flip() with facet_wrap(scales = "free_y") in ggplot2 seems to give unexpected facet axis tick marks and tick labels

查看:2283
本文介绍了在ggplot2中使用coord_flip()和facet_wrap(scales =“free_y”)似乎会给出意想不到的刻面轴刻度标记和刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个带有翻转坐标的小平面图,其中只有一个坐标轴可以针对每个方面发生变化。  require(ggplot2)
p <-qplot(displ,hwy,data = mpg)
p + facet_wrap(〜cyl,scales =free_y)+ coord_flip()



这个图对我来说并不令人满意,因为每个图都重复了错误的刻度标记和刻度标签。我希望每个水平轴上的刻度线不在每一个垂直轴上。



这是意想不到的行为,因为该图意味着顶部面板的横轴刻度标记是相同的因为它们是底层的,但它们不是。要看到这个运行:

  p < -  qplot(displ,hwy,data = mpg)
p + facet_wrap 〜cyl,scales =fixed)+ coord_flip()

所以我的问题是:如何去除右侧刻面的垂直轴刻度并在顶面添加水平轴刻度标记和标签?



正如Paul在下面深刻指出的那样,示例I可以通过在qplot()中交换x和y并避免coord_flip()来解决,但是这对于所有geoms都不适用,例如,如果我想要一个带有自由横轴的水平小平面条图,我可以运行:

  c < -  ggplot(aes(clarity,fill = cut))+ geom_bar()
c + facet_wrap(〜cut ,scale =free_y)+ coord_flip()



这些构面有一个可变的水平轴,但是重复的垂直轴刻度而不是重复的水平轴刻度标记秒。我不认为保罗的技巧会在这里工作,因为与散点图不同,柱状图不是旋转对称的。



我会非常感兴趣地听到任何部分或完整的解决方案。

解决方案

这是我自己遇到这个问题的第二次或第三次。

  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< - 函数(。)aes(color = NA,fill =grey20,size = 0.5,linetype = 1,weight = 1,alpha = NA)

required_aes< - c(y)

重参数< - 函数(。,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
})

ggplot2 github中的geom_bar代码,然后切换 x y 引用以在标准笛卡尔坐标系中创建水平barplot。

请注意,您必须使用 position ='identity'并且可能还有 stat ='identity'这个工作。如果您需要使用身份以外的职位,那么您必须编辑碰撞功能以使其正常工作。


I am trying to create a faceted plot with flipped co-ordinates where one and only one of the axes are allowed to vary for each facet:

require(ggplot2)
p <- qplot(displ, hwy, data = mpg)
p + facet_wrap(~ cyl, scales = "free_y") + coord_flip()

This plot is not satisfactory to me because the wrong tick marks and tick labels are repeated for each plot. I want tick marks on every horizontal axis not on every vertical axis.

This is unexpected behaviour because the plot implies that the horizontal axis tick marks are the same for the top panels as they are for the bottom ones, but they are not. To see this run:

p <- qplot(displ, hwy, data = mpg)
p + facet_wrap(~ cyl, scales = "fixed") + coord_flip()

So my question is: is there a way to remove the vertical axis tick marks for the right facets and add horizontal axis tick marks and labels to the top facets?

As Paul insightfully points out below, the example I gave can be addressed by swapping x and y in qplot() and avoiding coord_flip(), however this does not work for all geoms for example, if I want a horizontal faceted bar plot with free horizontal axes I could run:

c <- ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
c + facet_wrap(~cut, scales = "free_y") + coord_flip()

These facets have a variable horizontal axes but repeated vertical axis tick marks instead of repeated horizontal axes tick marks. I do not think Paul's trick will work here, because unlike scatter plots, bar plots are not rotationally symmetric.

I would be very interested to hear any partial or complete solutions.

解决方案

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.

这篇关于在ggplot2中使用coord_flip()和facet_wrap(scales =“free_y”)似乎会给出意想不到的刻面轴刻度标记和刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆