繁体   English   中英

为什么会出现“因子级别[2]重复”的错误?

[英]Why the error “factor level [2] is duplicated” will occur?

我尝试使用因子(月)将月份变量(整数)转换为分类变量,但由于错误而失败。 我该如何解决?

这是我的代码:

library(tidyverse)
library(dplyr)
install.packages("nycflights13")
library(nycflights13)
month_new <- flights$month
month_new
flights %>%
   filter(dest == "HNL", air_time > 10) %>%
   factor(month_new) %>%
   ggplot(x = month_new) + geom_bar()

您的分配factor(month_new)不起作用。 我建议mutate(month = as.factor(month))并且没有美学aes

library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)

  flights %>%
    filter(dest == "HNL", air_time > 10) %>%
    mutate(month = as.factor(month)) %>%
    ggplot(aes(x = month)) + 
    geom_bar()

或者:

library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)

flights %>%
  filter(dest == "HNL", air_time > 10) 

  ggplot(flights, aes(x=factor(month)))+
    geom_bar(fill="steelblue")+
    theme_minimal()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM