我正在处理来自数据库的数据,这些数据经常被刷新,我希望有一个图形表示时间框架,当数据有良好的数据覆盖时,版本延迟到数据的实际日期最后刷新了。
# data example
name <- c("DATA1", "DATA2", "DATA3")
start <- c("1988-01-01","1994-01-01", "1988-01-01")
end <- c("2013-12-31","2013-05-31","2014-03-31")
refresh <- c("2014-02-28","2013-07-25","2014-05-20")
mydata <- data.frame(name, start, end, refresh)
# data preview
mydata
name start end refresh
1 DATA1 1988-01-01 2013-12-31 2014-02-28
2 DATA2 1994-01-01 2013-05-31 2013-07-25
3 DATA3 1988-01-01 2014-03-31 2014-05-20
然后我重塑数据(我不确定这是否是绝对必要的:
library(ggplot2)
library(reshape)
mdata <- melt(mydata, measure.vars = c("start", "end", "refresh"))
到目前为止,我只设法绘制了这样的开始 - 刷新日期:
ggplot(mdata, aes(as.Date(value, "%Y-%m-%d"), factor(name, levels=name))) +
geom_line(size=6) +
xlab("") + ylab("") +
theme_bw()
问题:
1:我似乎收到很多警告信息,即
Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
duplicated levels in factors are deprecated
3: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
duplicated levels in factors are deprecated
4: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, :
duplicated levels in factors are deprecated
2:我真正想要的是从开始到结束时使用黑色条形图以及用于刷新结束的红色条形
P.S。我只使用factor
位,因为我希望数据按原始表的顺序排序,否则它们将按表名的字母顺序排序(我的实际表名与本例不同)。 / p>
答案 0 :(得分:4)
发生错误是因为在factor(name, levels=name))
中存在重复的name
。你可能想要factor(name, levels=unique(name)))
如果您想使用geom_line
,则需要复制部分数据。您可以改为使用geom_linerange
:
mydata[,2:4] <- lapply(mydata[,2:4], as.Date)
library(ggplot2)
ggplot(mydata, aes(x=factor(name, levels=unique(name)))) +
geom_linerange(aes(ymin=start, ymax=end, colour="period1"), size=5) +
geom_linerange(aes(ymin=end, ymax=refresh, colour="period2"), size=5) +
coord_flip() +
scale_colour_manual(name="period", values=c("period1"="black", "period2"="red")) +
xlab("name")