如何替换(红色)水平误差线以便在 R 中使用 ggplot 实现更轻松的可视化?

How can I displace the (red) horizontal errorbars to enable easier visualisation using ggplot in R?

我有以下ggplot

如何将红色(或蓝色,无关紧要)水平误差线从水平灰线向上或向下移动一点,以便更好地显示 reader?

代码在这里

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

谢谢

position = position_nudge()是你的朋友

已更新以包含 OP 关于移动点数的建议。


# `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")
  )

reprex package (v0.3.0)

于 2020-05-14 创建