在使用带有日期变量的数据框时,我遇到了 ggplot2 faceting 和 dplyr group_by 的错误。仅当我首先转换日期变量然后融化数据框时才会发生此错误。如果我做相反的事情,变量看起来完全一样,但不会出错。一个例子:
#base df
df <- data.frame(
id = c("A", "B", "C"),
date1 = c("12/Sep/2010", "13/Mar/2011", "05/Jan/2010"),
date2 = c("13/Sep/2010", "14/Mar/2011", "06/Jan/2010"),
value1 = 1:3,
value2 = 4:6
)
df
id date1 date2 value1 value2
1 A 12/Sep/2010 13/Sep/2010 1 4
2 B 13/Mar/2011 14/Mar/2011 2 5
3 C 05/Jan/2010 06/Jan/2010 3 6
我将展示带有 mutate 的示例,但使用 df$date <- as.Date(df$date) 会给出相同的错误。我很抱歉,或者整理我的数据的丑陋和低效的代码(建议赞赏:-))。
#mutate first
df_muta <- df %>% mutate_each(funs(as.Date(., format = "%d/%b/%Y")), c(starts_with("date")))
df_muta <- data.frame(
id = melt(df_muta, id.vars = c("id"), measure.vars = c("date1", "date2"))[[1]],
date = melt(df_muta, id.vars = c("id"), measure.vars = c("date1", "date2"))[[3]],
value = melt(df_muta, id.vars = c("id"), measure.vars = c("value1", "value2"))[[3]])
str(df_muta)
'data.frame': 6 obs. of 3 variables:
$ id : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3
$ date : Date, format: "2010-09-12" "2011-03-13" "2010-01-05" ...
$ value: int 1 2 3 4 5 6
p <- ggplot(df_muta, aes(x = date, y = value)) + geom_point()
我想发布剧情,但还没有 10 名声望。上面的单图没问题,日期在 x 轴上。如果我尝试刻面,x 轴将转换为数字。
p + facet_wrap( ~ id)
如果我尝试使用 dplyr group_by 它也会出错。
df_muta %>% group_by(id)
Error: column 'date' has unsupported type
所以我尝试先融化,然后转换日期。
df_melt <- data.frame(
id = melt(df, id.vars = c("id"), measure.vars = c("date1", "date2"))[[1]],
date = melt(df, id.vars = c("id"), measure.vars = c("date1", "date2"))[[3]],
value = melt(df, id.vars = c("id"), measure.vars = c("value1", "value2"))[[3]])
df_melt <- df_melt %>% mutate(date = as.Date(date, format = "%d/%b/%Y"))
str(df_melt)
'data.frame': 6 obs. of 3 variables:
$ id : Factor w/ 3 levels "A","B","C": 1 2 3 1 2 3
$ date : Date, format: "2010-09-12" "2011-03-13" "2010-01-05" ...
$ value: int 1 2 3 4 5 6
两个数据框的结构和值似乎完全相同,但最后一个不会对分面图轴或 group_by 产生任何错误。它是一个错误吗?日期对象之间的区别在哪里?
谢谢!