获取 R Markdown 错误消息 - fortify(data, ...) 中的错误

发布于 2025-01-16 14:31:58 字数 1625 浏览 0 评论 0原文

错误消息:

fortify() 中出现错误: ! data 必须是数据框,或可通过 fortify() 强制转换的其他对象,而不是类 uneval 的 S3 对象。 您是否不小心将 aes() 传递给了 data 参数? 回溯:

  1. ggplot2::ggplot(aes(x = 因子(十年), y = 收入, 颜色 = 性别))
  2. ggplot2:::ggplot.default(...)
  3. ggplot2:::fortify.default(数据, ... ) 显示回溯 fortify(data, ...) 中的错误:

我的代码

  ``` {r pressure, message=FALSE, echo=FALSE}

setwd(dirname(rstudioapi::getSourceEditorContext()$path))
load("file.RData")

filter(decade == 1980 | decade == 2010) +
ggplot(aes(x = factor(decade), y = income, colour = sex)) +
geom_boxplot() +
facet_grid(cols = vars(race)) +
scale_y_log10() +
ggtitle("Race and Gender Pay Gap - Visual 1") +
labs(y= "Annual Income", x = "Year") +
labs(colour = "Gender") +
theme_bw() +
scale_color_brewer(palette = "Set2")

这就是我的数据集的样子:

  year sex     age income degree race     ID decade
 <dbl> <chr> <dbl>  <dbl> <chr>  <chr> <dbl>  <dbl>
1  1981 male     34  10000 yes    white     1   1980
2  1981 male     32   9095 no     black     2   1980
3  2009 male     64  45200 no     black     3   2000
4  1999 male     50  25000 no     white     4   1990
5  1990 male     26  24500 no     white     5   1990
6  2011 male     39  46500 yes    white     6   2010
7  2007 male     40  60000 no     white     7   2000
8  1990 male     47  39200 no     white     8   1990
9  1981 male     30  20500 no     white     9   1980
10  2007 male     55  33000 no     white    10   2000

任何帮助将不胜感激!谢谢:D

Error message:

Error in fortify():
! data must be a data frame, or other object coercible by fortify(), not an S3 object with class uneval.
Did you accidentally pass aes() to the data argument?
Backtrace:

  1. ggplot2::ggplot(aes(x = factor(decade), y = income, colour = sex))
  2. ggplot2:::ggplot.default(...)
  3. ggplot2:::fortify.default(data, ...)
    Show Traceback
    Error in fortify(data, ...) :

My code

  ``` {r pressure, message=FALSE, echo=FALSE}

setwd(dirname(rstudioapi::getSourceEditorContext()$path))
load("file.RData")

filter(decade == 1980 | decade == 2010) +
ggplot(aes(x = factor(decade), y = income, colour = sex)) +
geom_boxplot() +
facet_grid(cols = vars(race)) +
scale_y_log10() +
ggtitle("Race and Gender Pay Gap - Visual 1") +
labs(y= "Annual Income", x = "Year") +
labs(colour = "Gender") +
theme_bw() +
scale_color_brewer(palette = "Set2")

This is what my data set looks like:

  year sex     age income degree race     ID decade
 <dbl> <chr> <dbl>  <dbl> <chr>  <chr> <dbl>  <dbl>
1  1981 male     34  10000 yes    white     1   1980
2  1981 male     32   9095 no     black     2   1980
3  2009 male     64  45200 no     black     3   2000
4  1999 male     50  25000 no     white     4   1990
5  1990 male     26  24500 no     white     5   1990
6  2011 male     39  46500 yes    white     6   2010
7  2007 male     40  60000 no     white     7   2000
8  1990 male     47  39200 no     white     8   1990
9  1981 male     30  20500 no     white     9   1980
10  2007 male     55  33000 no     white    10   2000

Any help would be super appreciated!! Thanks :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千柳 2025-01-23 14:31:58

您的代码运行,可能需要添加

data %>% 
      filter(decade == 1980 | decade == 2010) %>% before the `ggplot()`

示例代码:

data %>% 
  filter(decade == 1980 | decade == 2010) %>% 
  ggplot(aes(x = factor(decade), y = income, colour = sex)) +
  geom_boxplot() +
  facet_grid(cols = vars(race)) +
  scale_y_log10() +
  ggtitle("Race and Gender Pay Gap - Visual 1") +
  labs(y= "Annual Income", x = "Year") +
  labs(colour = "Gender") +
  theme_bw() +
  scale_color_brewer(palette = "Set2")

基于您的评论的解决方案:

 df<- gender_pay_gap %>%  # in my sample data gender_pay_gap is called data
      filter(decade == 1980 | decade == 2010) 


ggplot(df,aes(x = factor(decade), y = income, colour = sex)) + 
  geom_boxplot() +
  facet_grid(cols = vars(race)) +
  scale_y_log10() +
  ggtitle("Race and Gender Pay Gap - Visual 1") +
  labs(y= "Annual Income", x = "Year") +
  labs(colour = "Gender") +
  theme_bw() +
  scale_color_brewer(palette = "Set2")

情节:

在此处输入图像描述

示例数据:

data<-structure(list(year = c(1981L, 1981L, 2009L, 1999L, 1990L, 2011L, 
2007L, 1990L, 1981L, 2007L), sex = c("male", "male", "male", 
"male", "male", "male", "male", "male", "male", "male"), age = c(34L, 
32L, 64L, 50L, 26L, 39L, 40L, 47L, 30L, 55L), income = c(10000L, 
9095L, 45200L, 25000L, 24500L, 46500L, 60000L, 39200L, 20500L, 
33000L), degree = c("yes", "no", "no", "no", "no", "yes", "no", 
"no", "no", "no"), race = c("white", "black", "black", "white", 
"white", "white", "white", "white", "white", "white"), ID = 1:10, 
    decade = c(1980L, 1980L, 2000L, 1990L, 1990L, 2010L, 2000L, 
    1990L, 1980L, 2000L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))

Your code runs, probably need to add

data %>% 
      filter(decade == 1980 | decade == 2010) %>% before the `ggplot()`

Sample code:

data %>% 
  filter(decade == 1980 | decade == 2010) %>% 
  ggplot(aes(x = factor(decade), y = income, colour = sex)) +
  geom_boxplot() +
  facet_grid(cols = vars(race)) +
  scale_y_log10() +
  ggtitle("Race and Gender Pay Gap - Visual 1") +
  labs(y= "Annual Income", x = "Year") +
  labs(colour = "Gender") +
  theme_bw() +
  scale_color_brewer(palette = "Set2")

Solution-based on your comment:

 df<- gender_pay_gap %>%  # in my sample data gender_pay_gap is called data
      filter(decade == 1980 | decade == 2010) 


ggplot(df,aes(x = factor(decade), y = income, colour = sex)) + 
  geom_boxplot() +
  facet_grid(cols = vars(race)) +
  scale_y_log10() +
  ggtitle("Race and Gender Pay Gap - Visual 1") +
  labs(y= "Annual Income", x = "Year") +
  labs(colour = "Gender") +
  theme_bw() +
  scale_color_brewer(palette = "Set2")

Plot:

enter image description here

Sample data:

data<-structure(list(year = c(1981L, 1981L, 2009L, 1999L, 1990L, 2011L, 
2007L, 1990L, 1981L, 2007L), sex = c("male", "male", "male", 
"male", "male", "male", "male", "male", "male", "male"), age = c(34L, 
32L, 64L, 50L, 26L, 39L, 40L, 47L, 30L, 55L), income = c(10000L, 
9095L, 45200L, 25000L, 24500L, 46500L, 60000L, 39200L, 20500L, 
33000L), degree = c("yes", "no", "no", "no", "no", "yes", "no", 
"no", "no", "no"), race = c("white", "black", "black", "white", 
"white", "white", "white", "white", "white", "white"), ID = 1:10, 
    decade = c(1980L, 1980L, 2000L, 1990L, 1990L, 2010L, 2000L, 
    1990L, 1980L, 2000L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文