0

I have the following ggplot

Horizontal errorbars

How can I displace the red (or blue, doesn't matter) horizontal errorbar a bit up or down from the horizontal grey line in order to make a better visualisation for the reader?

Code comes here

set.seed(121)
Varnames <- c("Age", "Weight", "Length")  # Variable names

uVarvalues <- runif(3, 1, 5)          # Univariate Odds ratios
uspread <- runif(3, 1, 2)             # Confidence intervals
uCI_hi <- uVarvalues + uspread/2        # Upper level of CIs
uCI_lo <- uVarvalues - uspread/2        # Lower level of CIs

mVarvalues <- runif(3, 1, 5)          # Multivariate Odds ratios
mspread <- runif(3, 0, 2)             # Confidence intervals
mCI_hi <- mVarvalues + mspread/2        # Upper level of CIs
mCI_lo <- mVarvalues - mspread/2        # Lower level of CIs

library(ggplot2)
vertline <- c(1)                     # preparing vertical lines to be shown in graph
ggplot(data=NULL, aes(x=uVarvalues, y=Varnames)) +
  geom_point(aes(x=uVarvalues, y=Varnames), shape=18, color="deepskyblue3", size=3) +
  ylab(NULL) +
  xlab(NULL) +
  geom_vline(xintercept = vertline,
             size=0.1
  ) +
  geom_errorbarh(
    aes(xmin=uCI_lo, xmax=uCI_hi),
    size=.6,
    height=0.1,
    colour="deepskyblue3",
    linetype="solid"
  ) +
  geom_point(aes(x=mVarvalues, y=Varnames), shape=18, color="firebrick", size=3) +
  geom_errorbarh(
    aes(xmin=mCI_lo, xmax=mCI_hi),
    size=.6,
    height=0.1,
    colour="firebrick",
    linetype="solid"
  ) +
  scale_x_log10(
    breaks = c(0.2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
    labels = c("0.2", "1", "2", "", "","5","","","","", "10", "","","","","15"),
    limits = c(0.2,15)
  ) +
  theme(
    panel.background = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(size = 0.25, linetype = 'solid', colour = "grey"),
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    axis.text.x = element_text(family="Georgia", size=10),
    axis.text.y =element_text(family="Courier New", size=10, face="bold")
  )

Thank you

pha
  • 316
  • 2
  • 9
  • This could be useful https://stackoverflow.com/questions/43827332/ggplot2-dodge-horizontal-error-bars-with-points – desval May 13 '20 at 20:18

1 Answers1

2

position = position_nudge() is your friend

Updated to include OP's suggestion to move points too.


# `position = position_nudge()` is your friend:


set.seed(121)
Varnames <- c("Age", "Weight", "Length")  # Variable names

uVarvalues <- runif(3, 1, 5)          # Univariate Odds ratios
uspread <- runif(3, 1, 2)             # Confidence intervals
uCI_hi <- uVarvalues + uspread/2        # Upper level of CIs
uCI_lo <- uVarvalues - uspread/2        # Lower level of CIs

mVarvalues <- runif(3, 1, 5)          # Multivariate Odds ratios
mspread <- runif(3, 0, 2)             # Confidence intervals
mCI_hi <- mVarvalues + mspread/2        # Upper level of CIs
mCI_lo <- mVarvalues - mspread/2        # Lower level of CIs

library(ggplot2)
vertline <- c(1)                     # preparing vertical lines to be shown in graph
ggplot(data=NULL, aes(x=uVarvalues, y=Varnames)) +
  geom_point(aes(x=uVarvalues, y=Varnames), shape=18, color="deepskyblue3", size=3,
             position = position_nudge(y = 0.05)) +
  ylab(NULL) +
  xlab(NULL) +
  geom_vline(xintercept = vertline,
             size=0.1
  ) +
  geom_errorbarh(
    aes(xmin=uCI_lo, xmax=uCI_hi),
    size=.6,
    height=0.1,
    colour="deepskyblue3",
    linetype="solid",
    position = position_nudge(y = 0.05)
  ) +
  geom_point(aes(x=mVarvalues, y=Varnames), shape=18, color="firebrick", size=3,
             position = position_nudge(y = -0.05)) +
  geom_errorbarh(
    aes(xmin=mCI_lo, xmax=mCI_hi),
    size=.6,
    height=0.1,
    colour="firebrick",
    linetype="solid",
    position = position_nudge(y = -0.05)
  ) +
  scale_x_log10(
    breaks = c(0.2,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15),
    labels = c("0.2", "1", "2", "", "","5","","","","", "10", "","","","","15"),
    limits = c(0.2,15)
  ) +
  theme(
    panel.background = element_blank(),
    axis.ticks.y = element_blank(),
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(size = 0.25, linetype = 'solid', colour = "grey"),
    panel.grid.minor.x = element_blank(),
    panel.grid.minor.y = element_blank(),
    axis.text.x = element_text(family="Georgia", size=10),
    axis.text.y =element_text(family="Courier New", size=10, face="bold")
  )

Created on 2020-05-14 by the reprex package (v0.3.0)

Peter
  • 11,500
  • 5
  • 21
  • 31
  • Excellent, that worked, and I added `position_nudge` to `geom_point` to make the dots follow the same way. – pha May 14 '20 at 12:08
  • Of course, I wish I'd thought of that! too literal a focus on the question. – Peter May 14 '20 at 12:27