ggplot如何计算其默认中断? [英] How does ggplot calculate its default breaks?

查看:103
本文介绍了ggplot如何计算其默认中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是相对自我解释的.我想知道ggplot如何确定其默认中断(以及标签).

The title is relatively self explanatory. I would like to know how ggplot decides its default breaks (and hence labels).

从下面的代码中,每个几何似乎方法都相同:

From the below code, it looks like the method is the same for each geom:

library(ggplot2)

ggplot(data=mtcars,mapping=aes(x=carb,y=hp,fill=as.factor(gear)))+
  geom_bar(stat="identity",position="dodge")

ggplot(data=mtcars,mapping=aes(x=carb,y=hp,fill=as.factor(gear)))+
  geom_point()

任何帮助将不胜感激

推荐答案

我自己也遇到了同样的问题,谷歌将我带到了这个SO问题,所以我认为我会做些挖掘.

I had the same question myself, and Google brought me to this SO question, so I thought I'd do a bit of digging.

假设我们绘图

library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = mpg, size = hp)) +
  geom_point() 

这给出了下面的图,我们希望知道mpg(10,15,...,35),cyl(4,5,...,8)和导出hp(100、150,...,300).

which gives us the following plot, and we wish to know how the breaks for mpg (10, 15, ..., 35), cyl (4, 5, ..., 8), and hp (100, 150, ..., 300) are derived.

重点放在mpg上,我们检查了scale_y_continuous的代码,并发现它调用了continuous_scale.然后,调用?continuous_scale,我们在trans参数的描述下看到了

Focusing on mpg we inspect the code for scale_y_continuous and see that it calls continuous_scale. Then, calling up ?continuous_scale we see, under the description for the trans argument, that

变换对象将变换,逆变换以及生成中断和标签的方法捆绑在一起.

A transformation object bundles together a transform, it's inverse, and methods for generating breaks and labels.

然后,查找?scales::trans_new,我们看到breaks参数的默认值为extended_breaks().在跟踪之后,我们发现scales::extended_breaks调用labeling::extended(rng[1], rng[2], n, only.loose = FALSE, ...).将此应用于我们的数据,

Then, looking up ?scales::trans_new, we see that the default value for the breaks argument is extended_breaks(). Following the trail, we find that scales::extended_breaks calls labeling::extended(rng[1], rng[2], n, only.loose = FALSE, ...). Applying this to our data,

with(mtcars, labeling::extended(range(mpg)[1], range(mpg)[2], m = 5))
# [1] 10 15 20 25 30 35

这就是我们在情节中观察到的.这就提出了一个问题,尽管如此,为什么

which is what we observe in the plot. This raises the question of why, despite

with(mtcars, labeling::extended(range(hp)[1], range(hp)[2], m = 5))
# [1]  50 100 150 200 250 300 350

在图例中我们没有观察到50和350.我的理解是,答案与 https://stackoverflow.com/a/13888731/6455166 有关.

we don't observe 50 and 350 in the legend. My understanding is that the answer is related to https://stackoverflow.com/a/13888731/6455166.

这篇关于ggplot如何计算其默认中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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