Extract point from ggplot and plot

First I have a dataset as shown below:

ID AB Type Time Date 1 12 13 R 23:20 1-1-01 1 13 12 F 23:40 1-1-01 1 13 11 F 00:00 2-1-01 1 15 10 R 00:20 2-1-01 1 12 06 W 00:40 2-1-01 1 11 09 F 01:00 2-1-01 1 12 10 R 01:20 2-1-01 so on... 

I tried to ggplot the above dataset for A and B.

 ggplot(data=dataframe, aes(x=A, y=B, colour = Type)) +geom_point()+geom_path() 

Problem:

  • How to add a subset variable that is viewed in the first 24 hours after each point F.

  • Currently, I have posted a continuous dataset [in time], but my original dataset is not continuous. How can I make my data set continuous with an interval of 10 minutes? I used the xspline () interpolation function on A and B, but I don’t know how to make my dataset lengthy,

The highlighted part presented below is what I am looking for, I want to extract this dataset and then build a new ggplot:

From the MarkusN graphs, this is what I'm looking for:

Take the first point as β€œF” and drive 24 hours from this point (since there is no 24 hour dataset available here so it can look like this):

Picture

+8
r ggplot2
source share
2 answers

First I created sample data. Hope this looks like your problem:

 df = data.frame(id=rep(1:9), A=c(12,13,13,14,12,11,12,11,10), B=c(13,12,10,12,6,9,10,11,12), Type=c("F","R","F","R","W","F","R","F","R"), datetime=as.POSIXct(c("2015-01-01 01:00:00","2015-01-01 22:50:00", "2015-01-02 08:30:00","2015-01-02 23:00:00", "2015-01-03 14:10:00","2015-01-05 16:30:00", "2015-01-05 23:00:00","2015-01-06 17:00:00", "2015-01-07 23:00:00")), stringsAsFactors = F) 

Your first question is to build the data by highlighting the first 24 hours after the F-point. For this task, I used dplyr and ggplot.

 library(dplyr) library(ggplot) df %>% mutate(nf = cumsum(Type=="F")) %>% # build F-to-F groups group_by(nf) %>% mutate(first24h = as.numeric((datetime-min(datetime)) < (24*3600))) %>% # find the first 24h of each F-group mutate(lbl=paste0(row_number(),"-",Type)) %>% ggplot(aes(x=A, y=B, label=lbl)) + geom_path(aes(colour=first24h)) + scale_size(range = c(1, 2)) + geom_text() 

enter image description here The problem here is that color changes only at some points. The only thing that doesn’t suit me is the use of different line colors for the path sections. If first24h is a discrete variable geom_path draws two sepearate paths. Therefore, I defined the variable as numeric. Maybe someone can improve this?

The second question about interpolation can be easily solved with the zoo package:

 library(zoo) full.time = seq(df$datetime[1], tail(df$datetime, 1), by=600) # new timeline with point at every 10 min d.zoo = zoo(df[,2:3], df$datetime) # convert to zoo object d.full = as.data.frame(na.approx(d.zoo, xout=full.time)) # interpolate; result is also a zoo object d.full$datetime = as.POSIXct(rownames(d.full)) 

With these two data frames, you get a solution. Each FF section is drawn in a separate graph, and only points are displayed that do not exceed 24 hours after the F point is displayed.

 df %>% select(Type, datetime) %>% right_join(d.full, by="datetime") %>% mutate(Type = ifelse(is.na(Type),"",Type)) %>% mutate(nf = cumsum(Type=="F")) %>% group_by(nf) %>% mutate(first24h = (datetime-min(datetime)) < (24*3600)) %>% filter(first24h == TRUE) %>% mutate(lbl=paste0(row_number(),"-",Type)) %>% filter(first24h == 1) %>% ggplot(aes(x=A, y=B, label=Type)) + geom_path() + geom_text() + facet_wrap(~ nf) 

enter image description here

+1
source share

I tried the following, maybe you can get an idea from here. I recommend that you first have a variable with a given time (either in minutes or in hours, in this example I used a clock). See if it helps

 #a data set is built as an example N = 100 set.seed(1) dataframe = data.frame(A = cumsum(rnorm(N)), B = cumsum(rnorm(N)), Type = sample(c('R','F','W'), size = N, prob = c(5/7,1/7,1/7), replace=T), time.h = seq(0,240,length.out = N)) # here, a list with dataframes is built with the sequences l_dfs = lapply(which(dataframe$Type == 'F'), function(i, .data){ transform(subset(.data[i:nrow(.data),], (time.h - time.h[1]) <= 24), t0 = sprintf('t0=%4.2f', time.h[1])) }, dataframe) ggplot(data=do.call('rbind', l_dfs), aes(x=A, y=B, colour=Type)) + geom_point() + geom_path(colour='black') + facet_wrap(~t0) 
+2
source share

All Articles