Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot在aes中找不到对象_R_Ggplot2 - Fatal编程技术网

R ggplot在aes中找不到对象

R ggplot在aes中找不到对象,r,ggplot2,R,Ggplot2,我有以下数据框架和功能: structure(list(vnum1 = c(-0.853002701467605, -2.41044831451325, 1.22391941685926, 0.539011835935724, -1.33616761309235, -1.33762097068431, 0.0391687995434752

我有以下数据框架和功能:

structure(list(vnum1 = c(-0.853002701467605, -2.41044831451325, 
                          1.22391941685926, 0.539011835935724, 
                         -1.33616761309235, -1.33762097068431, 
                          0.0391687995434752, -0.0396899713936502, 
                          -1.34004495967791, 0.731212395958092), 

                vnum2 = c(-0.0140296461607895, 0.649714762844125, 
                          -0.202655014660386, 1.90785563726907, 
                           0.240191747220876, 0.0395243104031934, 
                           -2.1689146110194, -0.198126356757339, 
                           1.89172814288286, -0.484592561521101), 
             vint1 = c(7L, 4L, 7L, 3L, 10L, 10L, 7L, 8L, 2L, 3L), 
             vint2 = c(2L, 8L, 2L, 7L, 3L, 3L, 2L, 8L, 4L, 6L), 
             vfac1 = structure(c(3L, 1L, 2L, 4L, 1L, 1L, 2L, 2L, 1L, 2L),
            .Label = c("1", "2", "3", "4"), class = "factor"), 
             vfac2 = structure(c(3L, 4L, 4L, 2L, 4L, 1L, 1L, 2L, 1L, 4L), 
            .Label = c("1", "2", "3", "4"), class = "factor"), 
             vch1 = structure(c(5L,  1L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 5L), 
            .Label = c("A", "B", "C", "D", "E"), class = "factor"), 
             vbin1 = structure(c(1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L), 
            .Label = c("a", "b"), class = "factor")), 
            .Names = c("vnum1", "vnum2", "vint1", "vint2", "vfac1", "vfac2", "vch1", "vbin1"), 
            row.names = c(NA, -10L), class = "data.frame")
> 

grfour1 <- function(gdf, first, second, third, fourth){
    ggplot(gdf, aes(first, second)) + 
        geom_point(position = position_jitter(width = 0.2, height = 0)) + 
        facet_grid(third~fourth) 
}

错误在哪里?谢谢你的帮助

您的列名仅对
ggplot
函数可见,因为它使用数据作为其环境。它们在您的全局环境中对R不可见。要解决此问题,您的函数定义应为:

grfour1 <- function(gdf, first, second, third, fourth){
    ggplot(gdf, aes_string(first, second)) + 
      geom_point(position = position_jitter(width = 0.2, height = 0)) + 
      facet_grid(paste(third, "~", fourth)) 
}

这里有几个问题。首先,
ggplot
将列名解析为不带引号的
aes
,因此当您执行
aes(First,second)
时,
ggplot2
实际上查找名为
“First”
“second”
的列,而不是包含解析名称的变量。 第二个问题是R不知道什么是vints,他认为这是全局环境中的某个对象,因此,你需要告诉它这是一个使用“vint1”的字符

我的解决办法是

grfour1 <- function(gdf, first, second, third, fourth){
    gdf <- gdf[c(first, second, third, fourth)]
    names(gdf) <- c("first", "second", "third", "fourth") 
    ggplot(gdf, aes(first, second)) + 
      geom_point(position = position_jitter(width = 0.2, height = 0)) + 
      facet_grid(third ~ fourth) 
  }
library(ggplot2)
grfour1(rndf, "vint1", "vint2", "vch1", "vint2")

grfour1
grfour1我试过了,但得到了以下错误:>grfour1(ndf,“vnum1”,“vnum2”,“vch1”,“vbin1”)错误:铸造公式包含未在熔融数据中找到的变量:vch1,vbin1@rnso,您的数据不是称为
rndf
?因为您解析的是ndf
。。。无论如何,这段代码毫无疑问是有效的,我已经在您的数据上测试了好几次,我一定没有在前面附加它(rndf和ndf是相同的)。它工作得很好。非常感谢。我得到:>grfour1_ilir(rndf,“vint1”,“vint2”,“vch1”,“vint2”)错误:铸造公式包含未在熔融数据中找到的变量:vch1,vint2我得到:>grfour1_luke(rndf,“vint1”,“vint2”,“vch1”,“vint2”)错误:铸造公式包含未在熔融数据中找到的变量:“vch1”,“vint2”>grfour1_luke(rndf,vint1,vint2,vch1,vint2)错误:铸造公式包含未在熔融数据中找到的变量:vch1,vint2;>grfour1_luke(rndf,vint1,vint2,vch1,vint2)错误:铸造公式包含未在熔融数据中找到的变量:vch1,vint2>
grfour1(rndf, "vint1", "vint2", "vch1", "vint2")
grfour1 <- function(gdf, first, second, third, fourth){
    gdf <- gdf[c(first, second, third, fourth)]
    names(gdf) <- c("first", "second", "third", "fourth") 
    ggplot(gdf, aes(first, second)) + 
      geom_point(position = position_jitter(width = 0.2, height = 0)) + 
      facet_grid(third ~ fourth) 
  }
library(ggplot2)
grfour1(rndf, "vint1", "vint2", "vch1", "vint2")
grfour1 <- function(gdf, first, second, third, fourth){
  ggplot(gdf, aes_string(deparse(substitute(first)), deparse(substitute(second)))) + 
    geom_point(position = position_jitter(width = 0.2, height = 0)) +
    facet_grid(deparse(substitute(third~fourth))) 
}