如何使用自定义值绘制 geom_errorbar(R 或森林中的荟萃分析?)
How to plot geom_errorbar with custom values (meta-analysis in R or maybe forest?)
任何人都可以帮助我在 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
我想可视化各国在性别(filled/coloured 性别)方面的相关性差异。我有一个平均值(相关性)、该平均值的较低置信区间(较低)和较高(较高)。左边应该有国家和...基本上就是这样。不知何故我无法理解它。
在通过 Whosebug 搜索时,我想知道是否应该使用一些森林函数,因为它可能更接近我的想象。
到目前为止我所做的看起来很糟糕:
link
提前致谢!
使用 ggplot2
包中的 facet 和 facet_wrap()
尝试这种方法。这里的代码:
library(ggplot2)
library(dplyr)
#Code
df %>%
ggplot(aes(x=Country,y=Correlation,color=Sex))+
geom_point()+
geom_errorbar(aes(ymin=Lower,ymax=Upper))+
facet_wrap(.~Sex)+
theme_bw()+
theme(legend.position = 'top',
axis.text = element_text(color='black',face='bold'),
axis.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
strip.text = element_text(color='black',face='bold'),
strip.background = element_blank())
输出:
使用了一些数据:
#Data
df <- structure(list(Country = c("Brazil", "Bulgaria", "Canada", "Brazil",
"Bulgaria", "Canada"), Sex = c("Men", "Men", "Men", "Women",
"Women", "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"))
听起来您正在寻找这样的东西:
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, 2L, 3L),
.Label = c("Brazil", "Bulgaria", "Canada"), class = "factor"),
Sex = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Men", "Women"),
class = "factor"),
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"))
任何人都可以帮助我在 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
我想可视化各国在性别(filled/coloured 性别)方面的相关性差异。我有一个平均值(相关性)、该平均值的较低置信区间(较低)和较高(较高)。左边应该有国家和...基本上就是这样。不知何故我无法理解它。
在通过 Whosebug 搜索时,我想知道是否应该使用一些森林函数,因为它可能更接近我的想象。
到目前为止我所做的看起来很糟糕: link
提前致谢!
使用 ggplot2
包中的 facet 和 facet_wrap()
尝试这种方法。这里的代码:
library(ggplot2)
library(dplyr)
#Code
df %>%
ggplot(aes(x=Country,y=Correlation,color=Sex))+
geom_point()+
geom_errorbar(aes(ymin=Lower,ymax=Upper))+
facet_wrap(.~Sex)+
theme_bw()+
theme(legend.position = 'top',
axis.text = element_text(color='black',face='bold'),
axis.title = element_text(color='black',face='bold'),
legend.text = element_text(color='black',face='bold'),
legend.title = element_text(color='black',face='bold'),
strip.text = element_text(color='black',face='bold'),
strip.background = element_blank())
输出:
使用了一些数据:
#Data
df <- structure(list(Country = c("Brazil", "Bulgaria", "Canada", "Brazil",
"Bulgaria", "Canada"), Sex = c("Men", "Men", "Men", "Women",
"Women", "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"))
听起来您正在寻找这样的东西:
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, 2L, 3L),
.Label = c("Brazil", "Bulgaria", "Canada"), class = "factor"),
Sex = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Men", "Women"),
class = "factor"),
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"))