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

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

问题描述

在ggplot2中, coord_fixed()坐标系确保数据的宽高比保持在给定值。所以,面板的形状会发生变化以保持数据的形状。同时 coord_flip()交换图的坐标轴。但是,ggplot2中的图必须只有一个坐标系,所以这些函数不能组合。



我的问题是:


是否存在一种将行为的 coord_fixed() coord_flip(),产生一个x轴和y轴交换的坐标系,数据的纵横比?


这是一个受欢迎的问题,但常见的答案是不正确的:




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



    注意:这将只给出两个连续比例的精确正确答案,其中等于乘法扩展参数(即默认值)。

    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).

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

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