jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
163 stars 22 forks source link

I can't change the axis of graph with plot_summs function #79

Closed badgerinden closed 2 years ago

badgerinden commented 4 years ago

Hi,

I can't change the text and line of axis in my graph. I created a graph with the following code. But the graph doesn't have both x and y axis. And I can't edit text size and color of x and y axis. I think axis.line and axis.text function do not work. (Other functions, including plot.title, legend.title and panel.grid, do work well.)

SqrlGv0 <- plot_summs(sightv0Int, feedingv0Int, scale=TRUE, robust=TRUE, 
                      coefs=c("Control"="v0:ForestryControl", "Random"="v0:ForestryRandom logging", 
                      "Selective"="v0:ForestrySelective logging"), model.names=c("Sight", "Feeding"), 
                      legend.title = "Squirrel") + xlim(-2.5,2.5) + ggtitle('(a)') +
                      theme(legend.position = c(0.85, 0.85), legend.text=element_text(colour="black"), 
                      legend.title=element_text(colour="black"), plot.title= element_text(size=12, 
                      colour="black"), panel.grid=element_blank(),
                      axis.title = element_text(size=11, colour="black"), axis.text=element_text(size=10, 
                      colour="black"), axis.line=element_line())
jacob-long commented 2 years ago

So I'll use the example code here...

library(ggplot2)
library(jtools)
states <- as.data.frame(state.x77)
fit1 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))
fit2 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))
fit3 <- lm(Income ~ Frost + Illiteracy + Murder +
           Population + Area + `Life Exp` + `HS Grad`,
           data = states, weights = runif(50, 0.1, 3))

# Plot all 3 regressions with custom predictor labels,
# standardized coefficients, and robust standard errors
plot_summs(fit1, fit2, fit3,
           coefs = c("Frost Days" = "Frost", "% Illiterate" = "Illiteracy",
                     "Murder Rate" = "Murder"),
           scale = TRUE, robust = TRUE)

I can certainly edit the axis text like so:

p <- plot_summs(fit1, fit2, fit3,
           coefs = c("Frost Days" = "Frost", "% Illiterate" = "Illiteracy",
                     "Murder Rate" = "Murder"),
           scale = TRUE, robust = TRUE)

p + theme(axis.text.x = element_text(size = 50), axis.text.y = element_text(size = 50) # makes it really big

For the axis lines, that one was a bit more complicated but it appears to be originating in theme_nice() to some extent. It sets axis.line.x.bottom, axis.line.x.top, axis.line.y.left, and axis.line.y.right to element_blank(). For some reason these take precedence over a user-specified axis.line or axis.line.x/axis.line.y. I've removed these from theme_nice() so now using theme() after the fact should work as expected. As a general rule, if some argument to theme() isn't working it would be a good idea to try a specific version of it (e.g., instead of axis.line, axis.line.xoraxis.line.x.bottom) because of the wayggplot2seems to handle conflicting instructions from themes versus user calls totheme()`.