Home > Back-end >  Rearrange vertical order of tiles in `geom_tile`
Rearrange vertical order of tiles in `geom_tile`

Time:02-18

I have data like this:

target_seq <- structure(list(Line = c("074", "075", "076", "077", "078", "079", 
                                      "080", "081", "082", "083", "084", "085", "086", "087", "088", 
                                      "089", "090", "091", "092", "093", "094", "095", "096", "097", 
                                      "098", "099", "100", "101", "102"), 
                             Actor = c("R", "W", "R", "G", "W", "B", "R", "G", "G", "B", "B", "B", "B", "G", "G", "R", 
                                       "G", "R", "G", "G", "G", "G", "B", "G", "B", "W", "W", "G", "G"), 
                             Activity = c("dort drüben könnt ihr sehen wer damals auf der sparrenburg gewohnt hat", 
                                          "~", "hey; roboter", "@tab", "@R", "@R", "D-onset", "~", "@B", 
                                          "~", "@armor", "~", "@R", "~", "@R", "D-peak", "~", "D-retract", 
                                          "@mark", "~", "@tum", "~", "~", "@tum", "@mark", "~", "@tum", "~", "@ew "), 
                            Starttime_ms = c(36985, 37002, 
                                             37023, 37043, 37081, 37111, 37240, 37245, 37327, 37432, 37594, 
                                             37715, 37795, 38008, 38050, 38215, 38885, 38900, 38967, 39408, 
                                             39489, 39851, 39998, 40012, 40039, 40409, 40637, 40733, 40935), 
                            Endtime_ms = c(41025, 37081, 38023, 37244, 40408, 37431, 38210, 
                                           37326, 38007, 37593, 37714, 37795, 39997, 38049, 38884, 38900, 
                                           38966, 40250, 39407, 39488, 39850, 40011, 40039, 40732, 41561, 
                                           40636, 41198, 40934, 41497), 
                            Duration = c(4040, 79, 1000, 201, 
                                         3327, 320, 970, 81, 680, 161, 120, 80, 2202, 41, 834, 685, 81, 
                                         1350, 440, 80, 361, 160, 41, 720, 1522, 227, 561, 201, 562), 
                             File = c("VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", 
                                      "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", 
                                      "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", 
                                      "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", 
                                      "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", 
                                      "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007", "VP_4_007"), 
                            Action = c("R_ver", "W_gaze", "R_SpeechRec", "G_gaze", 
                                           "W_gaze", "B_gaze", "R_ges", "G_gaze", "G_gaze", "B_gaze", 
                                           "B_gaze", "B_gaze", "B_gaze", "G_gaze", "G_gaze", "R_ges", 
                                           "G_gaze", "R_ges", "G_gaze", "G_gaze", "G_gaze", "G_gaze", 
                                           "B_gaze", "G_gaze", "B_gaze", "W_gaze", "W_gaze", "G_gaze", 
                                           "G_gaze")), 
                        class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -29L))

To plot the Duration of each Activity:

ggplot(target_seq,
       aes(y = Action))  
  # draw tiles for each `Activity` (width of tiles determined by `Duration`):
  geom_tile(aes(x = Starttime_ms   Duration/2, width = Duration, height = 0.5, fill = Actor), color = "white")  
  # print text of `Activity`s:
  geom_text(aes(x = Starttime_ms, label = Activity), hjust = 0, size = 3.5)  
  # define labels for x- and y-axis and plot title:
  labs(
    # x- axis label:
    x = "Activity duration",
    # y-axis label:
    y = "Actors and activity type",
    # plot title:
    title = paste0("Activities during robot's utterance\nFile: ", 
                   target_seq$File, 
                   " [", 
                   min(target_seq$Line, na.rm = TRUE),
                   " - ",
                   max(target_seq$Line, na.rm = TRUE),
                   "]"))  
  theme_light()

The resulting plot is okay except that what should be the first (top most) tile is placed second:

enter image description here

How can I rearrange the vertical order of appearance of the tiles such that Action == "R_ver" is the top-most tile in the plot?

CodePudding user response:

You can either set the factor and save it back to the dataframe (as stated in the comment by @Allan Cameron), or you can change it inside ggplot. In this way, your original target_seq$Action will not be modified into factor.

Here I just set the factor level as the order present in your target_seq, but in reality, you can set it manually by supplying a vector of levels as in factor(levels = c("R_ver", ...)).

library(tidyverse)

ggplot(target_seq,
       aes(y = factor(Action, levels = unique(rev(Action)))))  
  # draw tiles for each `Activity` (width of tiles determined by `Duration`):
  geom_tile(aes(x = Starttime_ms   Duration/2, width = Duration, height = 0.5, fill = Actor), color = "white")  
  # print text of `Activity`s:
  geom_text(aes(x = Starttime_ms, label = Activity), hjust = 0, size = 3.5)  
  # define labels for x- and y-axis and plot title:
  labs(
    # x- axis label:
    x = "Activity duration",
    # y-axis label:
    y = "Actors and activity type",
    # plot title:
    title = paste0("Activities during robot's utterance\nFile: ", 
                   target_seq$File, 
                   " [", 
                   min(target_seq$Line, na.rm = TRUE),
                   " - ",
                   max(target_seq$Line, na.rm = TRUE),
                   "]"))  
  theme_light()

geom_tile_factor

  • Related