0

I have this graph that I made using this code:

df3 <- data.frame(outcome=c("Any complication", "Cardiac complication",
                            "Vascular Complication","Vascular complication",
                            "Respiratory complication", "Infectious complication",
                            "Neurological complication"),
                 index=1:7,
                 effect=c(-.4, -.25, -.1, .1, .15, .2, .3),
                 lower=c(-.43, -.29, -.17, -.02, .04, .17, .27),
                 upper=c(-.37, -.21, -.03, .22, .24, .23, .33))

plot2 <- ggplot(data=df3, aes(y=index, x=effect, xmin=lower, xmax=upper))+
  geom_point(shape="diamond", colour="royalblue4", size=5) + 
  geom_errorbarh(height=.2, colour="royalblue4") +
  scale_y_continuous(breaks=1:nrow(df3), labels=df3$outcome) +
  labs(title="Adjusted Relative Risk for Complications", x="Relative Risk", y = "Type of complication") +
  geom_vline(xintercept=0, color='gray', linetype='dashed', alpha=.5) +
  theme_minimal()+
  plots_theme

My graph

But I would like to add the RR, the 95% confidence interval and some other things there, as shown here:

The graph I want :)

I could really use some help! Quite new in R here :(

2 Answers 2

1

To make it easier, I would borrow one of several R packages which can make forest plots. As a quick start, I use forestplot package here as an example:

library(tidyverse)
library(forestplot)
df3 %>%
  mutate(ci = paste0(" (", lower, ",  ", upper, ")")) %>%
  arrange(-index) %>%
  forestplot::forestplot(
    labeltext = c(outcome, effect, ci),
    mean = effect, 
    low = lower,
    upper = upper,
    size = 0.1,
    graph.pos = 2,
    vertices = TRUE,
    xlab = "Relative Risk", 
    title="Adjusted Relative Risk for Complications") %>% 
  fp_add_header(
    outcome = c("Outcome"),
    effect = c("Estimate"), 
    ci = "95% (CI)") %>% 
  fp_set_style(box = "royalblue4", line = "royalblue4") 


enter image description here

Edit

Just something different from your question, the Estimate values shown in the graph may not be relative risk but regression coefficient. If you would like to present relative risk, you could take an advantage of meta package:

library(meta)
df3 <- df3 %>% arrange(-index)
forest_df <-metagen(lower = lower, 
              upper = upper, 
              TE = effect,
              data = df3, 
              sm = "RR")

forest(forest_df,
       studlab = paste0(outcome), 
       layout = "JAMA", 
       common = FALSE, 
       hetstat = FALSE, 
       overall = FALSE)

Get work done with less code and journal format.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

OMG I just realized this! How silly hahaha of course RR can't be negative! Thank you so much for your input, highly appreciated!!!!
1

Here a way with geom_rect, geom_text and annotate.

library(ggplot2)
df3 <- data.frame(outcome=c("Any complication", "Cardiac complication",
                            "Vascular Complication","Vascular complication",
                            "Respiratory complication", "Infectious complication",
                            "Neurological complication"),
                 index=1:7,
                 effect=c(-.4, -.25, -.1, .1, .15, .2, .3),
                 lower=c(-.43, -.29, -.17, -.02, .04, .17, .27),
                 upper=c(-.37, -.21, -.03, .22, .24, .23, .33))

ggplot(data=df3, aes(y=index, x=effect, xmin=lower, xmax=upper))+
  geom_point(shape="diamond", colour="royalblue4", size=5) + 
  geom_errorbarh(height=.2, colour="royalblue4") +
  scale_y_continuous(breaks=1:nrow(df3), labels=df3$outcome) +
  labs(title="Adjusted Relative Risk for Complications", x="Relative Risk", y = "Type of complication") +
  geom_vline(xintercept=0, color='gray', linetype='dashed', alpha=.5) +
  annotate("rect",  xmin=.5, xmax= 1, ymin=0, ymax= 8, fill = "white")+
  geom_text(aes(label = paste(effect, "(", lower, ",", upper,")")) ,
            x= 0.5, hjust="left") +
  scale_x_continuous(limits = c(-0.5, 1.0))+
  annotate("text", label = "Estimate  - 95% CI", x = 0.5, hjust="left", y= 8)+
  theme_minimal() + theme()

1 Comment

This was super helpful to understand annotate and geom_text! Thank you so much!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.