英文:
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("ggplot2")
data <- data.frame(x = c("AZ","AZ","CT","CT","IL","IL"),
sen_spe = c(rep(c("Sensitivity","Specificity"),3)),
y = runif(6, 0, 1),
lower = runif(6, 0, 0.1),
upper = runif(6, 0, 0.1))
data$lower <- data$y - data$lower
data$upper <- 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
)
英文:
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
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论