我正在绘制R中的两个条形图,其中我对其中一个条具有置信区间,并且遇到了一个问题,即position =“ dodge”或position = position_dodge()根本不会移动这些条。我查看了关于stackoverflow的类似问题,但似乎不多。
我的代码是:
ggplot(data = Estimates.df, aes(x = Assets, y = Estimate, group = Estimate)) +
geom_bar(data = Estimates.df, aes(x = Assets, y = Estimate, group = Estimate, fill = Estimate), stat = "identity", fill = "Blue", position = "dodge") +
geom_bar(data = Estimates.df, aes(x = Assets, y = Target, group = Target, fill = Target), stat = "identity", fill = "Black", position = "dodge") +
coord_flip() +
geom_errorbar(aes(ymin = (Estimate - 1.645*Std..Error), ymax = (Estimate + 1.645*Std..Error), group = Estimate), position = "dodge")
我需要估计图上的置信区间,而不是目标图上的置信区间。我尝试通过融化将数据帧转换为long,但是无法以这种方式保持置信区间。
谁能看到我哪里出了问题?
我的数据是:
Assets <- c("TW", "Asset1", "Asset2", "Asset3", "Asset4", "Asset5", "Asset6", "Asset7", "Asset8", "Asset9", "Asset10", "Asset11", "Asset12", "Asset13", "Asset14", "Asset15", "Asset16")
Std..Error <- c(.0002,.050,.0147,.028,.0289,.035,.0184,.0709,.0130,.0356,.0071,.0067,.0080,.0162,.0188,.0099,.0147)
Estimate <- c(.002,-0.032,.0317,.018,.0336,.0426,.09774,.3191,.0067,.0892,.0225,-0.0145,.0263,.01125,.07359,.09055,.1843)
Target <- c(0,.05,0,.04,0,0,.07,.23,0,.18,.05,0,.02,0,.09,.05,.22)
Estimates.df <- data.frame(cbind(Assets, Std..Error, Estimate, Target))
答案 0 :(得分:2)
您的代码有几个错误,您需要ggplot2
的长格式数据(不是宽格式),因此我将tidyr
和dplyr
的数据转换为长格式。然后,您不应在ggplot的每个参数中重复美学。最后,您需要确保值的类型为numeric
,因为它们的类型为factor
。
Estimates.df <- data.frame(cbind(Assets, Std..Error, Estimate, Target))
library(tidyr)
library(dplyr)
Estimates.df<-Estimates.df %>%
gather(type,value,Estimate:Target)
ggplot(data = Estimates.df, aes(x = Assets, y = value, fill=type, color=type)) +
geom_bar(stat = "identity",
position = "dodge")+
coord_flip() +
geom_errorbar(
aes(ymin = (as.numeric(value) - 1.645*as.numeric(Std..Error)),
ymax = (as.numeric(value) + 1.645*as.numeric(Std..Error))),
position = "dodge")
答案 1 :(得分:2)
您将需要使用长数据。我使用melt来做到这一点,并在绘制之前弄乱了结果,这样就不会像您所说的那样获得Target的误差线。我也更喜欢在之前创建ymin和ymax。 以下是我认为您要求的颜色以外的颜色。
library(reshape2)
Estimates.df <- data.frame("Assets" = c("TW", "Asset1", "Asset2", "Asset3", "Asset4", "Asset5", "Asset6", "Asset7", "Asset8", "Asset9", "Asset10", "Asset11", "Asset12", "Asset13", "Asset14", "Asset15", "Asset16"),
"Std..Error" = c(.0002,.050,.0147,.028,.0289,.035,.0184,.0709,.0130,.0356,.0071,.0067,.0080,.0162,.0188,.0099,.0147),
"Estimate" = c(.002,-0.032,.0317,.018,.0336,.0426,.09774,.3191,.0067,.0892,.0225,-0.0145,.0263,.01125,.07359,.09055,.1843),
"Target" = c(0,.05,0,.04,0,0,.07,.23,0,.18,.05,0,.02,0,.09,.05,.22))
Estimates.df.p <- Estimates.df %>%
mutate(ymin = Estimate - 1.645*Std..Error, ymax = Estimate + 1.645*Std..Error) %>%
melt(id.vars = c("Assets", "ymin", "ymax", "Std..Error"), value.name = "value", variable.name = "variable") %>%
mutate(ymin = ifelse(variable == "Target", NA, ymin),
ymax = ifelse(variable == "Target", NA, ymax))
ggplot(data = Estimates.df.p, aes(x = Assets, y = value, group = variable)) +
geom_bar(aes(fill = variable, colour = variable), stat = "identity", position = "dodge") +
coord_flip() +
geom_errorbar(aes(ymin = ymin, ymax = ymax, group = variable), position = "dodge")