2

How can I neatly annotate the four points in this forest plot using the "estimate" column? I would like them vertically aligned if possible.

library(ggplot2)
library(wesanderson)

## make data

df1 <- data.frame(matrix(, nrow=4, ncol=5))
colnames(df1) <- c("Metabolite", "OR", "CI_low", "CI_high", "Method")
df1$Metabolite <- c("Isoleucine", "Leucine", "Isoleucine", "Leucine")
df1$OR <- c(0.75, 0.64, 0.88, 0.95)
df1$CI_low <- c(0.61, 0.48, 0.8, 0.85)
df1$CI_high <- c(0.89, 0.8, 0.96, 1.05)
df1$Method <- c("Metabolomic", "Metabolomic", "GRS", "GRS")
df1$estimate <- c("0.75 (0.61, 0.89)", "0.64 (0.48, 0.8)",  "0.88 (0.8, 0.96)",  "0.95 (0.85, 1.05)")

# Plot using ggplot ------------------------------

ggplot(df1, aes(x=Metabolite, y=OR, ymin=CI_low, ymax=CI_high,col=Method,fill=Method)) + 
  #specify position here
  geom_linerange(size=1,position=position_dodge(width = 0.5), show.legend = FALSE) +
  geom_hline(yintercept=1, lty=2) +
  #specify position here too
  geom_point(size=3, shape=21, colour="white", stroke = 0.5,position=position_dodge(width = 0.5)) +
  # scale_fill_manual(values=barCOLS)+
  # scale_color_manual(values=dotCOLS)+
  scale_fill_manual(values = wes_palette(name = "GrandBudapest2")) +
  scale_color_manual(values = wes_palette(name = "GrandBudapest2")) +
  scale_x_discrete(name="Metabolite") +
  scale_y_continuous(name="Odds ratio (95% CI)", 
                     limits = c(0.4, 1.5), 
                     breaks = c(0.4, 1, 1.5),
                     labels=c("0.4", "1", "1.5")) +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme_classic()
2
  • 1
    Who's W. E. Sanderson? ;-) Commented Jan 8, 2021 at 14:21
  • google.com/… Commented Jan 8, 2021 at 14:23

1 Answer 1

4

How about a typical geom_text() with a dodge position?

library(ggplot2)
library(wesanderson)

## make data

df1 <- data.frame(matrix(nrow=4, ncol=5))
colnames(df1) <- c("Metabolite", "OR", "CI_low", "CI_high", "Method")
df1$Metabolite <- c("Isoleucine", "Leucine", "Isoleucine", "Leucine")
df1$OR <- c(0.75, 0.64, 0.88, 0.95)
df1$CI_low <- c(0.61, 0.48, 0.8, 0.85)
df1$CI_high <- c(0.89, 0.8, 0.96, 1.05)
df1$Method <- c("Metabolomic", "Metabolomic", "GRS", "GRS")
df1$estimate <- c("0.75 (0.61, 0.89)", "0.64 (0.48, 0.8)",  "0.88 (0.8, 0.96)",  "0.95 (0.85, 1.05)")

# Plot using ggplot ------------------------------

ggplot(df1, aes(x=Metabolite, y=OR, ymin=CI_low, ymax=CI_high,col=Method,fill=Method)) + 
  geom_linerange(size=1,position=position_dodge(width = 0.5), show.legend = FALSE) +
  geom_hline(yintercept=1, lty=2) +
  geom_point(size=3, shape=21, colour="white", stroke = 0.5,position=position_dodge(width = 0.5)) +
  geom_text(aes(y = 0.8,label = estimate), 
            position = position_dodge(width = 1),
            show.legend = FALSE, hjust = 0.5) +
  scale_fill_manual(values = wes_palette(name = "GrandBudapest2")) +
  scale_color_manual(values = wes_palette(name = "GrandBudapest2")) +
  scale_x_discrete(name="Metabolite") +
  scale_y_continuous(name="Odds ratio (95% CI)", 
                     limits = c(0.4, 1.5), 
                     breaks = c(0.4, 1, 1.5),
                     labels=c("0.4", "1", "1.5")) +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme_classic()

Created on 2021-01-08 by the reprex package (v0.3.0)

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

Comments

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.