Home > front end >  How can I add more text into a plot using ggplot2 in R?
How can I add more text into a plot using ggplot2 in R?

Time:11-10

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 :(

CodePudding user response:

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()

CodePudding user response:

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(estimate_lab = paste0(effect, " (", lower, ",  ", upper, ")")) %>%
  arrange(-index) %>%
  forestplot::forestplot(
    labeltext = c(outcome, estimate_lab),
    mean = effect,
    low = lower,
    upper = upper,
    size = 0.2,
    graph.pos = 2,
    xlab = "Relative Risk", 
    title="Adjusted Relative Risk for Complications"
  ) %>% 
  fp_add_header(
    outcome = c("Outcome"),
    estimate_lab = c("Estimate (95% CI)")
  )

enter image description here

  • Related