Hi各位,我又遇到过不去的坎儿了。
我想用ggplot做一个森林图。数据大概是这个样子的:
Estimate Std.Error df t.value p.value low high color var
-5.84226363 1.2759894 32.177022 -4.57861468 6.66E-05 -8.34320279 -3.34132448 red (Intercept)
-0.73321958 0.6201841 57.046193 -1.18226126 2.42E-01 -1.94878032 0.48234116 red ACTA2
3.07177365 0.7236063 28.381744 4.24508973 2.12E-04 1.65350531 4.49004199 blue AEN
-0.02666508 0.7201801 58.179861 -0.03702557 9.71E-01 -1.43821804 1.38488789 red CD14
-0.98295989 0.4899248 56.482464 -2.0063486 4.96E-02 -1.94321245 -0.02270733 red CD4
-0.26392348 0.6771824 58.910578 -0.38973764 6.98E-01 -1.59120107 1.0633541 red DDB2
-2.00887867 0.8623534 60.39924 -2.32953072 2.32E-02 -3.69909128 -0.31866607 red PHLDA3
2.38785539 0.610361 7.538894 3.91220166 5.02E-03 1.19154779 3.58416298 blue PHPT1
0.47901028 0.3224425 60.363546 1.48556796 1.43E-01 -0.15297706 1.11099762 blue PLK2
其中var那一列设置成了因子,是按照字母顺序排列的,然后我又特意把整个数据框按照var这一列拍了个序。
但是出来的图,纵轴的顺序和数据框中正好是反着的。
我画图用的代码是这样的:
p <-
ggplot(data, aes(Estimate, var, col=color)) +
geom_point() +
geom_errorbarh(aes(xmax = high, xmin = low), height = 0.2) +
scale_x_continuous(limits= c(min(data$low)-ifelse(min(data$low)<(-15),20,5),
max(data$high)+ifelse(max(data$high)>15,20,5))) +
geom_vline(aes(xintercept = 0),colour = 'black', linetype = 'dotdash',alpha = 0.6) +
geom_text(aes(x=max(high)+0.2, y=var, label=paste0('P = ',format(p.value, scientific = T, digits = 3))),hjust=0) +
geom_text(aes(x=min(low)-0.2, y=var, label=paste0(round(Estimate, 2), ' ± ', round(1.96*Std.Error, 2))), hjust=1) +
xlab('Estimate') + ylab('Dependent variable') +
theme(legend.position = "none")
我试过把数据库按照var反向排序,或者在绘图时把var换成rev(var),但是图的顺序始终保持不变。请大家帮帮忙,看看到底是咋回事?