当前位置 : 主页 > 手机开发 > 其它 >

ggplot2中继承的限制是什么?

来源:互联网 收集:自由互联 发布时间:2021-06-19
我一直试图解决一些关于ggplot2的事情,以及从第一部分ggplot()继承的补充参数.具体来说,如果继承传递到geom _ ***部分之外. 我有一个数据直方图: ggplot(data = faithful, aes(eruptions)) + geom_his
我一直试图解决一些关于ggplot2的事情,以及从第一部分ggplot()继承的补充参数.具体来说,如果继承传递到geom _ ***部分之外.

我有一个数据直方图:

ggplot(data = faithful, aes(eruptions)) + geom_histogram()

虽然休息是默认的,但它会生成一个精细的图表.在我看来(一个被承认的新手),geom_histogram()继承了ggplot()的数据规范.如果我想有一个更聪明的方法来设置休息,我可以使用这样的过程:

ggplot(data = faithful, aes(eruptions)) + 
geom_histogram(breaks = seq(from = min(faithful$eruptions), 
                            to = max(faithful$eruptions), length.out = 10))

但是,在这里我在geom_histogram()函数中重新指定我想要忠实的$eruptions.我一直无法找到一种方法来表达这一点,而无需重新指定.此外,如果我在geom_histogram()中使用data =参数,并且仅指定min和max中的喷发,则seq()仍然不理解我的意思是忠实的数据集.

我知道seq不是ggplot2的一部分,但是我想知道它是否能够继承,因为它绑定在geom_histogram()中,它本身继承自ggplot().我只是使用错误的语法,或者这可能吗?

请注意,您要查找的术语不是“继承”,而是非标准评估(NSE). ggplot提供了几个可以通过列名而不是完整引用(NSE)引用数据项的地方,但这些只是geom_ *层的映射参数,即使在使用aes时也是如此.这些工作:

ggplot(faithful) + geom_point(aes(eruptions, eruptions))
ggplot(faithful) + geom_point(aes(eruptions, eruptions, size=waiting))

以下不起作用,因为我们指的是在aes和映射之外等待(注意第一个arg到geom_ *是映射arg):

ggplot(faithful) + geom_point(aes(eruptions, eruptions), size=waiting)

但这有效:

ggplot(faithful) + geom_point(aes(eruptions, eruptions), size=faithful$waiting)

虽然不同,因为现在大小被解释为litterally,而不是像映射的一部分那样被标准化.

在您的情况下,因为中断不是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)
  ) )

并且没有NSE,但少一点打字:

ggplot(data=faithful, aes(eruptions)) + 
  geom_histogram(
    breaks=do.call(seq, c(as.list(range(faithful$eruptions)), len=10))
  )
网友评论