Darla Carvallo
Darla Carvallo

Reputation: 23

How can I add more text into a plot using ggplot2 in R?

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

Upvotes: 0

Views: 146

Answers (2)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

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

Upvotes: 1

Ric
Ric

Reputation: 5721

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

Upvotes: 1

Related Questions