Reputation: 516
I am trying to plot data which has missing values for the first and last day. ggplot
simply 'eats' that day:
ggplot()+
geom_col(data = x, aes(x = date, y = inf),fill= "cadetblue3", size = 0.1)
How do I make R plot the whole range of date(and time)? I have tried a small trick to assign 0 at the beginning, but that is a dirty solution.
x$spor[1] <-0
x$spor[nrow(x)] <-0
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)
Upvotes: 0
Views: 113
Reputation: 66415
Another option would be to use coord_cartesian based on the range of the source data:
ggplot()+
geom_col(data = x, aes(x = date, y = inf),
fill= "cadetblue3", size = 0.1) +
coord_cartesian(xlim = range(x$date))
Upvotes: 2
Reputation: 33782
ggplot
removes NA values. The real issue here is the way that scale_x_datetime
is setting the axis limits.
If you use geom_point
, you see the complete first day:
x %>%
ggplot(aes(date, inf)) + geom_point()
But if you use geom_col
, the expansion of the axis is different:
x %>%
ggplot(aes(date, inf)) + geom_col()
So one solution, if you want bars, is to adjust the expand()
parameters:
x %>%
ggplot(aes(date, inf)) + geom_col() + scale_x_datetime(expand = c(0.5, 0.5))
Another way as you indicated is to replace NA with zero. Strictly-speaking you should not, since NA (value not known) is not the same as value = 0. It may be OK for the purposes of a chart, unless the plot uses any statistical transformations.
library(dplyr)
x %>%
mutate(inf = ifelse(is.na(inf), 0, inf)) %>%
ggplot(aes(date, inf)) + geom_col()
Upvotes: 2