Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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错误:无法强制类“;c(“gg”、“ggplot”)“;到数据帧_R_Ggplot2_Error Handling - Fatal编程技术网

R ggplot错误:无法强制类“;c(“gg”、“ggplot”)“;到数据帧

R ggplot错误:无法强制类“;c(“gg”、“ggplot”)“;到数据帧,r,ggplot2,error-handling,R,Ggplot2,Error Handling,我正在尝试运行一行非常简单的代码,以便为家庭作业创建一个ggplot。我对R非常陌生,所以我怀疑这是一个简单的问题,但我现在只是在踩水。实际上,我的教授编写了这段代码,它也适用于与我交谈过的其他学生。然而,我得到了一个错误,这是相当混乱 问题的一部分可能是我以前试图将ggplot强制转换为数据帧(因为我暂时不知道ggplot到底是什么),并将其命名为gg 自从我开始分配任务以来,这行代码一直在崩溃 注意:这是我的教授提供的代码,为其他人工作 ggplot(filter(gapminder, ga

我正在尝试运行一行非常简单的代码,以便为家庭作业创建一个ggplot。我对R非常陌生,所以我怀疑这是一个简单的问题,但我现在只是在踩水。实际上,我的教授编写了这段代码,它也适用于与我交谈过的其他学生。然而,我得到了一个错误,这是相当混乱

问题的一部分可能是我以前试图将ggplot强制转换为数据帧(因为我暂时不知道ggplot到底是什么),并将其命名为gg

自从我开始分配任务以来,这行代码一直在崩溃 注意:这是我的教授提供的代码,为其他人工作

ggplot(filter(gapminder, gapminder$year==1987, group=1)) + geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) + xlab("GDP per capita") + ylab("Life expectancy at birth")
我试图使用以下命令将ggplot强制为数据帧:

gg = as.data.frame(ggplot)
显然,这不起作用或没有帮助,但从文件中删除此代码后,它可能仍然会影响前一行代码

我希望至少有某种类型的绘图,但我得到了以下错误:

as.data.frame.default(x[[i]]中出错,可选=TRUE, stringsAsFactors=stringsAsFactors):无法强制类“c”(“gg”), “ggplot”)”到data.frame


任何帮助都将不胜感激

ggplot
将一个数据帧作为其输入,并创建一个plot对象,其中包含许多与生成该对象的所有参数相对应的片段。虽然从ggplot中提取数据在技术上是可行的,但这有点复杂,可能不会在介绍性的环节中。(有关此示例,请参见底部。)

基于我所看到的其他
ggplot
教程(比如它的创建者),更典型的做法是从显示数据帧开始,并显示过滤数据如何更改绘图

这是一个应该有效的过程。如果它不适合您,请分享您收到的任何特定错误消息

  • 重新启动R。如果您使用的是RStudio,请单击会话->重新启动R
  • 加载库。该示例至少使用了ggplot2和gapminder,也可能使用其他的

  • 看看数据框。下面是包含1704行的
    gapminder
    数据。每个国家的数据中每年都有一行,如1952年、1957年等

  • 将过滤后的数据发送到ggplot。
    ggplot
    函数的第一项表示输入数据。(我在这里省略了“group=1”,因为我不知道它是在哪里定义的。这个部分是否真的属于
    aes(…)
    part?
    group=1
    有时在我们希望ggplot提供某种统计数据时使用,我们希望它将整个数据集作为一个组来处理,例如,如果您希望得到所有国家的平均gdp,而不是各洲的单独平均值……)

  • 这是我得到的结果。有打嗝吗


    从ggplot对象中提取数据。 这是同一个图,分配给一个名为
    gg
    的对象:

    gg <- ggplot(filter(gapminder, gapminder$year==1987)) + 
            geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) + 
            xlab("GDP per capita") + 
            ylab("Life expectancy at birth")
    
    gg-gg[[“数据”]]
    #A tibble:142 x 6
    国家大陆年寿命Exp pop gdpPercap
    1阿富汗亚洲1987 40.8 13867957 852。
    2阿尔巴尼亚欧洲1987 72 3075321 3739。
    3阿尔及利亚非洲1987 65.8 23254956 5681。
    4安哥拉非洲1987 39.9 7874230 2430。
    5阿根廷美洲1987 70.8 31620918 9140。
    6澳大利亚大洋洲1987 76.3 16257249 21889。
    7奥地利欧洲1987 74.9 7578903 23688。
    8巴林亚洲1987 70.8 454612 18524。
    9孟加拉国亚洲1987 52.8 103764241 752。
    10比利时欧洲1987 75.4 9870200 22526。
    #…还有132行
    
    ggplot
    将一个数据帧作为其输入,并创建一个plot对象,其中包含许多与生成该对象的所有参数相对应的片段。虽然从ggplot中提取数据在技术上是可行的,但这有点复杂,可能不会在介绍性的环节中。(有关此示例,请参见底部。)

    基于我所看到的其他
    ggplot
    教程(比如它的创建者),更典型的做法是从显示数据帧开始,并显示过滤数据如何更改绘图

    这是一个应该有效的过程。如果它不适合您,请分享您收到的任何特定错误消息

  • 重新启动R。如果您使用的是RStudio,请单击会话->重新启动R
  • 加载库。该示例至少使用了ggplot2和gapminder,也可能使用其他的

  • 看看数据框。下面是包含1704行的
    gapminder
    数据。每个国家的数据中每年都有一行,如1952年、1957年等

  • 将过滤后的数据发送到ggplot。
    ggplot
    函数的第一项表示输入数据。(我在这里省略了“group=1”,因为我不知道它是在哪里定义的。这个部分是否真的属于
    aes(…)
    part?
    group=1
    有时在我们希望ggplot提供某种统计数据时使用,我们希望它将整个数据集作为一个组来处理,例如,如果您希望得到所有国家的平均gdp,而不是各洲的单独平均值……)

  • 这是我得到的结果。有打嗝吗


    从ggplot对象中提取数据。 这是同一个图,分配给一个名为
    gg
    的对象:

    gg <- ggplot(filter(gapminder, gapminder$year==1987)) + 
            geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) + 
            xlab("GDP per capita") + 
            ylab("Life expectancy at birth")
    
    gg-gg[[“数据”]]
    #A tibble:142 x 6
    国家大陆年寿命Exp pop gdpPercap
    1阿富汗亚洲1987 40.8 13867957 852。
    2阿尔巴尼亚欧洲1987 72 3075321 3739。
    3阿尔及利亚非洲1987 65.8 23254956 5681。
    4安哥拉非洲1987 39.9 7874230 2430。
    5阿根廷美洲1987 70.8 31620918 9140。
    6澳大利亚大洋洲1987 76.3 16257249 21889。
    7 A
    
    # Note: equivalent to `filter(gapminder, year == 1957)`
    > filter(gapminder, gapminder$year == 1957)
    # A tibble: 142 x 6
       country     continent  year lifeExp      pop gdpPercap
       <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
     1 Afghanistan Asia       1957    30.3  9240934      821.
     2 Albania     Europe     1957    59.3  1476505     1942.
     3 Algeria     Africa     1957    45.7 10270856     3014.
     4 Angola      Africa     1957    32.0  4561361     3828.
     5 Argentina   Americas   1957    64.4 19610538     6857.
     6 Australia   Oceania    1957    70.3  9712569    10950.
     7 Austria     Europe     1957    67.5  6965860     8843.
     8 Bahrain     Asia       1957    53.8   138655    11636.
     9 Bangladesh  Asia       1957    39.3 51365468      662.
    10 Belgium     Europe     1957    69.2  8989111     9715.
    # … with 132 more rows
    
    ggplot(filter(gapminder, gapminder$year==1987)) + 
      geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) + 
      xlab("GDP per capita") + 
      ylab("Life expectancy at birth")
    
    gg <- ggplot(filter(gapminder, gapminder$year==1987)) + 
            geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) + 
            xlab("GDP per capita") + 
            ylab("Life expectancy at birth")
    
    > gg[["data"]]
    # A tibble: 142 x 6
       country     continent  year lifeExp       pop gdpPercap
       <fct>       <fct>     <int>   <dbl>     <int>     <dbl>
     1 Afghanistan Asia       1987    40.8  13867957      852.
     2 Albania     Europe     1987    72     3075321     3739.
     3 Algeria     Africa     1987    65.8  23254956     5681.
     4 Angola      Africa     1987    39.9   7874230     2430.
     5 Argentina   Americas   1987    70.8  31620918     9140.
     6 Australia   Oceania    1987    76.3  16257249    21889.
     7 Austria     Europe     1987    74.9   7578903    23688.
     8 Bahrain     Asia       1987    70.8    454612    18524.
     9 Bangladesh  Asia       1987    52.8 103764241      752.
    10 Belgium     Europe     1987    75.4   9870200    22526.
    # … with 132 more rows