【问题标题】:Plot a table of separate data below a ggplot2 graph that lines up on the X axis在 X 轴上排列的 ggplot2 图形下方绘制一个单独数据表
【发布时间】:2020-09-14 17:52:19
【问题描述】:

我正在寻找一个包含一个简单的多线 ggplot2 图表的图,该图表在图表下方按图表的 X 轴排列的单独(但相关)数据表。数据表的列名与图表的 x 轴(小时 1 到 24)匹配,但有一列专用于必要的行名。

下面分别是图表和数据表:

为简洁起见,数据表在第 16 小时被截断,但确实延长至 24 小时。

我整个上午都在尝试使用 gridExtra 中的不同解决方案来调整不同的参数,例如 nrow、ncol、高度和宽度,但最简单的解决方案只是产生某种合理结果的解决方案。下面的代码和图片是我取得的最好成绩:

library(gridExtra)

p1 <- ggplot(load_forecast_plot, aes(group=Load_Type, y=Load_Values, x=Hour,  colour = Load_Type)) + 
  geom_line(size = 1) +
  scale_x_continuous(breaks = c(1:24))

p1 <- ggplotGrob(p1)

p2<-tableGrob(df)

grid.arrange(p1, p2, top = paste("Load and Weather Error Power Grid", Sys.Date()-1, sep = " "))

grid.draw(tableGrob(MISO_wx_PrevDay_error_test,theme=ttheme_minimal(base_size = 5)))

产生:

我希望图表更大,而表格更小,并尽可能沿 x 轴对齐。我查看了将表转换为 ggplot2 对象的示例,但这些示例的数据与我的绘图和表中的数据相同。

以下是我的可重现示例的数据。任何帮助深表感谢!谢谢你。

ggplot 图的数据:

dput(load_forecast_plot)
structure(list(Hour = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 
5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 
11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 
17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 
22, 22, 23, 23, 23, 24, 24, 24), Load_Type = c("Load", "DA_MTLF", 
"BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", 
"Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", 
"DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", 
"BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", 
"Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", 
"DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", 
"BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", 
"Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", 
"DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", 
"BC_MTLF", "Load", "DA_MTLF", "BC_MTLF", "Load", "DA_MTLF", "BC_MTLF"
), Load_Values = c(59141, 59260, 57862, 56493, 56470, 54964, 
54480, 54553, 52996, 53270, 53252, 51683, 53050, 52520, 50845, 
53020, 51723, 49627, 53844, 51907, 49293, 56956, 55069, 52700, 
60975, 60036, 58251, 65595, 65023, 63881, 69796, 69023, 68776, 
73392, 72517, 72591, 76412, 74896, 75452, 78454, 76538, 77547, 
79959, 77782, 79256, 81315, 78851, 80627, 82478, 79921, 81763, 
82638, 80027, 81896, 81244, 78906, 80328, 78484, 76627, 77304, 
77187, 75130, 75391, 74495, 72612, 72776, 69736, 68216, 68488, 
64844, 63756, 64145)), row.names = c(NA, -72L), class = c("tbl_df", 
"tbl", "data.frame"))

数据表:

dput(df)
structure(list(WX_Error = c("CloudCover", "DewPoint", "RainFall", 
"SolarRadiation", "Temperature", "WindSpeed"), `1` = c("-13.72%", 
"-0.41°F", "0in", "0min", "-0.86°F", "0.26mph"), `2` = c("-8.52%", 
"-0.05°F", "-0.01in", "0min", "-1.2°F", "-0.11mph"), `3` = c("-9.22%", 
"-0.41°F", "-0.01in", "0min", "-1.26°F", "-1.41mph"), `4` = c("-14.57%", 
"-0.98°F", "-0.01in", "0min", "-1.48°F", "-0.99mph"), `5` = c("-15.81%", 
"-0.83°F", "-0.01in", "0min", "-0.83°F", "-1.58mph"), `6` = c("-13.43%", 
"-0.61°F", "0in", "-0.43min", "-0.46°F", "0.48mph"), `7` = c("-14.23%", 
"-0.28°F", "0in", "7.91min", "-1.15°F", "-0.43mph"), `8` = c("-2.29%", 
"0.1°F", "0in", "1.3min", "-0.72°F", "0.51mph"), `9` = c("-3.63%", 
"0.2°F", "0in", "1.96min", "-0.94°F", "-0.9mph"), `10` = c("4.73%", 
"0.25°F", "0in", "-2.99min", "-0.69°F", "0.25mph"), `11` = c("-8.68%", 
"0.8°F", "0.01in", "5.03min", "-0.83°F", "0.81mph"), `12` = c("-4.42%", 
"0.64°F", "0.01in", "2.34min", "-0.3°F", "0.9mph"), `13` = c("-15.06%", 
"0.49°F", "-0.01in", "8.08min", "0.29°F", "0.44mph"), `14` = c("-25.35%", 
"0.55°F", "-0.01in", "14.4min", "0.47°F", "0.59mph"), `15` = c("-19.36%", 
"0.6°F", "-0.01in", "10.76min", "0.44°F", "1.29mph"), `16` = c("-8.1%", 
"0.17°F", "-0.01in", "5.03min", "0.29°F", "1.26mph"), `17` = c("-21.01%", 
"-0.27°F", "-0.01in", "11.74min", "1.52°F", "0.72mph"), `18` = c("-22.84%", 
"-0.74°F", "-0.01in", "12.77min", "2.17°F", "1.34mph"), `19` = c("-18.57%", 
"-0.55°F", "0in", "10.35min", "0.46°F", "1.13mph"), `20` = c("-10.39%", 
"-0.91°F", "0.03in", "5.6min", "0.65°F", "0.71mph"), `21` = c("-6.65%", 
"-0.28°F", "0.06in", "1.66min", "-0.5°F", "-0.56mph"), `22` = c("-0.2%", 
"-0.4°F", "-0.01in", "0min", "-0.33°F", "-1.35mph"), `23` = c("4.39%", 
"0.11°F", "-0.01in", "0min", "-0.5°F", "-0.47mph"), `24` = c("-5.65%", 
"0.64°F", "0.01in", "0min", "-0.43°F", "0.35mph")), row.names = c(NA, 
-6L), groups = structure(list(Date = structure(c(18407, 18407, 
18407, 18407, 18407, 18407), class = "Date"), wx_vars = c("CloudCover", 
"DewPoint", "RainFall", "SolarRadiation", "Temperature", "WindSpeed"
), .rows = list(1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, -6L
), class = c("tbl_df", "tbl", "data.frame"), .drop = FALSE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

【问题讨论】:

    标签: r ggplot2 gridextra


    【解决方案1】:

    您可以将表格格式化为 ggplot 对象,然后使用 patchwork 包为您处理对齐。

    library(ggplot2)
    library(patchwork)
    
    p1 <- ggplot(load_forecast_plot, aes(group=Load_Type, y=Load_Values, x=Hour,  colour = Load_Type)) + 
      geom_line(size = 1) +
      scale_x_continuous(breaks = c(1:24))
    
    p2 <- gridExtra::tableGrob(df)
    # Set widths/heights to 'fill whatever space I have'
    p2$widths <- unit(rep(1, ncol(p2)), "null")
    p2$heights <- unit(rep(1, nrow(p2)), "null")
    
    # Format table as plot
    p3 <- ggplot() +
      annotation_custom(p2)
    
    # Patchwork magic
    p1 + p3 + plot_layout(ncol = 1)
    

    我知道现在看起来不太好;您必须对设备大小和文本大小进行更多修改。但是,问题是关于对齐的,这似乎没问题。

    编辑:

    如果您正确设置 x 轴,您也可以将轴刻度与列匹配:

    p1 <- ggplot(load_forecast_plot, aes(group=Load_Type, y=Load_Values, x=Hour,  colour = Load_Type)) + 
      geom_line(size = 1) +
      scale_x_continuous(breaks = c(1:24),
                         limits = c(-1, 24),
                         expand = c(0,0.5))
    

    或者您可以将第二列设置为轴文本:

    p1 <- ggplot(load_forecast_plot, aes(group=Load_Type, y=Load_Values, x=Hour,  colour = Load_Type)) + 
      geom_line(size = 1) +
      scale_x_continuous(breaks = c(1:24),
                         expand = c(0,0.5))
    
    p2 <- gridExtra::tableGrob(df)[, -c(1:2)]
    p2$widths <- unit(rep(1, ncol(p2)), "null")
    p2$heights <- unit(rep(1, nrow(p2)), "null")
    
    p3 <- ggplot() +
      annotation_custom(p2) +
      scale_y_discrete(breaks = rev(df$WX_Error), 
                       limits = c(rev(df$WX_Error), ""))
    
    p1 + p3 + plot_layout(ncol = 1)
    

    EDIT2:

    我也没有看到任何文本大小选项,但您可以通过以下方式手动更改字体大小:

    is_text <- vapply(p2$grobs, inherits, logical(1), "text")
    p2$grobs[is_text] <- lapply(p2$grobs[is_text], function(text) {
      text$gp$fontsize <- 8
      text
    })
    

    【讨论】:

    • 我喜欢第一个编辑示例。我正在研究文本大小,因为这可能有效!第二个示例抛出错误:“zero_range(from) 中的错误:x 必须是长度 1 或 2”
    • 嗯,奇怪;我在 R 版本较低 (3.6.3) 的另一台机器上重新运行了代码,但没有收到错误消息。
    • 我找到了如何更改表格中的文字大小。在 tableGrob 函数中,调用:theme = ttheme_default(base_size = 6)。 base_size 可以调整我目前正在玩的。我将您的答案标记为正确,我认为此编辑将完成它。我非常感谢您的帮助@teunbrand。谢谢!
    【解决方案2】:

    经过一番折腾,我得到了这个:

    library(ggpubr)
    p1 <- ggplot(load_forecast_plot, aes(group=Load_Type, y=Load_Values, x=Hour,  colour = Load_Type)) + 
      geom_line(size = 1) +
      scale_x_continuous(breaks = c(1:24)) +
      theme(legend.position="top")+
      theme(plot.margin=unit(c(0,1,0,3.1),"cm"))
    p1
    
    
    p2 <- ggtexttable(df)
    
    plot <- ggarrange(p1, p2,
                      ncol = 1, nrow = 2,
                      heights = c(10,3))
    plot
    
    

    只需调整边距和设备大小,直到它或多或少对齐。不过是一种解决方法,我刚刚看到的@teunbrand 的解决方案现在看起来更有希望。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多