置信区间上的 barplot2 错误?
barplot2 error on Confidence intervals?
所以,我有这个 table
Ccela Cviri Dfrag Pfict
S.obs 1759.00000 634.00000 747.00000 1714.00000
S.chao1 2610.80685 1374.29167 1192.63830 2192.72862
se.chao1 83.85972 122.35336 63.93822 55.99704
我希望看到由 barplot2 或任何其他绘制的,其置信区间对应于 se.chao1
行以及 S.obs
和S.chao1
为每一列绘制为一组。
为此,我尝试使用 barplot2()
,为此我将最后一行提取到两个文件中(ci.u
和 ci.l
)并将其从之前的文件中删除table。这样,在向 ci.l
和 ci.u
.
添加一个空行后,我得到了 3 个具有相同尺寸的 tables
运行 以下内容:
barplot2(new_barplot, legend = row.names(barplot_est.plot), beside=TRUE, plot.ci = TRUE, ci.l = ci.l[1,], ci.u = ci.u[1,], col=c("lightblue","lightcyan"))
我收到以下错误
Error in barplot2.default(new_barplot, legend =
row.names(barplot_est.plot), : ‘height’ and ‘ci.u’ must have the
same dimensions.
是否有另一种使用 barplot2 的方法?
如果没有,你能告诉我用 ggplots2 做这件事的方法吗?
提前致谢!
安德烈
看到 Max Ghenis 建议的 link,这可能是一种方法。我先重新整理了你的数据。然后,我创建了 limits
,您在绘制误差线时需要它。
library(dplyr)
library(tidyr)
library(ggplot2)
data.frame(t(mydf)) %>%
add_rownames() %>%
gather(group, value, - c(rowname, se.chao1)) -> mydf2
limits <- aes(ymax = value + se.chao1, ymin = value - se.chao1)
g <- ggplot(data = mydf2, aes(x = rowname, y = value, fill = group)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(limits, position = dodge, width = 0.25) +
labs(x = "Assign your name", y = "Value")
ggsave(g, file = "april12.png")
数据
mydf <- structure(list(Ccela = c(1759, 2610.80685, 83.85972), Cviri = c(634,
1374.29167, 122.35336), Dfrag = c(747, 1192.6383, 63.93822),
Pfict = c(1714, 2192.72862, 55.99704)), .Names = c("Ccela",
"Cviri", "Dfrag", "Pfict"), class = "data.frame", row.names = c("S.obs",
"S.chao1", "se.chao1"))
编辑
看到 OP 的评论,我做了以下操作。在此版本中,您只会看到 S.chao1
.
的错误栏
mutate(mydf2, se.chao1 = replace(se.chao1, which(group == "S.obs"), NA)) -> mydf3
dodge <- position_dodge(width=0.9)
ggplot(data = mydf2, aes(x = rowname, y = value, fill = group)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(data = mydf3, aes(ymax = value + se.chao1, ymin = value - se.chao1),
position = dodge, width = 0.25) +
labs(x = "Assign your name", y = "Value")
所以,我有这个 table
Ccela Cviri Dfrag Pfict
S.obs 1759.00000 634.00000 747.00000 1714.00000
S.chao1 2610.80685 1374.29167 1192.63830 2192.72862
se.chao1 83.85972 122.35336 63.93822 55.99704
我希望看到由 barplot2 或任何其他绘制的,其置信区间对应于 se.chao1
行以及 S.obs
和S.chao1
为每一列绘制为一组。
为此,我尝试使用 barplot2()
,为此我将最后一行提取到两个文件中(ci.u
和 ci.l
)并将其从之前的文件中删除table。这样,在向 ci.l
和 ci.u
.
运行 以下内容:
barplot2(new_barplot, legend = row.names(barplot_est.plot), beside=TRUE, plot.ci = TRUE, ci.l = ci.l[1,], ci.u = ci.u[1,], col=c("lightblue","lightcyan"))
我收到以下错误
Error in barplot2.default(new_barplot, legend = row.names(barplot_est.plot), : ‘height’ and ‘ci.u’ must have the same dimensions.
是否有另一种使用 barplot2 的方法?
如果没有,你能告诉我用 ggplots2 做这件事的方法吗?
提前致谢!
安德烈
看到 Max Ghenis 建议的 link,这可能是一种方法。我先重新整理了你的数据。然后,我创建了 limits
,您在绘制误差线时需要它。
library(dplyr)
library(tidyr)
library(ggplot2)
data.frame(t(mydf)) %>%
add_rownames() %>%
gather(group, value, - c(rowname, se.chao1)) -> mydf2
limits <- aes(ymax = value + se.chao1, ymin = value - se.chao1)
g <- ggplot(data = mydf2, aes(x = rowname, y = value, fill = group)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(limits, position = dodge, width = 0.25) +
labs(x = "Assign your name", y = "Value")
ggsave(g, file = "april12.png")
数据
mydf <- structure(list(Ccela = c(1759, 2610.80685, 83.85972), Cviri = c(634,
1374.29167, 122.35336), Dfrag = c(747, 1192.6383, 63.93822),
Pfict = c(1714, 2192.72862, 55.99704)), .Names = c("Ccela",
"Cviri", "Dfrag", "Pfict"), class = "data.frame", row.names = c("S.obs",
"S.chao1", "se.chao1"))
编辑
看到 OP 的评论,我做了以下操作。在此版本中,您只会看到 S.chao1
.
mutate(mydf2, se.chao1 = replace(se.chao1, which(group == "S.obs"), NA)) -> mydf3
dodge <- position_dodge(width=0.9)
ggplot(data = mydf2, aes(x = rowname, y = value, fill = group)) +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(data = mydf3, aes(ymax = value + se.chao1, ymin = value - se.chao1),
position = dodge, width = 0.25) +
labs(x = "Assign your name", y = "Value")