Search code examples
rggplot2dplyrgambroom

Selecting coefficents to plot as dot-and-whiskers


I would like to plot the results of a GAM as dot-and-whisker plots using the dwplot() command in the dotwhisker R package. The example in the package documentation is as follows:

#Package preload
library(dotwhisker)
library(broom)
library(dplyr)
# run a regression compatible with tidy
m1 <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)
m1_df <- tidy(m1) # create data.frame of regression results
m1_df # a tidy data.frame available for dwplot
dwplot(m1_df) #same as dwplot(m1)

I have two questions:

  1. I only want to plot two of the coefficients. How could I change the command to plot only, say, wt and cyl and exclude disp and gear.
  2. How can I make the whiskers go vertically instead of horizontally (like in a "small multiple" plot, small_multiple())

Thanks,

Josh


Solution

  • You can try something like this:

    dwplot(m1_df) + ylim(breaks=c("wt","cyl")) + coord_flip()
    

    enter image description here

    In the ylim() function, you can specify the breaks (factors) to include when the axis is discrete. And coord_flip() simply flips the plot around.