【发布时间】:2018-02-22 03:32:42
【问题描述】:
我正在尝试按年进行时间序列 ggplot2。我的问题是某些列有 .而不是 NA。似乎我的变量是因子而不是数字。
数据集
DATE IR IQ
9/1/1983 77.6 85.7
10/1/1983 . .
11/1/1983 . .
12/1/1983 78 85.4
df_temp <- read.csv("",na.strings = "")
IR.factor <- factor(IR)
IQ.factor <- factor(IQ)
as.numeric(IR.factor)
as.numeric(IQ.factor)
head(df_temp)
str(df_temp)
df_temp <- df_temp[rowSums(is.na(df_temp)) != ncol(df_temp), ]
ggplot(aes(x=date, weight=value, fill=variable), data=df_temp) +
geom_bar() +
labs(x='DATE', y='Index 2000=100, Not Seasonally Adjusted') +
labs(color='Legend') +
scale_fill_discrete(labels = c('IR.factor',
'IQ.factor'
)) +
scale_y_continuous(breaks = round(seq(0, 200, by = 20), 1)) +
scale_x_date(date_breaks = '5 year', date_minor_breaks = '5 year',
date_labels = '%Y') +
theme(plot.title = element_text(lineheight=.7, face='bold')) +
theme(legend.position='bottom')
非常感谢任何建议
【问题讨论】:
-
阅读“。”作为 NA,请参阅此答案,例如:stackoverflow.com/a/19126403/680068
-
除了将您的期间读取为 NA 之外,您可能还希望使用不将列作为因子读取的函数来读取文件。我相信 readr::read_csv() 是一个不错的选择。
-
感谢您的“。”作为 NA 提示。但是我仍然收到错误“不知道如何为函数类型的对象自动选择比例。默认为连续。FUN(X [[i]],...)中的错误:找不到对象'变量' "