David Atkins
2010-Apr-19 16:29 UTC
[R] plotting RR, 95% CI as table and figure in same plot
Hi all--
I am in the process of helping colleagues write up a ms in which we fit
zero-inflated Poisson models. I would prefer plotting the rate ratios
and 95% CI (as I've found Gelman and others convincing about plotting
tables...), but our journals usually like the numbers themselves.
Thus, I'm looking at a recent JAMA article in which both numbers and
dotplot of RR and 95% CI are presented and wondering about best way to
do this in R.
Essentially, the plot has 3 columns: variable names, RR and 95% CI, and
dotplot of the same.
Using the bioChemists data in the pscl package and errbar function in
Hmisc package, the code below is in the right direction... but still
pretty ugly.
Wondering if folks would have alternative suggestions about how to go
about this, or pointers on cleaning up the code below (eg, I know there
are many functions for plotting errbars/CI).
[And, obviously, there are somethings that would be straightforward to
clean-up such as supplying better variable names, etc., just wanted to
see if there were better overall suggestions before getting too far on
this route.]
Thanks in advance.
cheers, Dave
library(Hmisc)
library(pscl)
## data
data("bioChemists", package = "pscl")
fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
summary(fm_pois)
### pull out rate-ratios and 95% CI
rr <- exp(cbind(coef(fm_pois), confint(fm_pois)))
rr
### round to 2 decimal places
rr <- round(rr, 2)
### plot
par(mfrow=c(1,3))
plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
axes = FALSE, ylab=NULL, xlab=NULL)
text(row.names(rr), x = 1, y = 1:6)
plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
axes = FALSE, ylab=NULL, xlab=NULL)
text(paste(rr[,1], " [", rr[,2], ", ", rr[,3],
"]", sep = ""), x = 1, y
= 1:6)
errbar(x = factor(row.names(rr)),
y = rr[,1], yplus = rr[,3],
yminus = rr[,2])
abline(v = 1, lty =2)
--
Dave Atkins, PhD
Research Associate Professor
Department of Psychiatry and Behavioral Science
University of Washington
datkins at u.washington.edu
Center for the Study of Health and Risk Behaviors (CSHRB)
1100 NE 45th Street, Suite 300
Seattle, WA 98105
206-616-3879
http://depts.washington.edu/cshrb/
(Mon-Wed)
Center for Healthcare Improvement, for Addictions, Mental Illness,
Medically Vulnerable Populations (CHAMMP)
325 9th Avenue, 2HH-15
Box 359911
Seattle, WA 98104?
206-897-4210
http://www.chammp.org
(Thurs)
Thomas Lumley
2010-Apr-19 16:43 UTC
[R] plotting RR, 95% CI as table and figure in same plot
You could try the forestplot() function in rmeta, or the original grid code on
which it is based,
http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter1.html
-thomas
On Mon, 19 Apr 2010, David Atkins wrote:
>
> Hi all--
>
> I am in the process of helping colleagues write up a ms in which we fit
> zero-inflated Poisson models. I would prefer plotting the rate ratios and
> 95% CI (as I've found Gelman and others convincing about plotting
tables...),
> but our journals usually like the numbers themselves.
>
> Thus, I'm looking at a recent JAMA article in which both numbers and
dotplot
> of RR and 95% CI are presented and wondering about best way to do this in
R.
>
> Essentially, the plot has 3 columns: variable names, RR and 95% CI, and
> dotplot of the same.
>
> Using the bioChemists data in the pscl package and errbar function in Hmisc
> package, the code below is in the right direction... but still pretty ugly.
>
> Wondering if folks would have alternative suggestions about how to go about
> this, or pointers on cleaning up the code below (eg, I know there are many
> functions for plotting errbars/CI).
>
> [And, obviously, there are somethings that would be straightforward to
> clean-up such as supplying better variable names, etc., just wanted to see
if
> there were better overall suggestions before getting too far on this
route.]
>
> Thanks in advance.
>
> cheers, Dave
>
> library(Hmisc)
> library(pscl)
>
> ## data
> data("bioChemists", package = "pscl")
> fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
> summary(fm_pois)
>
> ### pull out rate-ratios and 95% CI
> rr <- exp(cbind(coef(fm_pois), confint(fm_pois)))
> rr
> ### round to 2 decimal places
> rr <- round(rr, 2)
>
> ### plot
> par(mfrow=c(1,3))
> plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
> axes = FALSE, ylab=NULL, xlab=NULL)
> text(row.names(rr), x = 1, y = 1:6)
>
> plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
> axes = FALSE, ylab=NULL, xlab=NULL)
> text(paste(rr[,1], " [", rr[,2], ", ", rr[,3],
"]", sep = ""), x = 1, y =
> 1:6)
>
> errbar(x = factor(row.names(rr)),
> y = rr[,1], yplus = rr[,3],
> yminus = rr[,2])
> abline(v = 1, lty =2)
>
> --
> Dave Atkins, PhD
> Research Associate Professor
> Department of Psychiatry and Behavioral Science
> University of Washington
> datkins at u.washington.edu
>
> Center for the Study of Health and Risk Behaviors (CSHRB)
> 1100 NE 45th Street, Suite 300
> Seattle, WA 98105
> 206-616-3879
> http://depts.washington.edu/cshrb/
> (Mon-Wed)
>
> Center for Healthcare Improvement, for Addictions, Mental Illness,
> Medically Vulnerable Populations (CHAMMP)
> 325 9th Avenue, 2HH-15
> Box 359911
> Seattle, WA 98104?
> 206-897-4210
> http://www.chammp.org
> (Thurs)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
Christos Argyropoulos
2010-Apr-19 20:07 UTC
[R] plotting RR, 95% CI as table and figure in same plot
ggplot2 should work (resize to get the plot to the dimensions you need for the
paper)
library(Hmisc)
library(pscl)
library(ggplot2)
## data
data("bioChemists", package = "pscl")
fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
summary(fm_pois)
### pull out rate-ratios and 95% CI
rr <- exp(cbind(coef(fm_pois), confint(fm_pois)))
rr
### round to 2 decimal places
rr <- as.data.frame(round(rr, 2))
colnames(rr)<-c("y","ymin","ymax")
rr$labl<-rownames(rr) ## Change this to meaningful labels
rr$x<-1:length(rownames(rr))
gpl<-ggplot(rr,aes(x,y,ymin=ymin,ymax=ymax))
gpl+geom_point()+geom_linerange()+
geom_hline(aes(yintercept=1),
linetype="dashed",size=0.5)+
geom_text(aes(x,y=0.3,label=y,hjust=0),size=3)+
geom_text(aes(x,y=0.0,label=labl),size=3)+
geom_text(aes(x,y=0.5,label=paste("[",ymin,",",ymax,"]",sep="")
,hjust=0.0),size=3)+
ylab("Relative Risk")+xlab("")+
coord_cartesian(ylim=c(-1,1.7))+
coord_cartesian(xlim=c(0.85,6.15))+
scale_x_continuous(breaks=NA)+
scale_y_continuous(breaks=seq(0.8,1.6,.1))+
opts(
panel.grid.major = theme_blank(),
panel.grid.minor=theme_blank(),
title="",
panel.background = theme_rect(fill=NA,colour=NA)
)+
coord_flip()+
geom_text(aes(x=6.3,y=0.35,label="RR"),size=4)+
geom_text(aes(x=6.3,y=0.60,label="95% CI"),size=4)
Christos
> Date: Mon, 19 Apr 2010 09:29:48 -0700
> From: datkins@u.washington.edu
> To: r-help@r-project.org
> Subject: [R] plotting RR, 95% CI as table and figure in same plot
>
>
> Hi all--
>
> I am in the process of helping colleagues write up a ms in which we fit
> zero-inflated Poisson models. I would prefer plotting the rate ratios
> and 95% CI (as I've found Gelman and others convincing about plotting
> tables...), but our journals usually like the numbers themselves.
>
> Thus, I'm looking at a recent JAMA article in which both numbers and
> dotplot of RR and 95% CI are presented and wondering about best way to
> do this in R.
>
> Essentially, the plot has 3 columns: variable names, RR and 95% CI, and
> dotplot of the same.
>
> Using the bioChemists data in the pscl package and errbar function in
> Hmisc package, the code below is in the right direction... but still
> pretty ugly.
>
> Wondering if folks would have alternative suggestions about how to go
> about this, or pointers on cleaning up the code below (eg, I know there
> are many functions for plotting errbars/CI).
>
> [And, obviously, there are somethings that would be straightforward to
> clean-up such as supplying better variable names, etc., just wanted to
> see if there were better overall suggestions before getting too far on
> this route.]
>
> Thanks in advance.
>
> cheers, Dave
>
> library(Hmisc)
> library(pscl)
>
> ## data
> data("bioChemists", package = "pscl")
> fm_pois <- glm(art ~ ., data = bioChemists, family = poisson)
> summary(fm_pois)
>
> ### pull out rate-ratios and 95% CI
> rr <- exp(cbind(coef(fm_pois), confint(fm_pois)))
> rr
> ### round to 2 decimal places
> rr <- round(rr, 2)
>
> ### plot
> par(mfrow=c(1,3))
> plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
> axes = FALSE, ylab=NULL, xlab=NULL)
> text(row.names(rr), x = 1, y = 1:6)
>
> plot(0, type = "n", xlim=c(0,2), ylim=c(1,6),
> axes = FALSE, ylab=NULL, xlab=NULL)
> text(paste(rr[,1], " [", rr[,2], ", ", rr[,3],
"]", sep = ""), x = 1, y
> = 1:6)
>
> errbar(x = factor(row.names(rr)),
> y = rr[,1], yplus = rr[,3],
> yminus = rr[,2])
> abline(v = 1, lty =2)
>
> --
> Dave Atkins, PhD
> Research Associate Professor
> Department of Psychiatry and Behavioral Science
> University of Washington
> datkins@u.washington.edu
>
> Center for the Study of Health and Risk Behaviors (CSHRB)
> 1100 NE 45th Street, Suite 300
> Seattle, WA 98105
> 206-616-3879
> http://depts.washington.edu/cshrb/
> (Mon-Wed)
>
> Center for Healthcare Improvement, for Addictions, Mental Illness,
> Medically Vulnerable Populations (CHAMMP)
> 325 9th Avenue, 2HH-15
> Box 359911
> Seattle, WA 98104?
> 206-897-4210
> http://www.chammp.org
> (Thurs)
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
_________________________________________________________________
Hotmail: Trusted email with powerful SPAM protection.
[[alternative HTML version deleted]]