[英]R: “Error in facet_grid” - “unused argument”
I am trying to plot some data from my experiment in R using ggplot2
, and I am trying to split the graph in two parts using facet_grid()
. 我试图使用ggplot2
从R中的实验中绘制一些数据,并且试图使用facet_grid()
将图分为两部分。
Here is an MWE I built with the cars dataset: 这是我使用汽车数据集构建的MWE:
data(mtcars)
ggplot(data=mtcars, aes(x=mtcars$mpg,y=mtcars$cyl)) +
geom_point()+
facet_grid(rows=mtcars$disp)
I get the following error: 我收到以下错误:
Error in facet_grid(rows = mtcars$disp) :
unused argument (rows = mtcars$disp)
I really have no idea why this is happening. 我真的不知道为什么会这样。 I used this function before, and it worked fine. 我以前使用过此功能,但效果很好。 Would appreciate ideas on how to solve this. 将不胜感激如何解决此问题的想法。
edit: I accepted the second answer, because it provides some more context, but as I see it, both are equally correct in pointing out that I need to quote the variable name. 编辑:我接受第二个答案,因为它提供了更多的上下文,但是正如我所看到的,在指出我需要引用变量名时,两者都是正确的。 The actual error was resolved after installig R and all packages again. 在installig R和所有软件包之后,实际的错误已解决。 Now I have a new error, but that is another story. 现在我有一个新的错误,但这是另一个故事。 Thanks again! 再次感谢!
This should do: 应该这样做:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(rows = "disp")
alternatively: 或者:
ggplot(data=mtcars, aes(mpg, cyl)) +
geom_point()+
facet_grid(~disp)
First, don't explicitly refer to mtcars
within the aes()
call. 首先,不要在aes()
调用中明确引用mtcars
。 Second, quote the facet argument. 第二,引用方面参数。
library(ggplot2)
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp")
Also, consider creating a new variable that collapses disp
into fewer values for the facet to be more meaningful & readable. 另外,考虑创建一个新的变量,该变量将disp
折叠成较少的值,以使构面更有意义和更易读。
Here's an example of four arbitrary cut points. 这是四个任意切割点的示例。
mtcars$disp_cut_4 <- cut(mtcars$disp, breaks=c(0, 200, 300, 400, 500))
ggplot(data=mtcars, aes(x=mpg,y=cyl)) +
geom_point()+
facet_grid(rows="disp_cut_4")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.