我正在尝试在 R 中制作一个具有 95% CI 的水平分组条形图。我创建了一个包含所有数据的 CSV 文件(见下文)。到目前为止我的代码是:
geom_bar(aes(fill = Descriptor), position = "dodge", stat = "identity") +
geom_errorbar(aes(ymax=Upper_CI, ymin=Lower_CI), position= position_dodge(0.9), width=0.25)
ylab("Prevalence (%)") +
ylim(0,100)
当我在 R 上输入此代码时,我得到结果“NULL”。你能帮我么?如果您需要更多信息,请与我们联系。谢谢!
参见上面的描述。
您的图层之前似乎没有
ggplot
调用。如果我们采用您的数据框的可复制版本:
df <- data.frame(Descriptor = c("Not painful", "Not painful", "Not painful",
"Painful", "Painful", "Painful"),
Severity = c("Clear/mild", "Moderate", "Severe", "Clear/mild",
"Moderate", "Severe"),
Value = c(92.6, 77.9, 54.1, 7.4, 22.1, 45.9),
Lower_CI = c(86.2, 68.7, 45.1, 3.9, 15.1, 37.2),
Upper_CI = c(96.1, 84.9, 62.8, 13.8, 31.3, 54.9))
那么代码会是这样的:
library(ggplot2)
ggplot(df, aes(Severity, Value)) +
geom_col(aes(fill = Descriptor), position = "dodge", color = "black") +
geom_errorbar(aes(ymin = Lower_CI, ymax = Upper_CI, group = Descriptor),
position = position_dodge(0.9), width = 0.2) +
scale_fill_manual(values = c("gray90", "gray50")) +
theme_minimal(base_size = 20) +
labs(x = NULL, y = "Percentage")