gpt4 book ai didi

r - ggplot错误: cannot coerce class “c(” gg“,”ggplot“)” to a data.frame

转载 作者:行者123 更新时间:2023-12-03 08:10:22 24 4
gpt4 key购买 nike

我试图运行一段非常简单的代码,以便为作业创建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)

显然这没有用或没有帮助,但是从文件中删除此代码后,它可能仍会影响前一行代码?

我期望至少有某种情节,但我收到以下错误:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class "c("gg", "ggplot")" to a data.frame



任何帮助,将不胜感激!

最佳答案

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

根据我见过的其他ggplot教程(例如其创建者的this one),更典型的做法是先显示输入的数据框,并说明过滤数据如何改变绘图。

这是一个应该起作用的过程。如果对您不起作用,请分享您收到的所有特定错误消息。

  • 重新启动R。如果您使用的是RStudio,请单击“ session ”->“重新启动R”。
  • 加载库。该示例至少使用ggplot2和gapminder,也许也使用其他方法。

  • library(ggplot2)
    library(gapminder)
    library(dplyr) # I think this is the source of the "filter" function used here

  • 查看数据框。这是gapminder数据,具有1,704行。数据中的每个国家/地区每年都有一行,例如1952、1957等。

  • > gapminder
    # A tibble: 1,704 x 6
    country continent year lifeExp pop gdpPercap
    <fct> <fct> <int> <dbl> <int> <dbl>
    1 Afghanistan Asia 1952 28.8 8425333 779.
    2 Afghanistan Asia 1957 30.3 9240934 821.
    3 Afghanistan Asia 1962 32.0 10267083 853.
    4 Afghanistan Asia 1967 34.0 11537966 836.
    5 Afghanistan Asia 1972 36.1 13079460 740.
    6 Afghanistan Asia 1977 38.4 14880372 786.
    7 Afghanistan Asia 1982 39.9 12881816 978.
    8 Afghanistan Asia 1987 40.8 13867957 852.
    9 Afghanistan Asia 1992 41.7 16317921 649.
    10 Afghanistan Asia 1997 41.8 22227415 635.
    # … with 1,694 more rows
  • 我们可以仅过滤1957年的数据。(我不确定group = 1部分的用途是什么-也许您的问题中没有提到更早的步骤?)

  • # 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中。 ggplot函数的第一项代表输入数据。 (我在这里省略了“group = 1”,因为我不知道它的定义位置。该部分可能实际上属于aes(...)部分吗?当我们希望ggplot在所需的位置提供某种统计信息时,有时会在此处使用group = 1将整个数据集视为一组,例如,如果要所有国家/地区的平均GDP而不是按洲划分的单独平均值...)

  • ggplot(filter(gapminder, gapminder$year==1987)) + 
    geom_point(aes(gdpPercap, lifeExp, color=continent, size=pop)) +
    xlab("GDP per capita") +
    ylab("Life expectancy at birth")

    这是我得到的输出。有打h吗?

    enter image description here

    从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对象结合了许多组件。在RStudio中,您可以检查它们并以交互方式提取组件。其中之一是源数据:

    enter image description here
    > 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

    关于r - ggplot错误: cannot coerce class “c(” gg“,”ggplot“)” to a data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55676254/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com