我想知道是否有人知道組合表格和 ggplot 圖例的方法,以便圖例顯示為表中的一列,如圖所示。抱歉,如果之前有人問過這個問題,但我一直找不到辦法做到這一點。

編輯:附加的是生成下面輸出的代碼(減去我試圖生成的圖例/表格組合,因為我在 Powerpoint 中將它們縫合在一起)
library(ggplot2)
library(gridExtra)
library(dplyr)
library(formattable)
library(signal)
#dataset for ggplot
full.data <- structure(list(error = c(0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,
5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,
5, 6, 0, 1, 2, 3, 4, 5, 6), prob.ed.n = c(0, 0, 0.2, 0.5, 0.8,
1, 1, 0, 0, 0.3, 0.7, 1, 1, 1, 0, 0.1, 0.4, 0.9, 1, 1, 1, 0,
0.1, 0.5, 0.9, 1, 1, 1, 0, 0.1, 0.6, 1, 1, 1, 1, 0, 0.1, 0.6,
1, 1, 1, 1), N = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6)), row.names = c(NA, -42L), class = "data.frame")
#summary table
summary.table <- structure(list(prob.fr = c("1.62%", "1.35%", "1.09%", "0.81%", "0.54%", "0.27%"), prob.ed.n = c("87.4%", "82.2%", "74.8%", "64.4%", "49.8%", "29.2%"), N = c(6, 5, 4, 3, 2, 1)), row.names = c(NA,
-6L), class = "data.frame")
#table object to beincluded with ggplot
table <- tableGrob(summary.table %>%
rename(
`Prb FR` = prob.fr,
`Prb ED` = prob.ed.n,
),
rows = NULL)
#plot
plot <- ggplot(full.data, aes(x = error, y = prob.ed.n, group = N, colour = as.factor(N)))
geom_vline(xintercept = 2.45, colour = "red", linetype = "dashed")
geom_hline(yintercept = 0.9, linetype = "dashed")
geom_line(data = full.data %>%
group_by(N) %>%
do({
tibble(error = seq(min(.$error), max(.$error),length.out=100),
prob.ed.n = pchip(.$error, .$prob.ed.n, error))
}),
size = 1)
scale_x_continuous(labels = full.data$error, breaks = full.data$error, expand = c(0, 0.05))
scale_y_continuous(expand = expansion(add = c(0.01, 0.01)))
scale_color_brewer(palette = "Dark2")
guides(color = guide_legend(reverse=TRUE, nrow = 1))
theme_bw()
theme(legend.key = element_rect(fill = "white", colour = "black"),
legend.direction= "horizontal",
legend.position=c(0.8,0.05)
)
#arrange plot and grid side-by-side
grid.arrange(plot, table, nrow = 1, widths = c(4,1))
uj5u.com熱心網友回復:
這是一個有趣的問題。簡短的回答:是的,這是可能的。但它目前非常丑陋。盡管我可能永遠不會真正需要這種可視化,但我認為一旦可能,我會為此付出代價。
根據 r2evans 的評論,圖例與表格的對齊可能是最困難的部分。這里是丑陋的 hack,使用 ggpubr 作為表格,cowplot 用于拼接。
另一個問題來自垂直圖例的圖例鍵間距。據我所知,對于多邊形以外的其他鍵,這仍然是一個相當未解決的問題。相關的 GitHub 問題已關閉,但可能會有新問題。簡單看一下 中使用的 draw_key 函式ggplot2:::GeomLine,我不清楚在哪里更改不同鍵之間的距離。我想我很快會將此作為一個新問題發布,但今天為時已晚。
代碼中的其他一些相關注釋。
library(tidyverse)
library(ggpubr)
library(cowplot)
#>
#> Attaching package: 'cowplot'
#> The following object is masked from 'package:ggpubr':
#>
#> get_legend
# dataset for ggplot
full.data <- structure(list(error = c(
0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,
5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4,
5, 6, 0, 1, 2, 3, 4, 5, 6
), prob.ed.n = c(
0, 0, 0.2, 0.5, 0.8,
1, 1, 0, 0, 0.3, 0.7, 1, 1, 1, 0, 0.1, 0.4, 0.9, 1, 1, 1, 0,
0.1, 0.5, 0.9, 1, 1, 1, 0, 0.1, 0.6, 1, 1, 1, 1, 0, 0.1, 0.6,
1, 1, 1, 1
), N = c(
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6
)), row.names = c(NA, -42L), class = "data.frame")
summary.table <-
structure(list(
prob.fr = c("1.62%", "1.35%", "1.09%", "0.81%", "0.54%", "0.27%"),
prob.ed.n = c("87.4%", "82.2%", "74.8%", "64.4%", "49.8%", "29.2%"),
N = c(6, 5, 4, 3, 2, 1)
), row.names = c(NA, -6L), class = "data.frame")
## Hack number zero - create some space for the new legend
## this is not great, as not automated
spacer <- paste(rep(" ", 7), collapse = "")
my_table <-
summary.table %>%
mutate(N = paste(spacer, N))
p1 <-
ggplot(full.data, aes(x = error, y = prob.ed.n, group = N, colour = as.factor(N)))
geom_vline(xintercept = 2.45, colour = "red", linetype = "dashed")
geom_hline(yintercept = 0.9, linetype = "dashed")
geom_line(
data = full.data %>%
group_by(N) %>%
do({
tibble(
error = seq(min(.$error), max(.$error), length.out = 100),
prob.ed.n = signal::pchip(.$error, .$prob.ed.n, error)
)
}),
size = 1
)
## hack 1 - remove the legend labels. You have them in the table already.
scale_color_brewer(NULL, palette = "Dark2", labels = rep("", 6))
## remove all the legend specs! I've also removed the not so important reverse scale
## I have to remove fill and color, otherwise the hack becomes too evident
theme(
legend.key = element_rect(fill = NA, colour = NA),
legend.key.height = unit(.27, "in"),
legend.background = element_blank()
)
## create the plot elements
p_leg <- cowplot::get_legend(p1)
p2 <- ggtexttable(my_table, rows = NULL)
## we don't want the legend twice
p <- p1 theme(legend.position = "none")
## the positioning is the horrible bit and totally lacks automation.
ggdraw(p, xlim = c(0, 1.7))
draw_plot(p2, x = .8)
draw_plot(p_leg, x = .97, y = 0.98, vjust = 1)

由reprex 包(v2.0.1)于 2021 年 12 月 29 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398476.html
