tableGrob 中不寻常的白色矩形
Unusual white rectangle in tableGrob
不久前,我问 关于将 ggplot 图例与 R 中的 table 相结合以使图例信息更加丰富。答案非常有帮助,直到最近我一直在使用公认的解决方案,没有任何问题。现在,我尝试将这种方法应用于稍微修改过的脚本,由于某种原因,我在图例顶部得到了一个意想不到的白色矩形。本质上,我从 table 制作了一个 tableGrob 并将其放置在 ggplot 图例旁边,以便它们看起来是相同的元素。为了清楚起见,我省略了一些其他格式。我在下面发布了一些示例图片:
对于我的一生,我无法弄清楚为什么那个矩形在那里或如何删除它。任何见解将不胜感激。
代码:
library(dplyr)
library(ggplot2)
library(gridExtra)
library(grid)
library(patchwork)
#Data
fullData <- 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), prob.ed = c(0.005, 0.041, 0.298, 0.753, 0.966, 0.998,
0.999, 0.005, 0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.005,
0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.007, 0.261, 0.875,
0.996, 1, 1, 1, 0.038, 0.568, 0.983, 1, 1, 1, 1), prob.fr = c(0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007,
0.038, 0.038, 0.038, 0.038, 0.038, 0.038, 0.038), N = c(2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), rules = c("13SD", "13SD",
"13SD", "13SD", "13SD", "13SD", "13SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar")), row.names = c(NA, -35L), class = "data.frame")
#Table for tableGrob
critTable <- structure(list(error = c(7, 7, 7, 7, 7), prob.ed = c(0.192, 0.456,
0.456, 0.77, 0.962), prob.fr = c(0.005, 0.005, 0.005, 0.007,
0.038), N = c(2, 2, 2, 2, 2), rules = c("13SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD-Xbar"
)), row.names = c(NA, -5L), class = "data.frame")
#ggplot
plot <- plot <- ggplot(fullData, aes(x = error, y = prob.ed, group = rules, colour = as.factor(rules))) +
geom_vline(xintercept = 1.75, colour = "red", linetype = "dashed") +
geom_hline(yintercept = 0.90, linetype = "dashed") +
geom_line(data = full.data %>%
group_by(rules) %>%
do({tibble(error = seq(min(.$error), max(.$error),length.out=100),
prob.ed = pchip(.$error, .$prob.ed, 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") +
theme_bw() +
theme(
legend.position=c(0.93,0.2075),
legend.title=element_blank(),
legend.background = element_blank(),
legend.key.size = unit(0.687, "cm"),
legend.text = element_text(size = 12))
#tableGrob
table <- tableGrob(critTable %>%
select(prob.fr,
prob.ed,) %>%
rename(`Prb FR` = prob.fr,
`Prb ED` = prob.ed) %>%
mutate(Rules = ""),
rows = NULL,
theme = ttheme_minimal())
#ggplot + tableGrob
plot <- plot + inset_element(table,
left = 0.65, bottom = 0.2, right = 0.9, top = .3, align_to = 'full')
我稍微修改了代码(添加:library(pracma)
)
在 ggplot
的某些位置将 full.data
更改为 fullData
。解决方案并不完全是所需的输出。不过干涉应该是个好的开始。
所以白色矩形是由tableGrob
中的ttheme_minimal
造成的。你可以适应 gridExtra::ttheme_default()
.
我更喜欢这里已经建议的 r - tableGrob background(但不知何故更新以任何方式得到认可)添加 theme_void()
并调整位置左、右、上和下等...
library(dplyr)
library(ggplot2)
library(gridExtra)
library(grid)
library(patchwork)
library(pracma)
#install.packages("pracma")
#Data
fullData <- 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), prob.ed = c(0.005, 0.041, 0.298, 0.753, 0.966, 0.998,
0.999, 0.005, 0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.005,
0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.007, 0.261, 0.875,
0.996, 1, 1, 1, 0.038, 0.568, 0.983, 1, 1, 1, 1), prob.fr = c(0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007,
0.038, 0.038, 0.038, 0.038, 0.038, 0.038, 0.038), N = c(2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), rules = c("13SD", "13SD",
"13SD", "13SD", "13SD", "13SD", "13SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar")), row.names = c(NA, -35L), class = "data.frame")
#Table for tableGrob
critTable <- structure(list(error = c(7, 7, 7, 7, 7), prob.ed = c(0.192, 0.456,
0.456, 0.77, 0.962), prob.fr = c(0.005, 0.005, 0.005, 0.007,
0.038), N = c(2, 2, 2, 2, 2), rules = c("13SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD-Xbar"
)), row.names = c(NA, -5L), class = "data.frame")
#ggplot
plot <- ggplot(fullData, aes(x = error, y = prob.ed, group = rules, colour = as.factor(rules))) +
geom_vline(xintercept = 1.75, colour = "red", linetype = "dashed") +
geom_hline(yintercept = 0.90, linetype = "dashed") +
geom_line(data = fullData %>%
group_by(rules) %>%
do({tibble(error = seq(min(.$error), max(.$error),length.out=100),
prob.ed = pchip(.$error, .$prob.ed, error))}),
size = 1) +
scale_x_continuous(labels = fullData$error, breaks = fullData$error, expand = c(0, 0.05)) +
scale_y_continuous(expand = expansion(add = c(0.01, 0.01))) +
scale_color_brewer(palette = "Dark2") +
theme_bw() +
theme(
legend.position=c(0.93,0.2075),
legend.title=element_blank(),
legend.background = element_blank(),
legend.key.size = unit(0.687, "cm"),
legend.text = element_text(size = 12))
#tableGrob
table <- tableGrob(critTable %>%
select(prob.fr,
prob.ed,) %>%
rename(`Prb FR` = prob.fr,
`Prb ED` = prob.ed) %>%
mutate(Rules = ""),
rows = NULL,
theme = ttheme_minimal())
#ggplot + tableGrob
plot + inset_element(table,
left = 0.8, bottom = 0.2,
right = 0.9, top = .24, align_to = 'panel')+
theme_void()
不久前,我问
对于我的一生,我无法弄清楚为什么那个矩形在那里或如何删除它。任何见解将不胜感激。
代码:
library(dplyr)
library(ggplot2)
library(gridExtra)
library(grid)
library(patchwork)
#Data
fullData <- 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), prob.ed = c(0.005, 0.041, 0.298, 0.753, 0.966, 0.998,
0.999, 0.005, 0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.005,
0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.007, 0.261, 0.875,
0.996, 1, 1, 1, 0.038, 0.568, 0.983, 1, 1, 1, 1), prob.fr = c(0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007,
0.038, 0.038, 0.038, 0.038, 0.038, 0.038, 0.038), N = c(2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), rules = c("13SD", "13SD",
"13SD", "13SD", "13SD", "13SD", "13SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar")), row.names = c(NA, -35L), class = "data.frame")
#Table for tableGrob
critTable <- structure(list(error = c(7, 7, 7, 7, 7), prob.ed = c(0.192, 0.456,
0.456, 0.77, 0.962), prob.fr = c(0.005, 0.005, 0.005, 0.007,
0.038), N = c(2, 2, 2, 2, 2), rules = c("13SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD-Xbar"
)), row.names = c(NA, -5L), class = "data.frame")
#ggplot
plot <- plot <- ggplot(fullData, aes(x = error, y = prob.ed, group = rules, colour = as.factor(rules))) +
geom_vline(xintercept = 1.75, colour = "red", linetype = "dashed") +
geom_hline(yintercept = 0.90, linetype = "dashed") +
geom_line(data = full.data %>%
group_by(rules) %>%
do({tibble(error = seq(min(.$error), max(.$error),length.out=100),
prob.ed = pchip(.$error, .$prob.ed, 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") +
theme_bw() +
theme(
legend.position=c(0.93,0.2075),
legend.title=element_blank(),
legend.background = element_blank(),
legend.key.size = unit(0.687, "cm"),
legend.text = element_text(size = 12))
#tableGrob
table <- tableGrob(critTable %>%
select(prob.fr,
prob.ed,) %>%
rename(`Prb FR` = prob.fr,
`Prb ED` = prob.ed) %>%
mutate(Rules = ""),
rows = NULL,
theme = ttheme_minimal())
#ggplot + tableGrob
plot <- plot + inset_element(table,
left = 0.65, bottom = 0.2, right = 0.9, top = .3, align_to = 'full')
我稍微修改了代码(添加:library(pracma)
)
在 ggplot
的某些位置将 full.data
更改为 fullData
。解决方案并不完全是所需的输出。不过干涉应该是个好的开始。
所以白色矩形是由tableGrob
中的ttheme_minimal
造成的。你可以适应 gridExtra::ttheme_default()
.
我更喜欢这里已经建议的 r - tableGrob background(但不知何故更新以任何方式得到认可)添加 theme_void()
并调整位置左、右、上和下等...
library(dplyr)
library(ggplot2)
library(gridExtra)
library(grid)
library(patchwork)
library(pracma)
#install.packages("pracma")
#Data
fullData <- 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), prob.ed = c(0.005, 0.041, 0.298, 0.753, 0.966, 0.998,
0.999, 0.005, 0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.005,
0.091, 0.598, 0.949, 0.997, 0.998, 0.999, 0.007, 0.261, 0.875,
0.996, 1, 1, 1, 0.038, 0.568, 0.983, 1, 1, 1, 1), prob.fr = c(0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005, 0.005,
0.005, 0.005, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007, 0.007,
0.038, 0.038, 0.038, 0.038, 0.038, 0.038, 0.038), N = c(2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), rules = c("13SD", "13SD",
"13SD", "13SD", "13SD", "13SD", "13SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar", "13SD-22SD-R4SD-41SD-Xbar",
"13SD-22SD-R4SD-41SD-Xbar")), row.names = c(NA, -35L), class = "data.frame")
#Table for tableGrob
critTable <- structure(list(error = c(7, 7, 7, 7, 7), prob.ed = c(0.192, 0.456,
0.456, 0.77, 0.962), prob.fr = c(0.005, 0.005, 0.005, 0.007,
0.038), N = c(2, 2, 2, 2, 2), rules = c("13SD", "13SD-22SD",
"13SD-22SD-R4SD", "13SD-22SD-R4SD-41SD", "13SD-22SD-R4SD-41SD-Xbar"
)), row.names = c(NA, -5L), class = "data.frame")
#ggplot
plot <- ggplot(fullData, aes(x = error, y = prob.ed, group = rules, colour = as.factor(rules))) +
geom_vline(xintercept = 1.75, colour = "red", linetype = "dashed") +
geom_hline(yintercept = 0.90, linetype = "dashed") +
geom_line(data = fullData %>%
group_by(rules) %>%
do({tibble(error = seq(min(.$error), max(.$error),length.out=100),
prob.ed = pchip(.$error, .$prob.ed, error))}),
size = 1) +
scale_x_continuous(labels = fullData$error, breaks = fullData$error, expand = c(0, 0.05)) +
scale_y_continuous(expand = expansion(add = c(0.01, 0.01))) +
scale_color_brewer(palette = "Dark2") +
theme_bw() +
theme(
legend.position=c(0.93,0.2075),
legend.title=element_blank(),
legend.background = element_blank(),
legend.key.size = unit(0.687, "cm"),
legend.text = element_text(size = 12))
#tableGrob
table <- tableGrob(critTable %>%
select(prob.fr,
prob.ed,) %>%
rename(`Prb FR` = prob.fr,
`Prb ED` = prob.ed) %>%
mutate(Rules = ""),
rows = NULL,
theme = ttheme_minimal())
#ggplot + tableGrob
plot + inset_element(table,
left = 0.8, bottom = 0.2,
right = 0.9, top = .24, align_to = 'panel')+
theme_void()