ggplot省略了缺少数据的一天

时间:2019-05-30 04:52:06

标签: r ggplot2

我正在尝试绘制第一天和最后一天缺少值的数据。 ggplot只是在那天“吃”了

ggplot()+
     geom_col(data = x, aes(x = date, y = inf),fill= "cadetblue3", size = 0.1)

如何使R绘制整个日期(和时间)范围? 我尝试了一个小技巧,在开始时分配0,但这是一个肮脏的解决方案。

 x$spor[1] <-0 
 x$spor[nrow(x)] <-0 

This is the desired solution. 还有更好的建议吗?

Data:    

x  <- structure(list(date = structure(c(1465689600, 1465693200, 1465696800, 
1465700400, 1465704000, 1465707600, 1465711200, 1465714800, 1465718400, 
1465722000, 1465725600, 1465729200, 1465732800, 1465736400, 1465740000, 
1465743600, 1465747200, 1465750800, 1465754400, 1465758000, 1465761600, 
1465765200, 1465768800, 1465772400, 1465776000, 1465779600, 1465783200, 
1465786800, 1465790400, 1465794000, 1465797600, 1465801200, 1465804800, 
1465808400, 1465812000, 1465815600, 1465819200, 1465822800, 1465826400, 
1465830000, 1465833600, 1465837200, 1465840800, 1465844400, 1465848000, 
1465851600, 1465855200, 1465858800, 1465862400, 1465866000, 1465869600, 
1465873200, 1465876800, 1465880400, 1465884000, 1465887600, 1465891200, 
1465894800, 1465898400, 1465902000, 1465905600, 1465909200, 1465912800, 
1465916400, 1465920000, 1465923600, 1465927200, 1465930800, 1465934400, 
1465938000, 1465941600, 1465945200, 1465948800, 1465952400, 1465956000, 
1465959600, 1465963200, 1465966800, 1465970400, 1465974000, 1465977600, 
1465981200, 1465984800, 1465988400, 1465992000, 1465995600, 1465999200, 
1466002800, 1466006400, 1466010000, 1466013600, 1466017200, 1466020800, 
1466024400, 1466028000, 1466031600, 1466035200, 1466038800, 1466042400, 
1466046000, 1466049600, 1466053200, 1466056800, 1466060400, 1466064000, 
1466067600, 1466071200, 1466074800, 1466078400, 1466082000, 1466085600, 
1466089200, 1466092800, 1466096400, 1466100000, 1466103600, 1466107200, 
1466110800, 1466114400, 1466118000, 1466121600, 1466125200, 1466128800, 
1466132400, 1466136000, 1466139600, 1466143200, 1466146800, 1466150400, 
1466154000, 1466157600, 1466161200, 1466164800, 1466168400, 1466172000, 
1466175600, 1466179200, 1466182800, 1466186400, 1466190000, 1466193600, 
1466197200, 1466200800, 1466204400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), inf = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 18.4313, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 25.3237, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 22.9965, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, 11.7823, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = "data.frame", row.names = 2997018:2997161)

2 个答案:

答案 0 :(得分:2)

ggplot删除NA值。真正的问题是scale_x_datetime设置轴限制的方式。

如果您使用geom_point,则会看到完整的第一天:

x %>% 
  ggplot(aes(date, inf)) + geom_point()

enter image description here

但是,如果您使用geom_col,则轴的扩展是不同的:

x %>% 
  ggplot(aes(date, inf)) + geom_col()

enter image description here

因此,一种解决方法(如果需要的话)是调整expand()参数:

x %>% 
  ggplot(aes(date, inf)) + geom_col() + scale_x_datetime(expand = c(0.5, 0.5))

enter image description here

您指出的另一种方法是将NA替换为零。严格来讲,您不应该这样做,因为NA(未知值)与value = 0并不相同。就图表而言,这可能是可以的,除非该图使用任何统计转换。

library(dplyr)
x %>% 
  mutate(inf = ifelse(is.na(inf), 0, inf)) %>% 
  ggplot(aes(date, inf)) + geom_col()

enter image description here

答案 1 :(得分:2)

另一种选择是根据源数据的范围使用coord_cartesian:

ggplot()+
  geom_col(data = x, aes(x = date, y = inf),
           fill= "cadetblue3", size = 0.1) +
  coord_cartesian(xlim = range(x$date))

enter image description here