I am trying to select or resize the plot margins for this figure that I have created after I have filtered the labels, I am left with this white part that I would like to get rid of as shown on the image.
Thank you
Below my reproducible code:
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)
CodePudding user response:
You can get this effect simply by omitting the elements of the plot you want to drop in the ggplot()
call itself by specifying element_blank()
within the theme()
for each.
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()
)