[英]R how to overlay in geom_tile?
我在 ggplot2 中使用 geom_tile 創建了一個相對簡單的熱圖。 它只是一個小的數據矩陣,如彩色框。
在 y 軸上,我代表人(使用從 1 到 1000 的標識號),在 x 軸上代表時間(以小時為單位)。
第一個 df 代表人們的入住模式或他們在家時/出門在外時的情況。 例如,它顯示 id1 在家或不在家的時間。
第二個 df2 代表與設備使用行為有關的相同人的行為。例如,它顯示了 id1 使用設備時和不使用任何設備時的時間。
現在我試圖用相同的圖來表示 id 的占用率和設備使用模式。 例如,我想表示 id 何時在家並在一個圖中使用設備。
這樣的事情能做到嗎?
來自 df 的數據:
structure(list(id = 1:6, variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = c("04:00", "04:10", "04:20", "04:30", "04:40",
"04:50", "05:00", "05:10", "05:20", "05:30", "05:40", "05:50",
"06:00", "06:10", "06:20", "06:30", "06:40", "06:50", "07:00",
"07:10", "07:20", "07:30", "07:40", "07:50", "08:00", "08:10",
"08:20", "08:30", "08:40", "08:50", "09:00", "09:10", "09:20",
"09:30", "09:40", "09:50", "10:00", "10:10", "10:20", "10:30",
"10:40", "10:50", "11:00", "11:10", "11:20", "11:30", "11:40",
"11:50", "12:00", "12:10", "12:20", "12:30", "12:40", "12:50",
"13:00", "13:10", "13:20", "13:30", "13:40", "13:50", "14:00",
"14:10", "14:20", "14:30", "14:40", "14:50", "15:00", "15:10",
"15:20", "15:30", "15:40", "15:50", "16:00", "16:10", "16:20",
"16:30", "16:40", "16:50", "17:00", "17:10", "17:20", "17:30",
"17:40", "17:50", "18:00", "18:10", "18:20", "18:30", "18:40",
"18:50", "19:00", "19:10", "19:20", "19:30", "19:40", "19:50",
"20:00", "20:10", "20:20", "20:30", "20:40", "20:50", "21:00",
"21:10", "21:20", "21:30", "21:40", "21:50", "22:00", "22:10",
"22:20", "22:30", "22:40", "22:50", "23:00", "23:10", "23:20",
"23:30", "23:40", "23:50", "00:00", "00:10", "00:20", "00:30",
"00:40", "00:50", "01:00", "01:10", "01:20", "01:30", "01:40",
"01:50", "02:00", "02:10", "02:20", "02:30", "02:40", "02:50",
"03:00", "03:10", "03:20", "03:30", "03:40", "03:50"), class = "factor"),
value = c("Did not used device", "Did not used device", "Did not used device",
"Did not used device", "Did not used device", "Did not used device"
)), row.names = c(NA, 6L), class = "data.frame")
從這個數據我創建了上面的圖
來自 df2 的數據:
structure(list(id = 1:6, variable = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = c("04:00", "04:10", "04:20", "04:30", "04:40",
"04:50", "05:00", "05:10", "05:20", "05:30", "05:40", "05:50",
"06:00", "06:10", "06:20", "06:30", "06:40", "06:50", "07:00",
"07:10", "07:20", "07:30", "07:40", "07:50", "08:00", "08:10",
"08:20", "08:30", "08:40", "08:50", "09:00", "09:10", "09:20",
"09:30", "09:40", "09:50", "10:00", "10:10", "10:20", "10:30",
"10:40", "10:50", "11:00", "11:10", "11:20", "11:30", "11:40",
"11:50", "12:00", "12:10", "12:20", "12:30", "12:40", "12:50",
"13:00", "13:10", "13:20", "13:30", "13:40", "13:50", "14:00",
"14:10", "14:20", "14:30", "14:40", "14:50", "15:00", "15:10",
"15:20", "15:30", "15:40", "15:50", "16:00", "16:10", "16:20",
"16:30", "16:40", "16:50", "17:00", "17:10", "17:20", "17:30",
"17:40", "17:50", "18:00", "18:10", "18:20", "18:30", "18:40",
"18:50", "19:00", "19:10", "19:20", "19:30", "19:40", "19:50",
"20:00", "20:10", "20:20", "20:30", "20:40", "20:50", "21:00",
"21:10", "21:20", "21:30", "21:40", "21:50", "22:00", "22:10",
"22:20", "22:30", "22:40", "22:50", "23:00", "23:10", "23:20",
"23:30", "23:40", "23:50", "00:00", "00:10", "00:20", "00:30",
"00:40", "00:50", "01:00", "01:10", "01:20", "01:30", "01:40",
"01:50", "02:00", "02:10", "02:20", "02:30", "02:40", "02:50",
"03:00", "03:10", "03:20", "03:30", "03:40", "03:50"), class = "factor"),
value = c("At home", "At home", "At home", "Not Athome",
"At home", "At home")), row.names = c(NA, 6L), class = "data.frame")
我想覆蓋 2 個圖以顯示 Id 1 在家並使用用於覆蓋圖的 device.Code:
ggplot( df2_melted_modified , aes(x = variable, y = id, fill= value)) +
scale_x_discrete( name= "Time (hours)") +
scale_y_continuous(expand = c(-0, 0)) +
geom_tile() +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.spacing = unit(1, "mm"), axis.text.x=element_text(angle=90, hjust=1)) +
labs(x="Time", y ="Respondents indentification number", fill="Behaviour")
這是我創建的情節,我不明白為什么沒有突出顯示設備的使用? 歡迎我幫忙!
我想要一個這樣的情節:
我發現你的示例數據很難概括你的問題,所以我希望你不介意我創建了自己的(見帖子結尾)
如果您在一個 data.frame 中擁有共享 x/y 值的數據和在家中/使用設備變量作為兩列的數據,這可能是最簡單的。
最簡單的方法是在這些列之間使用interaction
:
ggplot(df, aes(Var2, Var1)) +
geom_tile(aes(fill = interaction(athome, usedev)))
稍微手動一點的方法是為您在家制作兩層/使用設備變量並將一層設置為(半)透明:
ggplot(df, aes(Var2, Var1)) +
geom_tile(aes(fill = athome)) +
geom_tile(aes(fill = usedev)) +
scale_fill_manual(values = c("At home" = "red",
"Not at home" = "white",
"Not using device" = "transparent",
"Using device" = "#0000FF88"))
偽造數據:
m <- matrix(0, nrow = 10, ncol = 24)
athome <- t(apply(m, 1, function(x) {
i <- rpois(1, 10)
x[i:pmin(rpois(1, 10), 24)] <- 1
x
}))
usedev <- t(apply(m, 1, function(x) {
i <- rpois(1, 11)
x[i:pmin(rpois(1, 11), 24)] <- 1
x
}))
df <- cbind(
reshape2::melt(athome, value.name = "athome"),
usedev = reshape2::melt(usedev)[,-c(1:2)]
)
df$athome <- factor(df$athome)
levels(df$athome) <- c("Not at home", "At home")
df$usedev <- factor(df$usedev)
levels(df$usedev) <- c("Not using device", "Using device")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.