[英]Tile length in geom_tile heatmap incorrect
我正在嘗試使用 ggplots geom_tile
使用 R 中的熱圖來可視化工作日和一天中的幾小時的聚合值。 使用我的測試數據,該方法工作得很好,但是,當我嘗試另一個測試數據集的摘錄時,圖塊的長度突然不正確。
工作測試:
# constructing testframe
set.seed(123)
testframe <- cbind.data.frame(
day = factor(sample(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"),100, replace = TRUE), levels = rev(c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))),
hour = sample(c(0:23),100, replace = TRUE),
year = sample(c(2018,2019,2020),100, replace = TRUE),
value = sample(seq(-312,324,1),100, replace = TRUE)
)
# trying to set scale limits somewhat intelligently
UpperLim <- max(abs(c(max(testframe$value),min(testframe$value))))
LowerLim <- -UpperLim
# plotting
ggplot(testframe, aes(hour, day)) +
geom_tile(aes(fill = value), colour = "black") +
labs(title = "Value by Weekday and Hour",
x = "",
y = "") +
scale_fill_distiller(palette = "RdYlGn", direction = 1, limits = c(LowerLim, UpperLim)) +
scale_y_discrete(drop = FALSE) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.ticks.x = element_blank(),
legend.position = "bottom",
legend.key.width = unit(2, "cm"),
panel.grid = element_blank()) +
coord_equal() +
scale_x_continuous(breaks = seq(-0.5,23.5,1),
limits = c(-0.5,23.5),
labels = c("00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00",
"24:00"))
這正是我想要的 plot。 但是,當我使用另一個測試數據集的摘錄嘗試相同的代碼時,它不會以這種方式工作:
其他測試數據集:
helperframe <- structure(list(day = structure(c(7L, 7L, 6L), .Label = c("Sunday",
"Saturday", "Friday", "Thursday", "Wednesday", "Tuesday", "Monday"
), class = "factor"), hour = c(12L, 23L, 0L), year = c(2018,
2018, 2018), affect = c(0, 286.11, 44.44), PosAffect = c(0, 286.11,
44.44), NegAffect = c(0, 0, 0)), row.names = c(NA, -3L), groups = structure(list(
day = structure(c(6L, 7L, 7L), .Label = c("Sunday", "Saturday",
"Friday", "Thursday", "Wednesday", "Tuesday", "Monday"), class = "factor"),
hour = c(0L, 12L, 23L), .rows = structure(list(3L, 1L, 2L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
繪圖輔助框架
# trying to set scale limits somewhat intelligently
UpperLim <- max(abs(c(max(helperframe$affect),min(helperframe$affect))))
LowerLim <- -UpperLim
out <- ggplot(helperframe, aes(hour, day)) +
geom_tile(aes(fill = affect), colour = "black") +
labs(title = "Reported Affect by Weekday and Hour",
subtitle = paste(starttime, " - ", endtime),
x = "",
y = "") +
scale_fill_distiller(palette = "RdYlGn", direction = 1, limits = c(LowerLim, UpperLim)) +
scale_y_discrete(drop = FALSE) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.ticks.x = element_blank(),
legend.position = "bottom",
legend.key.width = unit(2, "cm"),
panel.grid = element_blank()) +
coord_equal() +
scale_x_contiunous(breaks = seq(-0.5,23.5,1),
limits = c(-0.5,23.5),
labels = c("00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00",
"24:00"))
這給了我一個不正確的 plot ,其中瓷磚長度不正確並且瓷磚的 position 與數據不匹配
當我為scale_x_discrete
切換scale_x_continuous
時,我確實得到了正確的圖塊,但現在 x 軸消失了......
有什么建議可以在不丟失 x 軸的情況下獲得正確的瓷磚長度和 position?
嘗試對您的代碼進行以下更改:
首先,格式化 x 軸變量:
library(ggplot2)
#Adjust hour
helperframe$hour <- factor(helperframe$hour,
levels = 0:24,
labels = c("00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00",
"24:00"),
ordered = T)
現在,plot:
#Code
outplot <- ggplot(helperframe, aes(hour, day)) +
geom_tile(aes(fill = affect), colour = "black") +
labs(title = "Reported Affect by Weekday and Hour",
subtitle = paste('starttime', " - ", 'endtime'),
x = "",
y = "") +
scale_fill_distiller(palette = "RdYlGn", direction = 1, limits = c(LowerLim, UpperLim)) +
scale_y_discrete(drop = FALSE) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.ticks.x = element_blank(),
legend.position = "bottom",
legend.key.width = unit(2, "cm"),
panel.grid = element_blank()) +
coord_equal()+
scale_x_discrete(limits = c("00:00",
"01:00",
"02:00",
"03:00",
"04:00",
"05:00",
"06:00",
"07:00",
"08:00",
"09:00",
"10:00",
"11:00",
"12:00",
"13:00",
"14:00",
"15:00",
"16:00",
"17:00",
"18:00",
"19:00",
"20:00",
"21:00",
"22:00",
"23:00",
"24:00"))
Output:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.