Skip to content

Instantly share code, notes, and snippets.

@webbedfeet
Last active July 31, 2019 06:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webbedfeet/7031404fc3f500f6258e to your computer and use it in GitHub Desktop.
Save webbedfeet/7031404fc3f500f6258e to your computer and use it in GitHub Desktop.
Forest plots using R and ggplot2
credplot.gg <- function(d){
# d is a data frame with 4 columns
# d$x gives variable names
# d$y gives center point
# d$ylo gives lower limits
# d$yhi gives upper limits
require(ggplot2)
p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi))+
geom_pointrange()+
geom_hline(yintercept = 0, linetype=2)+
coord_flip()+
xlab('Variable')
return(p)
}
@webbedfeet
Copy link
Author

The code from today works with R 3.2.0 and ggplot2 2.0.0

@ayasseen
Copy link

ayasseen commented Apr 1, 2016

Hello Abhijit,

Thank you for posting this function, I found it very helpful. However, I have an additional request, if you would be so kind.

I would like to include a second center point and confidence limits under the same variable name (possibly shaded with a different colour). I could format the data frame input to have column headings of d$y1, d$y2, d$y1lo, d$y2lo, d$y1hi, and d$y2hi, all under the same variable name (d$x).

Would this require alot of changes to your current function?

Abdool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment