ASH
ASH

Reputation: 20322

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

I'm getting this error:

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

Here is the code.

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()

Can someone tell me what's wrong here? I don't even see an 'f' in the code. I don't see it as a variable, or anything at all.

I'm following the example here.

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

Upvotes: 4

Views: 17813

Answers (1)

Teemu Daniel Laajala
Teemu Daniel Laajala

Reputation: 2366

Your initial error lies inside the function fct_lump inside mutate inside the code chunk:

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)

If you inspect the 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 is the first parameter passed on to your fct_lump function and it is saved in by_major_category, but it is not a factor or a character string.

Taking a quick search for the particular code, the correctly formulated chunk uses Major_category as 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)

(Found from the original raw https://github.com/dgrtwo/data-screencasts/blob/master/college-majors.Rmd which your code tries to follow but differs in few crucial spots)

This produces your correct plot. In other words, you should be pointing to the factor in by_major_category$Major_category and not its parent data structure.

Deducing from the typo in your code chunk and an odd knitr::knit_exit() as well as omitting the text This is scrap work. from the linked source, I presume you're manually typing the code from a compiled knitr-document such as a PDF and your errors are due to manual cut-paste / typing. I suggest you refer to the original .Rmd-file, which stands for R Markdown and probably is currently in your use as in HTML/PDF/Word instead of raw R source.

Here is the figure the corrected code chunk should create:

Corrected code chunk

Upvotes: 6

Related Questions