繁体   English   中英

自动为 geom_tile 手动选择颜色

[英]Automate manual color selection for `geom_tile`s

我有这种类型的数据:

target_seq <- structure(list(Line = c("130", "131", "132", "133", "134", "135", 
                                      "136", "137", "138", "139", "140", "141", "142"), 
                             Actor = c("R", "R", "R", "R", "B", "R", "B", "B", "B", "M", "M", "M", "M"), 
                             Act_cat = c("ver", "SpeechRec", "ges", "ges", "gaze", "ges", 
                                         "gaze", "gaze", "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", "~", "@tum", "~", "@tum"), 
                             Starttime_ms = c(48825, 48865, 49220, 50080, 50730, 50900, 51009, 51191, 51270, 51486, 51809, 52251, 52333), 
                             Endtime_ms = c(53035, 49865, 50080, 50900, 51009, 52220, 51191, 51270, 53474, 51808, 52250, 52332, 53226), 
                             Duration = c(4210, 1000, 860, 820,279, 1320, 182, 79, 2204, 322, 441, 81, 893), 
                             File = c("VP_4_004", "VP_4_004", "VP_4_004", "VP_4_004", "VP_4_004", "VP_4_004", 
                                      "VP_4_004", "VP_4_004", "VP_4_004", "VP_4_004", "VP_4_004", 
                                      "VP_4_004", "VP_4_004"), 
                             Action = c("R_ver", "R_SpeechRec", "R_ges", "R_ges", "B_gaze", "R_ges", "B_gaze", "B_gaze", 
                                          "B_gaze", "M_gaze", "M_gaze", "M_gaze", "M_gaze")), 
                        class = c("tbl_df","tbl", "data.frame"), row.names = c(NA, -13L))

我想 plot 所有Activity值作为geom_tiles 我可以用这段代码做到这一点:

library(ggplot2)
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.3, 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 tile:
  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),
                   "]"))

这使: 在此处输入图像描述

我想为瓷砖选择我自己的 colors 让事情稍微复杂一点的是,这个target_seq只是众多中的一个Actors的数量和身份File变为File 如何自动为 geom_tile 手动选择geom_tile

您可以创建一个向量来存储“Actor”的名称及其相应的颜色。 然后在scale_fill_manual(values = vector)中使用向量。

这里我的矢量是tile_fill ,它用蓝色填充演员 B,用红色填充 R,用绿色填充 M(演员名称来自您的target_seq )。

library(ggplot2)

tile_fill <- c("A" = "orange", "B" = "blue", "C" = "purple" ,"R" = "red", "M" = "green")

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.3, 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 tile:
  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),
                   "]")) +
  scale_fill_manual(values = tile_fill[names(tile_fill) %in% target_seq$Actor])

geom_tile_fill

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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