- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图运行一段非常简单的代码,以便为作业创建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")
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),更典型的做法是先显示输入的数据框,并说明过滤数据如何改变绘图。
这是一个应该起作用的过程。如果对您不起作用,请分享您收到的所有特定错误消息。
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
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
函数的第一项代表输入数据。 (我在这里省略了“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")
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[["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/
include.sh #!/bin/bash - export gg f() { for i in "${gg[@]}" do echo $i done } r
我的 ggplot 有问题,我想为某些值设置不同的形状。但是在图例中,所有东西都堆叠在一起,因此图例不再清晰。此外,我想从图例中删除日本或找到另一种方法将其设置为红色且形状=17。也许添加到过滤器 J
我正在使用 footable library用于显示表。每行都有一个日期列,我想按日期列(在列标题上单击)对该表进行排序。 Data Scadenza Titolo
当我尝试构建我的项目时,它返回以下错误: 错误 1 意外错误创建调试信息文件 'D:\Documents\Lance\Documents\School\Capstone\GG\GG\obj\Deb
我试图做到这一点: var curr = new Date; var first = curr.getDate() - curr.getDay(); var last = f
例如,假设我正在 Vim 中愉快地编辑文档,这时我不小心按下了 gg。然后我最终到达文档的第一行,当然我不知道我上次编辑的确切行是哪一行。有什么方法可以神奇地回到 gg 跳转之前最后编辑的行吗? 最佳
当我尝试使用 gg=G 修复 HTML 文件的缩进时,每一行都失去了缩进并变为左对齐。有人知道这里会发生什么吗? 测试.html Indent test
我正试图弄清楚如何解决这个问题。它没有打印任何内容,也没有发送 DM。 代码: async def on_dbl_vote(self, ctx, data): print("Recei
我正在尝试使用一个新类来扩展 ggplot2,我们将在本例中将其称为 foo。目标是编写一个 +.foo 方法来代替 +.gg。但是我遇到了“不兼容方法”的问题 设置 目前我可以编写 ggplot_a
全面警告:react-dom.development.js:2592 指定的值“”不符合要求的格式。格式为“#rrggbb”,其中 rr、gg、bb 是两位十六进制数。 代码运行良好。它正在做它应该做
我正在尝试使用一个新类来扩展 ggplot2,我们将在本例中将其称为 foo。目标是编写一个 +.foo 方法来代替 +.gg。但是我遇到了“不兼容方法”的问题 设置 目前我可以编写 ggplot_a
全面警告:react-dom.development.js:2592 指定的值“”不符合要求的格式。格式为“#rrggbb”,其中 rr、gg、bb 是两位十六进制数。 代码运行良好。它正在做它应该做
我正在尝试用 HTML5 制作一个多人游戏。我找到了 lance.gg 并四处游玩。我像这样修改了 Pong 游戏。 移除 Paddle(只剩下 Ball) 将重力设置为 (0, 0.1) 设置Bal
我正在尝试为 Oracle Golden Gate (12c) 配置 BigQuery 适配器。但不幸的是我无法理解这个gg.classpath。在文档中,他们提到从 GitHub 下载客户端库 但是
我的最终目标是使用 gg=G 自动缩进符合 eslintrc.js 文件的所有 JS 代码。 所以,目前我有 syntastic和 vim-javascript在我的 .vimrc 中查看我的 JS
我使用存储过程来检查表中是否存在名称,方法是使用以下代码片段.. BEGIN IF ids = 0 THEN SELECT * FROM table_name WHERE `table_id` = a
我想用 css 选择 child 的最后一个 该文本出现gg我只想选择 Last gg 请帮忙 ul { margin: 0; padding: 0; } .container ul li +
我试图运行一段非常简单的代码,以便为作业创建ggplot。我是R的新手,所以我怀疑这是一个简单的问题,但现在我只是在努力。我的教授实际上写了这段代码,并且已经为其他与我交谈过的学生工作过。但是,我遇到
通常,人们在 vim 中使用 gg=G 来自动切换 C/C++/Java 代码或实际上任何类型的代码。 但是,我在 vim 的默认设置中将 tabstop 变量更改为 tabstop=2,这样每当我按
我使用这段代码将数据导出到一个 csv 文件并且它有效: project_id = 'project_id' client = bigquery.Client() dataset_id = 'data
我是一名优秀的程序员,十分优秀!