我正在嘗試選擇或調整我在過濾標簽后創建的該圖形的繪圖邊距,我留下了我想擺脫的白色部分,如圖所示。
謝謝
在我的可重現代碼下方:
df=structure(list(Estimate = c(0.1784, 0.073, 0.0619, 0.1367, 0.1795,
0.087), name = structure(c(1L, 6L, 5L, 4L, 3L, 2L), .Label = c("Intercept",
"Doctor spouse", "8 years experience", "3 years experience",
"1 year experience", "Female"), class = "factor"), group = structure(c(1L,
2L, 3L, 3L, 3L, 4L), .Label = c("Intercept", "Male to", "0 Years Experience to",
"No Spouse to"), class = "factor"), upper.95 = c(0.209, 0.0899,
0.0858, 0.1606, 0.2034, 0.1077), lower.95 = c(0.1478, 0.0561,
0.038, 0.1129, 0.1556, 0.0662), resp_type = c("Legislator", "Legislator",
"Legislator", "Legislator", "Legislator", "Legislator")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
library(ggplot2)
library(tidyverse)
library(gridExtra)
library(gtable)
p1=df %>%
filter(name!="Intercept") %>%
ggplot(aes(x = name, y= Estimate*100))
geom_point(shape = 1)
geom_hline(yintercept = 0, lty = "longdash")
geom_errorbar(aes(ymax = upper.95*100, ymin = lower.95*100), width = 0.001)
coord_flip()
facet_grid(group ~.,scales = "free", space = "free", switch = "y")
theme(strip.placement = "outside",
strip.text.y.left = element_text(face = "bold", angle=0, vjust = 1))
xlab("")
gg=ggplotGrob(p1)
plot_filtered2 <- gtable_filter(gg,
"(background|panel|strip_l|axis-b|ylab-l|guide-box|title)", trim=TRUE)
grid.newpage()
grid.draw(plot_filtered2)
uj5u.com熱心網友回復:
從您所在的位置執行此操作的最簡單方法(假設您希望保留gtable從element_blank您的前 6 列gtable到 0 厘米:
plot_filtered2$widths[1:6] <- unit(rep(0, 6), "cm")
grid.newpage()
grid.draw(plot_filtered2)

uj5u.com熱心網友回復:
通過在for each 中指定,您可以通過省略要在ggplot()呼叫本身中洗掉的繪圖元素來獲得這種效果。element_blank()theme()
library(tidyverse)
df <- structure(list(Estimate = c(0.1784, 0.073, 0.0619, 0.1367, 0.1795, 0.087), name = structure(c(1L, 6L, 5L, 4L, 3L, 2L), .Label = c("Intercept", "Doctor spouse", "8 years experience", "3 years experience", "1 year experience", "Female"), class = "factor"), group = structure(c(1L, 2L, 3L, 3L, 3L, 4L), .Label = c("Intercept", "Male to", "0 Years Experience to", "No Spouse to"), class = "factor"), upper.95 = c(0.209, 0.0899, 0.0858, 0.1606, 0.2034, 0.1077), lower.95 = c(0.1478, 0.0561, 0.038, 0.1129, 0.1556, 0.0662), resp_type = c("Legislator", "Legislator", "Legislator", "Legislator", "Legislator", "Legislator")), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
df %>%
filter(name != "Intercept") %>%
ggplot(aes(x = name, y = Estimate * 100))
geom_point(shape = 1)
geom_hline(yintercept = 0, lty = "longdash")
geom_errorbar(aes(ymax = upper.95 * 100, ymin = lower.95 * 100), width = 0.001)
coord_flip()
facet_grid(group ~ .,
scales = "free",
space = "free",
switch = "y")
xlab(NULL)
theme(strip.text = element_blank(),
strip.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()
)

由reprex 包于 2022-02-15 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427647.html
上一篇:如何在抖動圖中僅繪制一個文本?
