简体   繁体   中英

Plotting time-series data with a gap in r?

I have a data set that has missing data from about July 7th to July 19th. Graph of my dataset . You can see the data gap pretty easily. I would like to truncate it so that the gap isnt there and the before and after data butt up against each other. Something like this . I did try to follow the linked example but I dont understand how they set up xseq. I also tried just removing the offending dates and creating a dataframe without them but that didnt solve the problem.

Im not sure if its helpful but here is the existing code for the graph:

    together <- ggplot() +
  stat_summary(data = grid_pad, aes(x = DTT, y = grid_value, fill = 'Ambient'), geom='ribbon', fun.data = mean_cl_quantile, alpha = 0.25) +
  stat_summary(data = grid_pad, aes(x = DTT, y = grid_value, color = 'Ambient'), geom='line', fun = mean, size = 0.9) +
  stat_summary(data = turtle_pad, aes(x = DTT, y = turtle_value, fill = 'Turtle'), geom='ribbon', fun.data = mean_cl_quantile, alpha = 0.25) +
  stat_summary(data = turtle_pad, aes(x = DTT, y = turtle_value, color = 'Turtle'), geom='line', fun = mean, size = 0.9) +
  labs(x = "Date", y = "Temperature")+
  scale_color_manual("Legend", values = c('Ambient' = '#1b9e77', 'Turtle' = '#d95f02'), labels = c(Ambient = 'Ambient Temp', Turtle = 'Turtle Temp')) +
  scale_fill_manual("Legend", values = c('Ambient' = '#1b9e77', 'Turtle' = '#d95f02'), labels = c(Ambient = 'Ambient Temp', Turtle = 'Turtle Temp')) +
  theme_classic() + 
  ggtitle("Ambient and Turtle Temperatures")+
  ggeasy::easy_center_title()+
  easy_remove_legend_title()
together

and here is the structure of my data:

> str(grid_pad)
grouped_df [142,800 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
         $ Logger    : Factor w/ 50 levels "TL1","TL11","TL12",..: 1 1 1 1 1 1 1 1 1 1 ...
         $ DTT       : POSIXct[1:142800], format: "2021-05-28 00:00:00" "2021-05-28 01:00:00" "2021-05-28 02:00:00" "2021-05-28 03:00:00" ...
         $ grid_value: num [1:142800] NA NA NA NA NA 19.5 19.5 19.5 20 22 ...
         - attr(*, "groups")= tibble [50 x 2] (S3: tbl_df/tbl/data.frame)
          ..$ Logger: Factor w/ 50 levels "TL1","TL11","TL12",..: 1 2 3 4 5 6 7 8 9 10 ...

> str(turtle_pad)
grouped_df [57,120 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
     $ Name        : Factor w/ 20 levels "F1","F11","F12",..: 1 1 1 1 1 1 1 1 1 1 ...
     $ DTT         : POSIXct[1:57120], format: "2021-05-28 00:00:00" "2021-05-28 01:00:00" "2021-05-28 02:00:00" "2021-05-28 03:00:00" ...
     $ turtle_value: num [1:57120] NA NA NA NA NA NA NA NA NA NA ...
     - attr(*, "groups")= tibble [20 x 2] (S3: tbl_df/tbl/data.frame)
      ..$ Name : Factor w/ 20 levels "F1","F11","F12",..: 1 2 3 4 5 6 7 8 9 10 ...
  

with base R, verbose:

df_with_gap <- data.frame(Name = gl(41, 1),
           DTT = as.Date(Sys.Date()) + (-20:20),
           turtle_value = c(runif(20), rep(NA, 5), runif(16))
           )

rows_to_keep  <- !is.na(df_with_gap$turtle_value)

## remove NAs
df_without_gap <- df_with_gap[rows_to_keep,]

## create some index to use for x-values ggplot
df_without_gap$pseudo_date  <- rownames(df)

Please note:

  • while you could use DTT of the remaining values to label your axis (see label argument in?scale_x_continuous`, the plot will be misleading as it covers up missing information)
  • a scatter plot would be the way to go if you want to show the association between ambient and turtle temperature.
  • to show seasonality of instead, consider adding a smoother ( ?geom_smooth for ggplot)
  • to convey variability, a boxplot might be more instructive
  • helpful chart pickers on the web

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM