r - 为什么会出现“因子级别[2]重复”的错误?
问题描述
我尝试使用因子(月)将月份变量(整数)转换为分类变量,但由于错误而失败。我该如何解决?
这是我的代码:
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()
推荐阅读
- excel - 为什么在 VBA 中使用 VLookup 引用另一个 Excel 文件时会出现 #N/A?
- amazon-web-services - 加密环境变量并在 terraform 代码中调用它
- python - 使用 miniupnp 在 Internet 上打开套接字服务器
- python - 在 Python 中返回一个连续的终端日志
- javascript - 使函数返回它内部的承诺的结果
- google-cloud-platform - 在 BigQuery 中模拟窗口聚合的 UDF 的解决方法?
- r - 如何在堆叠图中使用 scale_color_discrete 格式化图例并在 y 轴 r 中包含千位分隔符
- reactjs - firebase 实时数据库规则的安全性
- javascript - 一个简单的布尔值可以用作异步代码的锁吗?
- apache-kafka - 如何在Kafka中使用事务以及如何使用abortTransaction?