RE: Adding OR and 95% CI to the forest plots- repost

82 views
Skip to first unread message

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Nov 4, 2014, 4:49:43 PM11/4/14
to Pradip, ggp...@googlegroups.com

Hello,

 

My apology for sending you earlier an incorrect attachment. Attached please see the forest plots.  Could someone give me hints or help me with the code that I need to add in order to have the OR and 95% CI printed under each geom_hline? I could send you the reprodicble code if needed.

 

Thanks,

 

Pradip Muhuri

 

 

 

 

p <- ggplot(data, aes(x=xcovariate_f, or,y=or, ymin=lc, ymax=uc))+

  geom_pointrange()+

  geom_hline(aes(x=0), lty=2) +

  facet_wrap(~ age_cat_f)+

  coord_flip()+

  xlab ("  ")+

  ylab("  ")+

 

  theme_bw() +

  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+

  theme(strip.text.x=element_text(),

        strip.background = element_rect(colour="black", fill='white')  )

 

  print(p)

 

Pradip K. Muhuri, PhD

SAMHSA/CBHSQ

1 Choke Cherry Road, Room 2-1071

Rockville, MD 20857

Tel: 240-276-1070

Fax: 240-276-1260

 

From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Pradip
Sent: Tuesday, November 04, 2014 4:27 PM
To: ggp...@googlegroups.com
Subject: Adding OR and 95% CI to the forest plots

 

 

Hello,

 

I have made my forest plots (please see the attachment) using the code below. Could someone give me hints or help me with the code that I need to add in order to have the OR and 95% CI printed under each geom_hline?

I could send you the reprodicble code if needed.

 

Thanks,

 

Pradip Muhuri

 

 

##################################################################

p <- ggplot(data, aes(x=xcovariate_f, or,y=or, ymin=lc, ymax=uc))+
  geom_pointrange()+
  geom_hline(aes(x=0), lty=2) +
  facet_wrap(~ age_cat_f)+
  coord_flip()+
  xlab ("  ")+
  ylab("  ")+
 
  theme_bw() +
  theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
  theme(strip.text.x=element_text(),
        strip.background = element_rect(colour="black", fill='white')  )
 
  print(p)

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

forest_plots_indiv.png

Ben Bond-Lamberty

unread,
Nov 5, 2014, 3:19:53 PM11/5/14
to ggp...@googlegroups.com
Hi Pradip,

You're going to get a much better response rate, i.e. more help for
your problem, if you post a short, reproducible example. Glancing at
your code, for example, I see there's no data included, nor is the
problem clearly defined: what's "OR"? You want the 95% CI (of what?)
printed (where?). It confuses me, and and so I delete the message and
move on.

But people on this list are happy to help if we can...if you can
clearly and succinctly define the problem, including a short, clear
example.

Ben

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Nov 6, 2014, 12:10:51 AM11/6/14
to Ben Bond-Lamberty, ggp...@googlegroups.com, Muhuri, Pradip (SAMHSA/CBHSQ)
Hi Ben and Dennis,

Per your advice, I am now sending you the reproducible example (appended below and also attached). I was trying to draw the forest plots for 11 covariates for each of two models by age groups. In the data set, OR =odds ratio, and the lc and uc = confidence interval limits.

As usual, Dennis has provided very useful guidance, suggesting two approaches. I am leaning toward his Approach 2. Although the R script (shown below) has run successfully, the plots are overshadowed with the character string that involves the OR and confidence limits. I was trying to insert the character string to the right side of each of the plot lines. It seems that my geom_text needs to be fixed (y-value, x-value, and hjust in the geom_text).

I would appreciate receiving your advice.

Thanks,

Pradip Muhuri


##### My reproducible example (with data and code) is appended below ##############
#test_forest_plot.R
library(ggplot2)
loadedNamespaces()
#read this data set
temp <- "var age_cat model or lc uc
1.chronic1p 35_49 indiv 2.69 2.35 3.09
2.xalcstat 35_49 indiv 1.30 1.02 1.66
3.xalcstat 35_49 indiv 0.75 0.61 0.92
4.xalcstat 35_49 indiv 0.69 0.54 0.90
5.xalcstat 35_49 indiv 1.26 0.91 1.74
2.xcigsmoke 35_49 indiv 1.42 1.14 1.76
3.xcigsmoke 35_49 indiv 2.18 1.76 2.69
4.xcigsmoke 35_49 indiv 1.23 0.97 1.56
1.pa08_3r 35_49 indiv 1.78 1.46 2.18
2.pa08_3r 35_49 indiv 1.25 0.99 1.59
1.xspd2 35_49 indiv 2.56 1.99 3.29
1.xspd2 35_49 final 1.54 1.16 2.05
2.xcigsmoke 35_49 final 1.44 1.15 1.82
3.xcigsmoke 35_49 final 2.00 1.60 2.49
4.xcigsmoke 35_49 final 1.21 0.94 1.56
2.xalcstat 35_49 final 1.08 0.84 1.38
3.xalcstat 35_49 final 0.69 0.56 0.86
4.xalcstat 35_49 final 0.63 0.48 0.83
5.xalcstat 35_49 final 0.94 0.67 1.31
1.pa08_3r 35_49 final 1.54 1.26 1.89
2.pa08_3r 35_49 final 1.14 0.90 1.45
1.chronic1p 35_49 final 2.46 2.12 2.84
1.chronic1p 50_69 indiv 2.37 2.17 2.59
2.xalcstat 50_69 indiv 1.45 1.30 1.61
3.xalcstat 50_69 indiv 0.80 0.71 0.90
4.xalcstat 50_69 indiv 0.87 0.75 1.01
5.xalcstat 50_69 indiv 1.56 1.32 1.83
2.xcigsmoke 50_69 indiv 2.15 1.89 2.45
3.xcigsmoke 50_69 indiv 2.64 2.38 2.93
4.xcigsmoke 50_69 indiv 1.43 1.29 1.57
1.pa08_3r 50_69 indiv 2.09 1.91 2.28
2.pa08_3r 50_69 indiv 1.42 1.26 1.62
1.xspd2 50_69 indiv 2.11 1.85 2.40
1.xspd2 50_69 final 1.44 1.24 1.66
2.xcigsmoke 50_69 final 2.11 1.84 2.42
3.xcigsmoke 50_69 final 2.31 2.08 2.57
4.xcigsmoke 50_69 final 1.33 1.20 1.48
2.xalcstat 50_69 final 1.22 1.09 1.37
3.xalcstat 50_69 final 0.77 0.68 0.87
4.xalcstat 50_69 final 0.81 0.69 0.95
5.xalcstat 50_69 final 1.21 1.02 1.44
1.pa08_3r 50_69 final 1.81 1.65 1.99
2.pa08_3r 50_69 final 1.34 1.18 1.53
1.chronic1p 50_69 final 2.20 2.01 2.40
1.chronic1p 70_84 indiv 1.89 1.74 2.05
2.xalcstat 70_84 indiv 1.29 1.20 1.38
3.xalcstat 70_84 indiv 0.82 0.76 0.89
4.xalcstat 70_84 indiv 0.79 0.70 0.88
5.xalcstat 70_84 indiv 1.33 1.14 1.56
2.xcigsmoke 70_84 indiv 2.32 2.08 2.59
3.xcigsmoke 70_84 indiv 2.74 2.47 3.05
4.xcigsmoke 70_84 indiv 1.54 1.45 1.64
1.pa08_3r 70_84 indiv 2.27 2.11 2.45
2.pa08_3r 70_84 indiv 1.46 1.33 1.60
1.xspd2 70_84 indiv 1.97 1.72 2.24
1.xspd2 70_84 final 1.52 1.33 1.75
2.xcigsmoke 70_84 final 2.33 2.07 2.61
3.xcigsmoke 70_84 final 2.51 2.24 2.82
4.xcigsmoke 70_84 final 1.55 1.45 1.66
2.xalcstat 70_84 final 1.08 1.01 1.17
3.xalcstat 70_84 final 0.78 0.71 0.84
4.xalcstat 70_84 final 0.74 0.66 0.83
5.xalcstat 70_84 final 1.08 0.92 1.27
1.pa08_3r 70_84 final 2.04 1.88 2.20
2.pa08_3r 70_84 final 1.41 1.28 1.56
1.chronic1p 70_84 final 1.80 1.66 1.95"
data <- read.table(textConnection(temp), header=TRUE, as.is=TRUE)
#load("forest_plot_data.rdata")
data <- forest_plot_data
# convert to factor
data$age_cat_f <- factor(data$age_cat,
levels = c("35_49", "50_69", "70_84"),
labels = c("Ages 35-49","Ages 50-69", "Ages 70-84")
)
# convert to factor
data$covariate_f <- factor(data$var,
levels = c("1.xspd2", "2.xcigsmoke", "3.xcigsmoke", "4.xcigsmoke",
"2.xalcstat", "3.xalcstat", "4.xalcstat","5.xalcstat",
"1.pa08_3r", "2.pa08_3r", "1.chronic1p"
),
labels = c("Past-month SPD","Smoke cigrette <1 pack",
"Smoke cigrette 1+ pack", "Former smoker",
"Former drinker", "Light drinker","Moderate drinker",
"Heavy drinker","No activity","Insufficiently active",
"1+ chronic conditions"
)
)
# convert to factor
data$model_f <- factor(data$model,
levels = c("indiv", "final"),
labels = c("(M1)", "(M2)")
)
# create a character variable
data$xcovariate_f <- paste(data$covariate_f, data$model_f, sep=" ")
# convert to factor
data$xcovariate_f <- factor(data$xcovariate_f,
levels=c("Former drinker (M2)",
"Former drinker (M1)",
"Light drinker (M2)",
"Light drinker (M1)",
"Moderate drinker (M2)",
"Moderate drinker (M1)",
"Heavy drinker (M2)",
"Heavy drinker (M1)",
"Insufficiently active (M2)",
"Insufficiently active (M1)" ,
"No activity (M2)",
"No activity (M1)",
"Former smoker (M2)",
"Former smoker (M1)",
"Smoke cigrette <1 pack (M2)",
"Smoke cigrette <1 pack (M1)",
"Smoke cigrette 1+ pack (M2)" ,
"Smoke cigrette 1+ pack (M1)" ,
"1+ chronic conditions (M2)",
"1+ chronic conditions (M1)",
"Past-month SPD (M2)",
"Past-month SPD (M1)"
)
)
# construct a character string with OR, LC, and UC
data$string.est <- paste(sprintf('%.2f',data$or), " (",
sprintf('%.2f',data$lc), ",",
sprintf('%.2f',data$uc),")", sep=""
)

p <- ggplot(data, aes(x=xcovariate_f, or,y=or, ymin=lc, ymax=uc))+
geom_pointrange()+
geom_hline(aes(x=0), lty=2) +
geom_text( aes (label= string.est, y=.4),
hjust = -.2, colour="black")+
facet_wrap(~ age_cat_f)+
coord_flip()+
xlab (" ")+
ylab(" ")+
theme_bw() +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
theme(strip.text.x=element_text(),
strip.background = element_rect(colour="black", fill='white')
)

print(p)


________________________________________
From: ggp...@googlegroups.com [ggp...@googlegroups.com] on behalf of Ben Bond-Lamberty [bpb...@gmail.com]
Sent: Wednesday, November 05, 2014 3:19 PM
To: ggp...@googlegroups.com
Subject: Re: Adding OR and 95% CI to the forest plots- repost
test_forest_plots.R

Ron Crump

unread,
Nov 6, 2014, 4:13:37 AM11/6/14
to ggp...@googlegroups.com, Pradip...@samhsa.hhs.gov
Hi Pradip,

> As usual, Dennis has provided very useful guidance, suggesting two approaches. I am leaning toward his Approach 2. Although the R script (shown below) has run successfully, the plots are overshadowed with the character string that involves the OR and confidence limits. I was trying to insert the character string to the right side of each of the plot lines. It seems that my geom_text needs to be fixed (y-value, x-value, and hjust in the geom_text).
I think this is doing pretty much what you want (removed geom_text from
your code, then played with it
separately):

p <- ggplot(data, aes(x=xcovariate_f, or,y=or, ymin=lc, ymax=uc))+
geom_pointrange()+
geom_hline(aes(x=0), lty=2) + facet_wrap(~ age_cat_f)+
coord_flip()+
xlab (" ")+
ylab(" ")+
theme_bw() +
theme(panel.grid.major=element_blank(),panel.grid.minor=element_blank())+
theme(strip.text.x=element_text(),
strip.background = element_rect(colour="black",
fill='white') )

p+geom_text(aes(label=string.est,y=uc),hjust=-0.02,colour="black",size=3)+
scale_y_continuous(limits=c(0,4.25))

Using uc to set the y coordinate is what you need to do if you want the
labels just to the right
of the line (ie different position for each label). I'm not sure that
looks very nice, so if you want
them at a constant position to the right of all the lines, then it is
just of case of setting the y
coordinate appropriately, eg:

p+geom_text(aes(label=string.est,y=max(uc)+0.1),hjust=-0.02,colour="black",size=3)+scale_y_continuous(limits=c(0,4.25))

The other things are just tweaks to adjust positions and size of the
text and to make
enough room in the plot for the labels.

You will want to play with the values a bit.

Ron.

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Nov 6, 2014, 8:25:11 AM11/6/14
to Ron Crump, ggp...@googlegroups.com, Muhuri, Pradip (SAMHSA/CBHSQ)
Hello Ron,

Thank you so much for your guidance on the geom_text. I like your idea of setting the y coordinate at a constant position to the right of all lines.

Inserting your code renders the desired output only after now=3 is specified on the facet_wrap(). I could not make the code work without specifying nrow=3 on the facet_wrap(). Below are my questions:

1) What to tweak to make your code work without nrow=3 on the facet_wrap()?
2) What would I specify to change the orientation of the chart to landscape?
3) What is the best way to increase the font size globally?
4) Would you please explain to me how this code [y=max(uc)+0.1)] sets the y coordinate at a constant position?

I look forward to receiving your advice/hints.

Pradip




________________________________________
From: Ron Crump [R.E....@warwick.ac.uk]
Sent: Thursday, November 06, 2014 4:12 AM
To: ggp...@googlegroups.com; Muhuri, Pradip (SAMHSA/CBHSQ)
Subject: Re: Adding OR and 95% CI to the forest plots- repost

Ron Crump

unread,
Nov 7, 2014, 3:36:05 AM11/7/14
to Muhuri, Pradip (SAMHSA/CBHSQ), ggp...@googlegroups.com
Hi Pradip,
> Thank you so much for your guidance on the geom_text. I like your idea of setting the y coordinate at a constant position to the right of all lines.
>
> Inserting your code renders the desired output only after now=3 is specified on the facet_wrap(). I could not make the code work without specifying nrow=3 on the facet_wrap(). Below are my questions:
>
> 1) What to tweak to make your code work without nrow=3 on the facet_wrap()?
I don't understand. My code worked without nrow=3. If you set nrow=3
don't you
get the graphs in a single column? That isn't what was in your original
image or
what the code you provided did.
> 2) What would I specify to change the orientation of the chart to landscape?
Again, I don't understand what you mean here. Three charts in one column vs
three charts in one row? x and y swapped around? Different aspect ratio?
> 3) What is the best way to increase the font size globally?
Try using the help system. The default font size can be set when
initialising a device
to write to (see, for example, ?pdf ) or within a theme (see ?theme_set ).
> 4) Would you please explain to me how this code [y=max(uc)+0.1)] sets the y coordinate at a constant position?
It takes the maximum value of uc, your upper 95% CI limits, in the
data.frame and adds 0.1 to it. So
it has a single value used for all x-values.

Ron.

Muhuri, Pradip (SAMHSA/CBHSQ)

unread,
Nov 8, 2014, 11:03:49 PM11/8/14
to Ron Crump, ggp...@googlegroups.com, Dennis Murphy, Muhuri, Pradip (SAMHSA/CBHSQ)
Hello Ron,

Thank you so much for your clarifications and help with the chart. Dennis Murphy's further tweaks to the R script provides me a much nicer chart. Per his suggestions, I am now posting an updated version of the reproducible code below and the chart in the attachment.

Regards,

Pradip
################################## Updated version of the code (Acknowledgements to Dennis Murphy and Ron Crump) ##################################
data <- forest_plot_data

# convert to factor
data$age_cat_f <- factor(data$age_cat,
levels = c("35_49", "50_69", "70_84"),
labels = c("Ages 35-49","Ages 50-69", "Ages 70-84")
)
# convert to factor
data$covariate_f <- factor(data$var,
levels = c("1.xspd2", "2.xcigsmoke", "3.xcigsmoke", "4.xcigsmoke",
"2.xalcstat", "3.xalcstat", "4.xalcstat","5.xalcstat",
"1.pa08_3r", "2.pa08_3r", "1.chronic1p"
),
labels = c("Past-month SPD","Smoke cigarette <1 pack",
"Smoke cigarette 1+ pack", "Former smoker",
"Smoke cigarette <1 pack (M2)",
"Smoke cigarette <1 pack (M1)",
"Smoke cigarette 1+ pack (M2)" ,
"Smoke cigarette 1+ pack (M1)" ,
"1+ chronic conditions (M2)",
"1+ chronic conditions (M1)",
"Past-month SPD (M2)",
"Past-month SPD (M1)"
)
)
# construct a character string with OR, LC, and UC
data$string.est <- paste(sprintf('%.2f',data$or), " (",
sprintf('%.2f',data$lc), ",",
sprintf('%.2f',data$uc),")", sep=""
)

#colnames(data) <- "Potential predictors"
p <- ggplot(data, aes(x=xcovariate_f, y=or, ymin=lc, ymax=uc))+
geom_pointrange(size = 0.8)+
geom_hline(aes(x=0), lty=2) +
geom_text(y = 0.2, aes(label= string.est,
x = as.numeric(data$xcovariate_f) - 0.5),
size = 2.5, hjust = 0, colour="black")+
facet_wrap(~ age_cat_f)+
coord_flip()+
xlab (" ")+
ylab("Adjusted hazard ratio (95% CI) ")+
theme_bw() +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
strip.background = element_rect(colour="black",
fill='white')
)


print(p)
ggsave(file='H:/forest_plots_final.png', width=11, height=8.5)



Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260

-----Original Message-----
From: ggp...@googlegroups.com [mailto:ggp...@googlegroups.com] On Behalf Of Ron Crump
Sent: Friday, November 07, 2014 3:36 AM
To: Muhuri, Pradip (SAMHSA/CBHSQ); ggp...@googlegroups.com
Subject: Re: Adding OR and 95% CI to the forest plots- repost

forest_plots_final.png
Reply all
Reply to author
Forward
0 new messages