我一直在尝试解决有关ggplot2的一些问题,以及辅助参数如何从第一部分继承而来ggplot()。具体来说,如果继承是传递给该geom_***部分之外的。
我有一个数据直方图:
ggplot(data = faithful, aes(eruptions)) + geom_histogram()
Run Code Online (Sandbox Code Playgroud)
尽管默认情况下是休息时间,但它会生成一个精美的图表。在我看来(是新来的新手)geom_histogram()是从继承数据规范ggplot()。如果我想以更聪明的方式来设置休息时间,可以使用如下流程:
ggplot(data = faithful, aes(eruptions)) +
geom_histogram(breaks = seq(from = min(faithful$eruptions),
to = max(faithful$eruptions), length.out = 10))
Run Code Online (Sandbox Code Playgroud)
但是,在这里我要在需要的geom_histogram()功能内重新指定faithful$eruptions。没有重新指定,我一直无法找到一种表达方式。此外,如果我使用data =的参数geom_histogram(),只是指定eruptions在min和max,seq()仍然不明白我的意思的faithful数据集。
我知道这seq不是ggplot2的一部分,但我想知道它是否可以继承,无论它绑定在geom_histogram()哪个内部,而后者本身是从继承的ggplot()。我只是使用了错误的语法,还是可能?
请注意,您要查找的术语不是“继承”,而是非标准评估(NSE)。 ggplot提供了几个位置,您可以通过列名称而不是完整引用 (NSE) 来引用数据项,但这些只是mapping图层的参数geom_*,即使在您使用aes. 这些工作:
ggplot(faithful) + geom_point(aes(eruptions, eruptions))
ggplot(faithful) + geom_point(aes(eruptions, eruptions, size=waiting))
Run Code Online (Sandbox Code Playgroud)
以下内容不起作用,因为我们指的是andwaiting之外(注意第一个 arg to是arg):aesmappinggeom_*mapping
ggplot(faithful) + geom_point(aes(eruptions, eruptions), size=waiting)
Run Code Online (Sandbox Code Playgroud)
但这有效:
ggplot(faithful) + geom_point(aes(eruptions, eruptions), size=faithful$waiting)
Run Code Online (Sandbox Code Playgroud)
尽管有所不同,因为现在size被逐字解释,而不是被规范化为 的一部分mapping。
就您而言,由于breaks不是aes/mapping规范的一部分,因此您不能使用 NSE,并且您只能使用完整的参考。一些可能的解决方法:
ggplot(data = faithful, aes(eruptions)) + geom_histogram(bins=10) # not identical
ggplot(data=faithful, aes(eruptions)) +
geom_histogram(
breaks=with(faithful, # use `with`
seq(from=max(eruptions), to=min(eruptions), length.out=10)
) )
Run Code Online (Sandbox Code Playgroud)
没有 NSE,但打字少一点:
ggplot(data=faithful, aes(eruptions)) +
geom_histogram(
breaks=do.call(seq, c(as.list(range(faithful$eruptions)), len=10))
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |