R森林图的对数轴正负

时间:2018-08-10 14:42:05

标签: r ggplot2 forestplot

我使用R(ggplot2)的森林图有问题。 由于我的取值范围和置信区间范围很大,因此我想对显示的x轴使用对数标度,以表示等距间隔的正值和负值,以便仍然可以检测到接近参考线的效果。 此处已完成:https://stackoverflow.com/a/31149324/10203323用于直方图。但是,我只能更改刻度,而不能更改刻度本身。你能帮我吗?

感谢一百万!!!

我的数据如下:

structure(list(Effect = c(1L,1L,1L,1L,2L,2L,2L,2L),y = c(0.0727,-0.1033,0.3743,-0.585,0.1153,0.01,0.1334,0.1111) ,LowerLimit = c(-0.0372,-0.9189,-0.4287,-2.6247,-0.1496,-0.01,-0.0374,-0.0027),UpperLimit = c(0.1826、0.7124、1.1773、1.4547、0.3803、0.03、0.3042、0.2249) ,x =结构(c(1L,2L,3L,4L,1L,2L,3L,4L),.Label = c(“ A”,“ B”,“ C”,“ D”),class =“ factor “)),.Names = c(” Effect“,” y“,” LowerLimit“,” UpperLimit“,” x“),row.names = c(NA,8L),class =” data.frame“)< / p>

我尝试了以下代码:

 trans_new("custom_log_y",
      transform = function (x) ( sign(x)*log(abs(x)+1) ),
      inverse = function (y) ( sign(y)*( exp(abs(y))-1) ),
      domain = c(-Inf,Inf))

# Custom log y breaker (...,-10,-3,-1,0,1,3,10,...)
custom_y_breaks <- function(x)
 { 
  range <- max(abs(x), na.rm=TRUE)

  return (sort( c(0,
              sapply(0:log10(range), function(z) (1^z) ),
              sapply(0:log10(range/3), function(z) (3*1^z) ),
              sapply(0:log10(range), function(z) (-1^z) ),
              sapply(0:log10(range/3), function(z) (-3*1^z) )
  )))
}


p2 = ggplot(data=RR_data,
        aes(x = x,y = y, ymin = LowerLimit, ymax = UpperLimit))+
  geom_pointrange()+
  geom_hline(aes(fill=y),yintercept =0, linetype=2) +
  coord_trans(y = custom_log_y_trans) + 
  scale_y_continuous(breaks = custom_y_breaks) +
  xlab('Group')+ ylab("LnRR (95% Confidence Interval)")+
  geom_errorbar(aes(ymin=LowerLimit, ymax=UpperLimit),width=0.3,cex=1)+ 
  facet_wrap(~Effect,strip.position="left",nrow=3,scales = "free_y") +
  coord_flip()+theme_bw()+theme(legend.position = "none")+geom_vline(xintercept=4.7)
p2

0 个答案:

没有答案