R错误:错误:`f`必须是一个因子(或字符向量)

时间:2019-02-03 16:25:06

标签: r

我收到此错误:

Error: `f` must be a factor (or character vector)

这是代码。

library(tidyverse)
library(scales)
theme_set(theme_light())

recent_grads <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/recent-grads.csv")
head(recent_grads)

# recent_grads <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/college-majors/recent-grads.csv")
majors_processed <- recent_grads %>%
  arrange(desc(Median)) %>%
  mutate(Major = str_to_title(Major),
         Major = fct_reorder(Major, Median))


by_major_category <- majors_processed %>%
  filter(!is.na(Total)) %>%
  group_by(Major_category) %>%
  summarize(Men = sum(Men),
            Women = sum(Women),
            Total = sum(Total),
            MedianSalary = sum(as.numeric(Median * Sample_size)) / sum(Sample_size)) %>%
  mutate(ShareWomen = Women / Total) %>%
  arrange(desc(ShareWomen))



majors_processed %>%
  mutate(Major_category = fct_reorder(Major_category, Median)) %>%
  ggplot(aes(Major_category, Median, fill = Major_category)) +
  geom_boxplot()



majors_processed %>%
  arrange(desc(Total)) %>%
  head(20) %>%
  mutate(Major = fct_reorder(Major, Total)) %>%
  gather(Gender, Number, Men, Women) %>%
  ggplot(aes(Major, Number, fill = Gender)) +
  geom_col() +
  coord_flip()


library(ggrepel)
by_major_category %>%
  mutate(Major_category = fct_lump(by_major_category, 6)) %>%
  ggplot(aes(ShareWomen, MedianSalary, color = by_major_category)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_text_repel(aes(label = by_mjor_category), force = .2) +
  expand_limits(y = 0)


library(plotly)
g <- majors_processed %>%
  mutate(Major_category = fct_lump(Major_category, 4)) %>%
  ggplot(aes(ShareWomen, Median, color = Major_category, size = Sample_size, label = Major)) +
  geom_point() +
  geom_smooth(aes(group = 1), method = "lm") +
  scale_x_continuous(labels = percent_format()) +
  scale_y_continuous(labels = dollar_format()) +
  expand_limits(y = 0)
ggplotly(g)


library(plotly)
g <- majors_processed %>%
  mutate(Major_category = fct_lump(Major_category, 4)) %>%
  ggplot(aes(ShareWomen, Median, color = Major_category, size = Sample_size, label = Major)) +
  geom_point() +
  geom_smooth(aes(group = 1), method = "lm") +
  scale_x_continuous(labels = percent_format()) +
  scale_y_continuous(labels = dollar_format()) +
  expand_limits(y = 0)
ggplotly(g)


library(broom)
majors_processed %>%
  select(Major, Major_category, Total, ShareWomen, Sample_size, Median) %>%
  add_count(Major_category) %>%
  filter(n >= 10) %>%
  nest(-Major_category) %>%
  mutate(model = map(data, ~ lm(Median ~ ShareWomen, data = ., weights = Sample_size)),
         tidied = map(model, tidy)) %>%
  unnest(tidied) %>%
  filter(term == "ShareWomen") %>%
  arrange(estimate) %>%
  mutate(fdr = p.adjust(p.value, method = "fdr"))

majors_processed %>%
  filter(Sample_size >= 100) %>%
  mutate(IQR = P75th - P25th) %>%
  arrange(desc(IQR))



majors_processed %>%
  ggplot(aes(Sample_size, Median)) +
  geom_point() +
  geom_text(aes(label = Major), check_overlap = TRUE, vjust = 1, hjust = 1) +
  scale_x_log10()

knitr::knit_exit()


# What were the most common *majors*? (Since there were 173, we're not going to show them all).
majors_processed %>%
  mutate(Major = fct_reorder(Major, Total)) %>%
  arrange(desc(Total)) %>%
  head(20) %>%
  ggplot(aes(Major, Total, fill = Major_category)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(labels = comma_format()) +
  labs(x = "",
       y = "Total # of graduates")



majors_processed %>%
  group_by(Major_category) %>%
  summarize(Median = median(Median)) %>%
  mutate(Major_category = fct_reorder(Major_category, Median)) %>%
  ggplot(aes(Major_category, Median)) +
  geom_col() +
  scale_y_continuous(labels = dollar_format()) +
  coord_flip()


# What are the lowest earning majors?
majors_processed %>%
  filter(Sample_size >= 100) %>%
  tail(20) %>%
  ggplot(aes(Major, Median, color = Major_category)) +
  geom_point() +
  geom_errorbar(aes(ymin = P25th, ymax = P75th)) +
  expand_limits(y = 0) +
  coord_flip()

有人可以告诉我这是怎么回事吗?我什至在代码中都看不到“ f”。我不认为它是变量或任何东西。

我在这里遵循示例。

https://github.com/dgrtwo/data-screencasts/blob/master/college-majors.Rmd

1 个答案:

答案 0 :(得分:2)

您的初始错误位于代码块内fct_lump内的函数mutate内:

by_major_category %>%
  mutate(Major_category = fct_lump(by_major_category, 6)) %>%
  ggplot(aes(ShareWomen, MedianSalary, color = by_major_category)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_text_repel(aes(label = by_major_category), force = .2) +
### Further, typo below
#  geom_text_repel(aes(label = by_mjor_category), force = .2) +
  expand_limits(y = 0)

如果您检查fct_lump

> mutate(Major_category = fct_lump(by_major_category, 6))
Error: `f` must be a factor (or character vector).
> fct_lump(by_major_category, 6)
Error: `f` must be a factor (or character vector).
> ?fct_lump
> # f: A factor (or character vector).
> class(by_major_category)
[1] "tbl_df"     "tbl"        "data.frame"

f是传递给fct_lump函数的第一个参数,它保存在by_major_category中,但它不是一个因素或字符串。

快速搜索特定代码,正确编写的块将Major_category用作f

by_major_category %>%
  mutate(Major_category = fct_lump(Major_category, 6)) %>%
  ggplot(aes(ShareWomen, MedianSalary, color = Major_category)) +
  geom_point() +
  geom_smooth(method = "lm") +
  geom_text_repel(aes(label = Major_category), force = .2) +
  expand_limits(y = 0)

(从您的代码尝试遵循的原始原始https://github.com/dgrtwo/data-screencasts/blob/master/college-majors.Rmd中找到,但在几个关键点上有所不同)

这将产生正确的图。换句话说,您应该指向by_major_category$Major_category中的因子,而不是其父数据结构。

从代码块中的错字和奇数knitr::knit_exit()推论出,并从链接源中省略文本This is scrap work.,我想您是从已编译的knitr文档中手动键入代码的(例如PDF),而您的错误则是由于手动剪切/输入造成的。我建议您参考原始的.Rmd文件,该文件代表R Markdown,并且可能正在使用中,如HTML / PDF / Word一样,而不是原始R源。

这是校正后的代码块应创建的数字:

Corrected code chunk