Home > Mobile >  Why does data appear to go missing when switching from geom_point to geom_line?
Why does data appear to go missing when switching from geom_point to geom_line?

Time:09-30

I have created a large data frame (BEAVOutletWL_merge) that I am trying to produce plots pulling data from the various columns. When I input the following code:

BEAVbaro <- ggplot(BEAVOutletWL_merge, aes(x=DateTime,y=BaroPres_kpa))  
geom_point(size = .01) 
labs(x=NULL, y="BaroPres_kpa")   ylim(98, 103) 
theme() ggtitle("Beaver Marsh")
BEAVbaro

This is the plot that is produced:

enter image description here

However, when I try to make it a line plot using this code:

BEAVbaro <- ggplot(BEAVOutletWL_merge, aes(x=DateTime,y=BaroPres_kpa))  
geom_line(size = .5) 
labs(x=NULL, y="BaroPres_kpa")   ylim(98, 103) 
theme() ggtitle("Beaver Marsh")
BEAVbaro

This plot is produced:

enter image description here

The only thing that I have changed is "geom_point" to "geom_line" and the size (for aesthetic purpose) but does anyone know why the large chunk of data between 01/22 and 07/22 suddenly disappears on the line plot?

Thanks!

CodePudding user response:

Because ggplot leaves blanks in lines where there are NA values, points that are flanked by NA values won't show up with geom_line. Example:

library(ggplot2)
library(cowplot)
dd <- data.frame(x = 1:10,
                 y  = c(1, NA, 3, NA, 5, NA, 7, NA, 9, 10))

gg0 <- ggplot(dd, aes(x,y))

plot_grid(gg0   geom_point(), gg0   geom_line())

enter image description here

If you're OK with linear interpolation, you can fix this with na.omit():

gg0  % % na.omit(dd)   geom_line()

enter image description here

It would be interesting but a little more of a challenge than I have time for right now to plot a placeholder (e.g. a short horizontal dash) at the location of every NA-flanked data point ..)

  • Related