繁体   English   中英

geom_tile 热图中的瓦片长度不正确

[英]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.

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