什么做'〜'和'。'在facet_wrap()参数中指示 [英] What do '~' and '.' indicate in a facet_wrap() argument

查看:238
本文介绍了什么做'〜'和'。'在facet_wrap()参数中指示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经使用R了一段时间了,在使用 facet_wrap()时仍然不知道自己在做什么。考虑到这一点,对于exapmle:

  ggplot(df,aes(x,y))+ 
geom_bar(stat = identity)+
facet_wrap(〜z)

facet_wrap()我最终只是试着对它的参数进行格式化的不同排列。这是因为我有 no 这些参数中的的含义。 / p>

有人对这些东西有什么简洁的描述吗?

解决方案

facet_grid 文档状态:

一个带有(表格显示)行LHS和RHS上的列表(列表显示);公式中的点用于指示此维度(行或列)上不应该有切面。该公式也可以作为字符串提供,而不是传统的公式对象。

facet_wrap docs state :

公式或字符向量。使用单侧公式,〜a + b或字符矢量c(a,b)。



这两个帮助页面旨在说明正在发生的事情,但您可以模拟自己的示例以查看差异。例如:

  library(ggplot2)

gg < - ggplot(mtcars,aes(mpg, wt))+ geom_point()

gg + facet_wrap(〜cyl)
gg + facet_wrap(cyl)

gg + facet_wrap(〜gear)
gg + facet_wrap(gear)

gg + facet_wrap(gear〜cyl)
gg + facet_wrap(c(gear,cyl))

gg + facet_wrap(cyl〜gear)
gg + facet_wrap(c(cyl,gear))

这是与惯用语IMO建立心理联系的最佳方式。


I've used R for a while now, and still don't know what I'm doing when it comes to using facet_wrap(). Consider this, for exapmle:

ggplot(df, aes(x, y)) + 
    geom_bar(stat = "identity") + 
    facet_wrap(~ z)

It works, but whenever I use facet_wrap() I end up just trying different permutations of how its argument should be formatted. This is because I have no idea what ~ or . mean in these arguments.

Does anyone have a succinct description of what these things are?

解决方案

The facet_grid docs state:

a formula with the rows (of the tabular display) on the LHS and the columns (of the tabular display) on the RHS; the dot in the formula is used to indicate there should be no faceting on this dimension (either row or column). The formula can also be provided as a string instead of a classical formula object

facet_wrap docs state:

Either a formula or character vector. Use either a one sided formula, ~a + b, or a character vector, c("a", "b").

The examples in both help pages are designed to illustrate what's going on, but you can mock up your own examples to see what the differences are. e.g.:

library(ggplot2)

gg <- ggplot(mtcars, aes(mpg, wt)) + geom_point()

gg + facet_wrap(~cyl)
gg + facet_wrap("cyl")

gg + facet_wrap(~gear)
gg + facet_wrap("gear")

gg + facet_wrap(gear~cyl)
gg + facet_wrap(c("gear", "cyl"))

gg + facet_wrap(cyl~gear)
gg + facet_wrap(c("cyl", "gear"))

That's the best way to get a mental association with the idiom IMO.

这篇关于什么做'〜'和'。'在facet_wrap()参数中指示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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