简体   繁体   English

在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

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: 正如保罗在下面深刻指出的那样,我给出的例子可以通过在qplot()中交换x和y并避免coord_flip()来解决,但是这并不适用于所有的geoms,例如,如果我想要一个免费的水平多面条形图水平轴我可以运行:

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. 我很想听听任何部分或完整的解决方案。

Using coord_flip in conjunction with facet_wrap is the problem. coord_flipfacet_wrap结合使用是个问题。 First you define a certain axis to be free (the x axis) and then you swap the axis, making the y axis free. 首先,您将某个轴定义为自由( x轴),然后交换轴,使y轴自由。 Right now this is not reproduced well in ggplot2. 现在这在ggplot2中没有很好地再现。

In your first example, I would recommend not using coord_flip , but just swapping the variables around in your call to qplot , and using free_x : 在你的第一个例子中,我建议不要使用coord_flip ,而只是在调用qplot和使用free_x交换变量:

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

在此输入图像描述

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来破解我自己的解决方案。

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. 如果你需要使用身份以外的位置,那么你必须编辑碰撞功能才能正常工作。

I've just been trying to do a horizontal barplot, and run into this problem where I wanted to scales = "free_x" . 我刚刚尝试做一个水平的条形图,遇到这个问题我想要scales = "free_x" In the end, it seemed easier to create the conventional (vertical) barplot), rotate the text so that if you tip your head to the left, it looks like the plot that you want. 最后,创建传统(垂直)条形图似乎更容易,旋转文本,这样如果你将头部向左倾斜,它看起来就像你想要的情节。 And then, once your plot is completed, rotate the PDF/image output(!) 然后,一旦完成绘图,旋转PDF /图像输出(!)

ggplot(data, aes(x, y)) +
  geom_bar(stat = "identity") +
  facet_grid(var ~ group, scale = "free", space = "free_x", switch = "both") +
  theme(axis.text.y  = element_text(angle=90), axis.text.x = element_text(angle = 90),
                     strip.text.x = element_text(angle = 180))

The main keys to do this are to switch = "both" , which moves the facet labels to the other axis, and the element_text(angle=90) which rotates the axis labels and text. 执行此操作的主要键是switch = "both" ,将facet标签移动到另一个轴,以及element_text(angle=90) ,用于旋转轴标签和文本。

暂无
暂无

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

相关问题 ggplot 0.9.3问题与facet_wrap,自由秤和coord_flip-第二次尝试 - ggplot 0.9.3 issue with facet_wrap, free scales and coord_flip - 2nd try facet_wrap 标题环绕和 free_y 轴上的小数位 (ggplot2) - facet_wrap Title wrapping & Decimal places on free_y axis (ggplot2) 使用 xlim() 和 facet_wrap(..., scales=“free_y” ) 的意外 plot - Unexpected plot using xlim() and facet_wrap(…, scales=“free_y” ) ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由秤 - ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales 在多面板图中将顶部刻度添加到y轴(facet_wrap / ggplot2) - Adding top tick to y-axis in multi-panel plots (facet_wrap/ggplot2) 使用 ggplot2 和 facet_wrap 显示不同的轴标签 - Showing different axis labels using ggplot2 with facet_wrap 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() 部分“free_y”Facet Wrap with ggplot - Partaly "free_y" Facet Wrap with ggplot 在 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"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM