数据丢失时条形图和tidyr的问题

时间:2017-12-04 21:03:34

标签: r ggplot2 tidyr

我试图绘制一个简单的条形图,其中包含一年中某些事件的计数。这是数据:

> DPUT(all.events)
structure(list(YEAR = c("1987", "1988", "1989", "1990", "1991", 
"1992", "1993", "1994", "1995", "1996", "1998", "2000", "2001", 
"2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", 
"2010", "2011", "2013", "2015", "2016"), count = c(3L, 4L, 2L, 
3L, 1L, 2L, 6L, 1L, 7L, 1L, 6L, 1L, 2L, 3L, 3L, 4L, 1L, 1L, 1L, 
2L, 2L, 10L, 1L, 2L, 1L, 3L)), .Names = c("YEAR", "count"), row.names = c(NA, 
-26L), class = "data.frame")

这是我的ggplot脚本:

ggplot(tidyr::complete(all.events,YEAR,fill=list(count=0)),
       aes(x=YEAR,y=count)) + 
geom_bar(stat="identity") +
scale_y_continuous(limit=c(0,15),expand=c(0, 0)) +
scale_x_discrete(breaks=c(1985,1990,1995,2000,2005,2010,2015),
                 labels=c(1985,1990,1995,2000,2005,2010,2015)) +
theme_bw(base_size = 13) +
xlab("") + ylab("No of events")

我希望complete能做什么,以及它以前做过什么,就是沿着x轴插入年份,其中count = 0。我得到的情节是下面的情节,其中有几年缺少对应于count = 0的地方:

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:1)

complete不会无中生有价值。来自?complete的帮助:

  

对于连续变量,您可能需要填写数据中未显示的值:为此,请使用year = 2010:2020year = full_seq(year)等表达式

因此,正如评论中所述,您应首先将YEAR转换为数字,但关键点是指定您需要完整序列(或手动YEAR = 1987:2016)。

tidyr::complete(all.events, full_seq(YEAR), fill = list(count=0))