0

I am using dwplot() to plot estimates corresponding to years over the 1997-2011 period in an event-study type graph. However, I would like to add a line that connects the dots (as is default when plotting coefficients in other statistical packages).

I have attempted using geom_plot or an overlayed ggplot() object without much success.

My simple code is below:

# Plot using 'dwplot' (have to set variable order manually, stored in `var_order')
dwplot(est1, order_vars = rev(var_order), vline = geom_vline(xintercept = 0, colour = "grey60", linetype = 1))+
theme_bw() + xlab("") + ylab("Year") + coord_flip()  

which generates a horizontal plot of estimates where the variable names are the years 1997-2011.

  • 2
    `+ geom_line()` maybe? But honestly, it is hard to answer without a minimal reproducible example. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – markus Mar 31 '20 at 21:50
  • Where does dwplot come from? – camille Mar 31 '20 at 23:34
  • dwplot comes from the "dotwhisker" library, and apologies for the lack of detail, this was my first attempt to solicit help on stack. I was able to solve it in a somewhat inelegant way using geom_path() added as a layer (answer provided below). – kerob231 Apr 01 '20 at 22:06

1 Answers1

0

I was able to get the desired effect by adding a geom_path object as a layer after saving the dwplot object. I reproduce a cleaner version of the code from above plus the new command:

install.packages("dotwhisker")
library(dotwhisker)

gg <- dwplot(est1, order_vars = rev(est1$term), 
      dot_args = list(size = 2, color = "steelblue3"), 
       whisker_args = list(color = "steelblue3"),
       vline = geom_vline(xintercept = 0, color = "grey60", linetype = 1)) + coord_flip() 

gg$layers <- c(geom_path(data= est1, aes(x= estimate, y= obs), color = "steelblue3"),gg$layers)

where `est1' is model output, est1$term are the variable names of the coefficients (in my case, years), and est1$estimate are the coefficient point estimates:

> est1

   term   estimate  std.error    t.value      p.value obs
1  1997 0.01123708 0.01656981  0.6781657 4.976666e-01   1
2  1998 0.01316397 0.01571980  0.8374133 4.023603e-01   2
3  1999 0.01757487 0.02884073  0.6093766 5.422748e-01   3
4  2000 0.04682737 0.02670662  1.7533995 7.953346e-02   4

This solution is somewhat inelegant in that it required me to create an index of the terms (est1$obs) that correspond to the ordering of the coefficients in the dwplot call. Moreover, because I use coord_flip() to make the dwplot object horizontal, I pass est1$estimate to the x coordinate (even though they're displayed on the vertical axis) and est1$obs to the y coordinate.