ggplot2:翻转轴并保持数据的纵横比 [英] ggplot2: Flip axes and maintain aspect ratio of data

查看:26
本文介绍了ggplot2:翻转轴并保持数据的纵横比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ggplot2 中,coord_fixed() 坐标系确保数据的纵横比保持在给定值.因此,面板的形状会发生变化以保持数据的形状.同时 coord_flip() 交换绘图的轴.但是,ggplot2 中的绘图必须只有一个坐标系,因此这些功能不能组合使用.

我的问题是:

<块引用>

是否有一种方法可以将 coord_fixed()coord_flip() 的行为结合起来,从而得到一个 x 轴和 y 轴交换并固定的坐标系?数据的纵横比?

这是一个流行的问题,但常见的答案是不正确的:

  • 我在实践中可能会使用的解决方案是使用 ggstance 包中的水平几何体(尽管这可能并不总是可行).

    注意:这只会给出具有相等乘法 extend 参数(即默认值)的两个连续尺度的准确答案.

    在很多情况下,我会建议使用 coord_equalggstance 包结合使用,而不是这个解决方案.

    In ggplot2, the coord_fixed() coordinate system ensures that the aspect ratio of the data is maintained at a given value. So, the shape of the panel changes to maintain the shape of the data. Meanwhile coord_flip() swaps the axes of the plot. However, a plot in ggplot2 must have exactly one coordinate system, so these functions cannot be combined.

    My question is:

    Does there exist a way to combine the behaviours of coord_fixed() and coord_flip(), resulting in a coordinate system with the x and y axes exchanged and a fixed aspect ratio of the data?

    This is a popular question, however the common answer is incorrect:

    The commonly suggested answer is to use coord_flip() together with theme(aspect.ratio = 1) instead of coord_fixed(). However, as per the ggplot2 documentation, this setting refers to the "aspect ratio of the panel." Thus, the data will change shape to maintain the shape of the panel.

    I suspect that this is a feature that does not currently exist in ggplot2. But more importantly I think that a correct solution or at least response to this question should be documented.

    Quick minimal example of the issue:

    library(ggplot2)
    x <- 1:100; data <- data.frame(x = x, y = x * 2)
    p <- ggplot(data, aes(x, y)) + geom_point()
    
    p # by default panel and data both fit to device window
    p + coord_fixed() # panel changes shape to maintain shape of data
    p + theme(aspect.ratio = 1) # data changes shape to maintain shape of panel
    p + coord_fixed() + coord_flip() # coord_flip() overwrites coord_fixed()
    
    # popular suggested answer does not maintain aspect ratio of data:
    p + coord_flip() + theme(aspect.ratio = 1)
    

    解决方案

    I agree that the theme solution isn't really a proper one. Here is a solution that does work programatically by calculating the aspect from the actual axes ranges stored in the plot object, but it takes a few lines of code:

    ranges <- ggplot_build(p)$layout$panel_ranges[[1]][c('x.range', 'y.range')]
    sizes <- sapply(ranges, diff)
    aspect <- sizes[1] / sizes[2]
    
    p + coord_flip() + theme(aspect.ratio = aspect)
    

    The solution I would probably use in practice, is to use the horizontal geoms in the ggstance package (although this may not always be feasible).

    Note: This will only give the exact correct answer for two continuous scales with an equal multiplicative extend argument (i.e. the default).

    edit: In many cases I would recommend using coord_equal combined with the ggstance package instead of this solution.

    这篇关于ggplot2:翻转轴并保持数据的纵横比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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