我有這種型別的資料:
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))
我想將所有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),
"]"))
這使:

我想為瓷磚選擇我自己的顏色。更復雜一點的是,這target_seq只是從到變化的數量和身份中的一種Actors。如何自動為s 手動選擇顏色?FileFilegeom_tile
uj5u.com熱心網友回復:
您可以創建一個向量來存盤“演員”的名稱及其相應的顏色。然后使用中的向量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])

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427632.html
上一篇:ggplot2-為具有不同資料范圍的兩個圖創建一個顏色條圖例
下一篇:修復ggplot2中的軸標簽長度
