强制ggplot2散点图呈方形 [英] Force ggplot2 scatter plot to be square shaped
问题描述
我可以使用 xlim()
和 ylim()$ x来强制ggplot2散点图以相同的x和y缩放方形c $ c>,但它需要手动计算限制。有没有更方便的方法呢?
以方形表示我的意思是两个要求:
- x轴和y轴上的缩放比例相同。
- x和y轴的长度相等。 如果要使距离刻度点相同,则使用 coord_fixed():
- The same scale on x and y axis.
- The equal length of x and y axis.
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:
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屋!