Home > Software design >  Visualize start and end of activities as geom_segments relative to base line activity
Visualize start and end of activities as geom_segments relative to base line activity

Time:02-17

I have a table such as this:

df <- structure(list(Line = c("130", "131", "132", "133", "134", "135", 
                              "136", "137", "139", "140", "141"), 
                     Actor = c("R", "R", "R", "R", "B", "R", "B", "B", "M", "M", "M"), 
                     Act_cat = c("ver", "SpeechRec", "ges", "ges", "gaze", "ges", "gaze", "gaze", "gaze", "gaze", "gaze"), 
                     Activity = c("dort drüben könnt ihr sehen wer damals auf der sparrenburg gewohnt hat",
                                  "schwert", "D-onset", "D-peak", "~", "D-retract", "@tum", "~", "~", "@tum", "~"),
                     Starttime_ms = c(48825, 48865, 49220, 50080, 50730, 50900, 
                                      51009, 51191, 51486, 51809, 52251), 
                     Endtime_ms = c(53035, 49865, 50080, 50900, 51009, 52220, 51191, 51270, 51808, 52250, 
                                                                                         52332), 
                     Duration = c(4210, 1000, 860, 820, 279, 1320, 182, 79, 322, 441, 81)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -11L), 
                groups = structure(list(File = "VP_4_004", .rows = structure(list(1:11), ptype = integer(0), class = c("vctrs_list_of","vctrs_vctr", "list"))), 
                                   row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

I want to visualize how the Activitys after the first temporally relate to that first Activity, namely "dort drüben könnt ihr sehen wer damals auf der sparrenburg gewohnt hat". The kind of visualization I have in mind would involve the use of geom_segments for each Activity, their positions and end points would be determined by their Starttime_ms and, respectively, Endtime_ms values and could look roughly like this:

enter image description here

How can this be obtained? Thanks in advance!

CodePudding user response:

Here's a quick first go. You could potentially use geom_tile, geom_rect, or geom_segment here, depending on how you want to specify the position/size. I think geom_tile might be simplest, but its x/y is based on the centerpoint.

ggplot(df, aes( y = Act_cat))  
  geom_tile(aes(x = Starttime_ms   Duration/2, width = Duration,  height = 0.8, fill = Actor), color = "white")  
  geom_text(aes(x = Starttime_ms, label = stringr::str_wrap(Activity, 30)), hjust = 0)

enter image description here

  • Related