Я хочу создать график, который содержит простой многострочный график 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"))