问题描述
当我的数据看起来像时,有人可以帮我在R中绘制geom_errorbar吗?
> Country Sex Correlation Number Lower Upper
1 Brazil Men -0.108 301 -0.218 0.005
2 Bulgaria Men -0.012 63 -0.258 0.236
3 Canada Men 0.07 25 -0.334 0.452
4 Brazil Women -0.074 47 -0.353 0.217
5 Bulgaria Women -0.042 300 -0.154 0.071
6 Canada Women 0.092 51 -0.188 0.358
我想形象化地说明各国之间的性别差异(实心/有色性别)。我有一个平均值(相关性),该平均值的较低置信区间(较低)和较高(较高)。左边应该有国家,基本上就是这样。我莫名其妙地无法达到目的。
搜索Stackoverflow时,我想知道是否应该使用一些林函数,因为它可能更接近我的想象。
到目前为止,我设法做的看起来很糟糕: link
谢谢!
解决方法
使用struct A { int a; };
A initA(int a) { return A{a}; }
bool somePredicate(int input) { return input == 42; }
int main() {
const auto input = 42;
const auto a = somePredicate(input) ? initA(1) : initA(2);
}
包中带有struct A { int a; };
A initA(int a) { return A{a}; }
bool somePredicate(int input) { return input == 42; }
int main() {
const auto input = 42;
const auto a = []() {
if (somePredicate(input)) { return initA(1); }
else if (input == 100) { return initA(100); }
else { return initA(2); }}();
}
的构面来尝试这种方法。这里的代码:
const
输出:
使用了一些数据:
ggplot2
,
听起来您正在寻找这样的东西:
ggplot(df,aes(x = Correlation,y = Country,color = Sex)) +
geom_point(position = position_dodge(width = 0.75)) +
geom_errorbarh(aes(xmin = Lower,xmax = Upper),position = position_dodge(width = 0.75))
数据
df <- structure(list(Country = structure(c(1L,2L,3L,1L,3L),.Label = c("Brazil","Bulgaria","Canada"),class = "factor"),Sex = structure(c(1L,2L),.Label = c("Men","Women"),Correlation = c(-0.108,-0.012,0.07,-0.074,-0.042,0.092
),Number = c(301L,63L,25L,47L,300L,51L),Lower = c(-0.218,-0.258,-0.334,-0.353,-0.154,-0.188),Upper = c(0.005,0.236,0.452,0.217,0.071,0.358)),class = "data.frame",row.names = c("1","2","3","4","5","6"))