【问题标题】:R: Arranging variables in forest plotR:在森林图中排列变量
【发布时间】:2016-08-17 02:22:39
【问题描述】:

我使用“Pimping your forest plot”制作了一个森林地块代码

我想比较变量子组之间的男性和女性差异。

更具体地说,如果

变量1:年龄/亚组:30-39 岁、40-49 岁、50-59 岁

变量 2:教育/子组:

我想像这样比较变量之间的性别差异,(我想要这种方式的森林图变量数组)

**变量1

亚组(男性)

30-39 岁

40-49 岁

50-59 岁

亚组(女性)

30-39 岁

40-49 岁

50-59 岁

变量2

亚组(男性)

9-12 岁

亚组(女性)

9-12 岁

**

我应该如何编写这种森林图?

现在我已经按照这个可变顺序制作了一个森林图,

变量1 30-39岁(男性)

变量1 30-39岁(女性)

变量1 40-49岁(男性)

变量1 40-49岁(女性)

变量1 50-59岁(男性)

变量1 50-59岁(女性)

变量2

变量2

变量2 9-12岁(男性)

变量2 9-12岁(女性)

我现在的代码是这样的,

Male<-structure(c(0.22,0.54,2.09,2.65,1.04,1.16,1.15,0.78,1.06,0.99,1.06,0.97,1.00,1.80,0.81), .Dim=c(5L,3L),.Dimnames=list(c("age,30-39years","age,40-49years","age,50-59years" "education,<9 years","education, 9-12 years" ),c("OR","L","U")))

Female<-structure(c(0.89,1.47,1.08,1.32,1.41,1.38,1.00,27.01,5.15,0.88,0.28,0.96,0.49,0.36,1.61),.Dim=c(17L,3L),.Dimnames=list(c("age, 30-39years","age,40-49years","age,50-59years","education,<9 years","education,   9-12 years"),c("OR","L","U")))

library(Gmisc)

forestplot2(mean=cbind(log(Male[,"OR"]),log(Female[,"OR"])), lower=cbind(log(Male[,"L"]),log(Female[,"L"])),upper=cbind(log(Male[,"U"]),log(Female[,"U"])),labeltext=rownames(Male),legend=c("Male", "Female"),clip=c(-2.5,5.0), boxsize=0.1,col=fpColors(box=c("blue", "darkred")),fn.ci_norm=c("fpDrawNormalCI", "fpDrawCircleCI"),xlab="Hypertension prevalence among sex difference",new_page=TRUE)

请帮帮我~谢谢

【问题讨论】:

  • 请在每行代码前添加 4 个空格。
  • 谢谢 :)!!!!!!!!!

标签: r random-forest


【解决方案1】:

请确保您的示例数据在发布之前是干净且有效的。这是我认为可以解决您的问题的解决方案:

Male<-structure(c(0.22,0.54,2.09,2.65,1.04,1.16,1.15,0.78,1.06,0.99,1.06,0.97,1.00,1.80,0.81), 
                .Dim=c(5L,3L),
                .Dimnames=list(c("age,30-39years","age,40-49years","age,50-59years","education,<9 years","education, 9-12 years"),
                               c("OR","L","U")))

Female<-structure(c(0.89,1.47,1.08,1.32,1.41,1.38,1.00,27.01,5.15,0.88,0.28,0.96,0.49,0.36,1.61),
                  .Dim=c(5L,3L),
                  .Dimnames=list(c("age, 30-39years","age,40-49years","age,50-59years","education,<9 years","education,   9-12 years"),c
                                 ("OR","L","U")))

# Messy input data - something wrong
for (i in 1:nrow(Male)) {
  tmp <- Male[i,]
  low <- which.min(tmp)[1]
  high <- which.max(tmp)[1]
  Male[i,] <- c(Male[i,c(-low, -high)],
                Male[i,low],
                Male[i,high])

  tmp <- Female[i,]
  low <- which.min(tmp)[1]
  high <- which.max(tmp)[1]
  Female[i,] <- c(Female[i,c(-low, -high)],
                Female[i,low],
                Female[i,high])
}

library(forestplot)
library(abind)
Male <- Gmisc::insertRowAndKeepAttr(Male, 
                                    grep("education", rownames(Male))[1], 
                                    rName = "Education")
Female <- Gmisc::insertRowAndKeepAttr(Female, 
                                      grep("education", rownames(Female))[1], 
                                      rName = "Education")
Male <- Gmisc::insertRowAndKeepAttr(Male, 1, rName = "Age")
Female <- Gmisc::insertRowAndKeepAttr(Female, 1, rName = "Age")


out = abind(Male, Female, along = 3)
rownames(out) <- gsub("(age|education),[ ]*", "  ", rownames(out))
forestplot(out,
           xlog = TRUE,
           legend=c("Male", "Female"),
           clip=exp(c(-2.5,5.0)), 
           col=fpColors(box=c("blue", "darkred")),
           fn.ci_norm=c("fpDrawNormalCI", "fpDrawCircleCI"),
           xlab="Hypertension prevalence among sex difference",
           new_page=TRUE)

给出这个:

请注意,为简单起见,我为其提供了一个 3 维数组。不幸的是,包中存在错误,您需要下载包的开发版本(>1.5)。

【讨论】:

  • 我发布了另一个关于三次样条的问题,你能帮我解决这个问题吗?谢谢。
  • @gkduchl - 如果您对答案感到满意,请将其标记为答案。我会看看我是否有时间解释 rcs 是如何工作的 - 如果你有一个我可以重现的例子会更容易
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2018-12-22
  • 2017-08-29
  • 1970-01-01
相关资源
最近更新 更多