强制ggplot2散点图呈方形 [英] Force ggplot2 scatter plot to be square shaped

查看:573
本文介绍了强制ggplot2散点图呈方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 xlim() ylim(),但它需要手动计算限制。有没有更方便的方法呢?



以方形表示我的意思是两个要求:


  1. x轴和y轴上的缩放比例相同。

  2. x和y轴的长度相等。
  3. 如果要使距离刻度点相同,则使用 coord_fixed()

      p <-ggplot(...)
    p <-p + coord_fixed()#比率参数默认为1即y / x = 1

    如果你想确保结果图是平方的,那么你还需要指定x和y的限制是相同的(或者至少有相同的范围) 。 xlim ylim 都是 coord_fixed 的参数。所以你可以使用这些参数手动执行此操作。或者你可以使用一个函数从数据中提取出限制。


    I can force ggplot2 scatter plot to be square shaped with the same x and y scaling using xlim() and ylim(), but it needs manual calculation of the limits. Is there any more convenient way of doing it?

    By square shape I mean two requirements:

    1. The same scale on x and y axis.
    2. The equal length of x and y axis.

    解决方案

    If you want to make the distance scale points the same, then use coord_fixed():

    p <- ggplot(...)
    p <- p + coord_fixed() # ratio parameter defaults to 1 i.e. y / x = 1
    

    If you want to ensure that the resulting plot is square then you would also need to specify the x and y limits to be the same (or at least have the same range). xlim and ylim are both arguments to coord_fixed. So you could do this manually using those arguments. Or you could use a function to extract out limits from the data.

    这篇关于强制ggplot2散点图呈方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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