1
votes

Is there a way to flip the facet label using small_multiple from the dotwhisker package to have the facet labels on the left-handside and coefficient estimate on the right-hand side? I've seen the solutions to this when using ggplot and facet_wrap or facet_grid, but those aren't working with small_multiple since it does the faceting without those arguments. For simplicity, I'll be using the Dot-and-Whisker Plot Vignette that uses small_multiple as the example.

code used to generate plot:

# Generate a tidy data frame of regression results from six models
m <- list()
ordered_vars <- c("wt", "cyl", "disp", "hp", "gear", "am")
m[[1]] <- lm(mpg ~ wt, data = mtcars) 
m123456_df <- m[[1]] %>% 
  tidy() %>%
  by_2sd(mtcars) %>%
  mutate(model = "Model 1")
for (i in 2:6) {
  m[[i]] <- update(m[[i-1]], paste(". ~ . +", ordered_vars[i]))
  m123456_df <- rbind(m123456_df, m[[i]] %>%
                        tidy() %>%
                        by_2sd(mtcars) %>%
                        mutate(model = paste("Model", i)))
}

# Relabel predictors (they will appear as facet labels)
m123456_df <- m123456_df %>% 
  relabel_predictors(c("(Intercept)" = "Intercept",
                       wt = "Weight",
                       cyl = "Cylinders",
                       disp = "Displacement",
                       hp = "Horsepower",
                       gear = "Gears",
                       am = "Manual"))

# Generate a 'small multiple' plot
small_multiple(m123456_df) +
  theme_bw() + ylab("Coefficient Estimate") +
  geom_hline(yintercept = 0, colour = "grey60", linetype = 2) +
  ggtitle("Predicting Mileage") +
  theme(plot.title = element_text(face = "bold"), 
        legend.position = "none",
        axis.text.x = element_text(angle = 60, hjust = 1)) 

enter image description here

Ideally, I would have the flipped version of this plot with labels on the left and estimates on the right.

1

1 Answers

0
votes

This could be achieved by overwriting facet_grid and the scale_y as you would do in a normal ggplot like so:

  1. facet_grid(term~., switch = "y", scales="free_y")
  2. scale_y_continuous(position = "right")
library(dotwhisker)
library(dplyr)
library(broom)

# Generate a 'small multiple' plot
small_multiple(m123456_df) +
  theme_bw() + ylab("Coefficient Estimate") +
  geom_hline(yintercept = 0, colour = "grey60", linetype = 2) +
  facet_grid(term~., switch = "y", scales="free_y") +
  scale_y_continuous(position = "right") +
  ggtitle("Predicting Mileage") +
  theme(plot.title = element_text(face = "bold"), 
        legend.position = "none",
        axis.text.x = element_text(angle = 60, hjust = 1)) 

enter image description here