R误差条形图,采用并列格式。

huangapple go评论62阅读模式
英文:

R error bar plot with side by side format

问题

图表 <- ggplot(data, aes(x=x, y=y, color=sen_spe, fill=sen_spe)) + geom_point() + geom_errorbar(aes(ymin = lower, ymax = upper))
英文:

I want to create a side by side plot with error bar. I have sensitivity and specificity for each state. I also have upper bound and lower bound for each of the sensitivity and specificity. I want to draw a plot which shows the sensitivity/specificity with the upper and lower bound. My code is below. But it's not side by side. I want side by side like https://stackoverflow.com/questions/29995480/side-by-side-r-barplot-with-error-bars.

library(&quot;ggplot2&quot;)
data &lt;- data.frame(x = c(&quot;AZ&quot;,&quot;AZ&quot;,&quot;CT&quot;,&quot;CT&quot;,&quot;IL&quot;,&quot;IL&quot;),
                   sen_spe = c(rep(c(&quot;Sensitivity&quot;,&quot;Specificity&quot;),3)),
                         y = runif(6, 0, 1),
                         lower = runif(6, 0, 0.1),
                         upper = runif(6, 0, 0.1))

data$lower &lt;- data$y - data$lower
data$upper &lt;- data$y + data$upper


ggplot(data, aes(x=x, y=y, color=sen_spe, fill=sen_spe)) + geom_point() + geom_errorbar(aes(ymin = lower, ymax = upper))  

答案1

得分: 0

为了使误差条和数据点并排显示,您需要告诉 ggplot2 这样做,使用 position = position_dodge(width = XXX),其中 width 决定了误差条和数据点的偏移量或左右移动的幅度:

library(ggplot2)

set.seed(123)

ggplot(data, aes(x = x, y = y, color = sen_spe, fill = sen_spe)) +
  geom_point(position = position_dodge(width = .75)) +
  geom_errorbar(aes(ymin = lower, ymax = upper),
    position = position_dodge(width = .75), width = .45
  )

R误差条形图,采用并列格式。

英文:

To get your error bars (and the points) side by you have to tell ggplot2 to do so by using position = position_dodge(width = XXX) where the width determines by how much the error bars and points get dodged or shifted to the left/right:

library(ggplot2)

set.seed(123)

ggplot(data, aes(x = x, y = y, color = sen_spe, fill = sen_spe)) +
  geom_point(position = position_dodge(width = .75)) +
  geom_errorbar(aes(ymin = lower, ymax = upper),
    position = position_dodge(width = .75), width = .45
  )

R误差条形图,采用并列格式。

huangapple
  • 本文由 发表于 2023年4月20日 01:59:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定