Any suggestions on how to align text of the levels within a variable closer to the variable name? I am plotting model coefficients using the package GGally and broom.mixed which automatically incorporates faceting based on the covariates I'm interested in. I found strip.text.y.left(element.text = hjust = 1)
to get me the closest, but I'm trying to eliminate the award space between labels to make more room for the actual graph.
require(broom.helpers)
require(broom.mixed)
require(GGally)
# model4 is my glmer model
ggcoef_model(model4, conf.int = TRUE, include = c("Openness","Slope^2","Distance to trails and rec","Dom.Veg",
"Northness","Burn","Distance to roads"), intercept = FALSE,
add_reference_rows = FALSE, show_p_values = FALSE, signif_stars = FALSE, stripped_rows=FALSE,
point_size=3, errorbar_height = 0.2)
xlab("Coefficients")
theme(plot.title = element_text(hjust = 0.5, face="bold", size = 24), strip.text.y = element_text(size = 17),
strip.text.y.left = element_text(hjust = 1), strip.placement = "outside",
axis.title.x = element_text(size=17, vjust = 0.3), legend.position = "right",
axis.text=element_text(size=15.5, hjust = 1),
legend.text = element_text(size=14))
Ideally, I think labels with an inside placement would look best but that might cause issues for the Dom.Veg category as the main variable title should come before it's levels. Another good option would be an outside placement with left-aligned variable names (so remove the strip.text.y.left(element.text = hjust = 1)
line, and have the categories be much closer to the variable names. Is this even possible?
CodePudding user response:
I think direct labelling is probably the answer here:
p <- ggcoef_model(model4, conf.int = TRUE,
include = c("Openness","Slope^2","Distance to trails and rec","Dom.Veg",
"Northness","Burn","Distance to roads"),
intercept = FALSE,
add_reference_rows = FALSE,
show_p_values = FALSE,
signif_stars = FALSE,
stripped_rows=FALSE,
point_size=3, errorbar_height = 0.2)
xlab("Coefficients")
theme(plot.title = element_text(hjust = 0.5, face="bold", size = 24),
strip.text.y = element_text(size = 17),
strip.text.y.left = element_text(hjust = 1),
strip.placement = "outside",
axis.title.x = element_text(size=17, vjust = 0.3),
legend.position = "right",
axis.text=element_text(size=15.5, hjust = 1),
legend.text = element_text(size=14))
p theme(axis.text.y = element_blank())
geom_text(aes(label = label),
data = p$data[p$data$var_class == "factor",],
nudge_y = 0.4, color = "black")
Note, I didn't have your data set here, so had to create a similar one (this was much harder than actually answering the question!)
set.seed(1)
df <- data.frame(Openness = runif(1000),
`Slope^2` = runif(1000),
`Distance to trails and rec` = runif(1000),
`Northness` = runif(1000),
`Burn` = runif(1000),
`Distance to roads` = runif(1000),
`Dom.Veg` = factor(c(rep(c("NADA", "Aspen", "PJ", "Oak/Shrub",
"Ponderosa", "Mixed Con.",
"Wet meadow/pasture"), each = 142),
rep("Ponderosa", 6)),
levels = c("NADA", "Aspen", "PJ", "Oak/Shrub",
"Ponderosa", "Mixed Con.",
"Wet meadow/pasture"
)), check.names = FALSE)
df$outcome <- with(df, Openness * 0.2
`Slope^2` * -0.15
`Distance to trails and rec` * -0.9
`Northness` * -0.1
`Burn` * 0.5
`Distance to roads` * -0.6
rnorm(1000) c(0, 2.1, -1, -0.5, -1, -0.1, 1.2)[as.numeric(Dom.Veg)]
)
model4 <- glm(outcome ~ Openness `Slope^2` `Distance to trails and rec`
Northness Burn `Distance to roads` `Dom.Veg`, data = df)