figure.r 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # 基线表
  2. # install.packages("compareGroups", repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  3. # install.packages("rmarkdown", repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
  4. library(compareGroups)
  5. library(dplyr)
  6. data <- read.csv("paper_data.csv")
  7. #性别
  8. data$Rgender <- factor(data$rgender, levels = c(1, 2), labels = c("Male", "Female"))
  9. #年齡
  10. data$Age <- cut(data$age,
  11. breaks = c(45, 55, 65, Inf),
  12. labels = c("45-54", "55-64",">=65"),
  13. right = FALSE)
  14. #婚姻
  15. data$Marital_Status <- factor(data$marital_status, levels = c(1, 0), labels = c("married or partnered", "other marital status"))
  16. #教育
  17. data$Education <- factor(data$education, levels = c(0, 1, 2), labels = c("below high school", "high school", "college or above"))
  18. #运动情况
  19. data$Physical_Activity <- factor(data$Physical_activity, levels = c(0, 1, 2), labels = c("inactive", "moderate", "vigorous"))
  20. #心理得分
  21. data$Depressive_Symptoms <- cut(data$Psychiatric_score, breaks = c(0, 10, Inf),labels = c("No", "Yes"), right = FALSE)
  22. #BMI
  23. # 使用 cut() 创建因子类型的变量
  24. data$BMI <- cut(data$BMI, breaks = c(0, 18.5, 24, Inf), labels = c("underweight", "normal", "overweight"), right = FALSE)
  25. # 使用 dplyr::recode 直接替换因子水平
  26. data$BMI <- recode(data$BMI, "underweight" = 1L, "normal" = 0L, "overweight" = 2L)
  27. # 将 data$variable 转换回因子并保留原 levels
  28. data$BMI <- factor(data$BMI, levels = c(0, 1, 2), labels = c("normal", "underweight", "overweight"))
  29. #ADL
  30. # 使用 cut() 创建因子类型的变量
  31. data$ADL <- cut(data$ADL, breaks = c(0, 1, 3, Inf), labels = c("No impairment", "Mild impairment", "Severe impairment"), right = FALSE)
  32. #抽烟
  33. data$Smoke <- factor(data$Smoke, levels = c(0, 1), labels = c("No", "Yes"))
  34. #喝酒
  35. data$Drink <- factor(data$Drink, levels = c(0, 1), labels = c("No", "Yes"))
  36. #结局
  37. data$State <- factor(data$state, levels = c(1, 2, 3, 4), labels = c("No chronic disease", "One chronic disease", "Comorbidity", "Death"))
  38. # 定义分层变量和汇总变量
  39. vars <- c("State", "Age", "Rgender", "Marital_Status", "Education", "BMI","ADL","Smoke","Drink","Physical_Activity", "Depressive_Symptoms", "last_year_NO2", "last_year_O3")
  40. formula = State ~ Age+Rgender+Marital_Status+Education+BMI+ADL+Smoke+Drink+Physical_Activity+Depressive_Symptoms+last_year_NO2+last_year_O3
  41. # 创建基线表
  42. table = descrTable(formula = formula, data = data, show.p.overall = FALSE)
  43. export2word(table, file = "基线表.docx")
  44. # 森林图
  45. library(dplyr)
  46. library(ggforce)
  47. library(ggforestplot)
  48. df_compare_traits_groups <- read.csv("output_new.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
  49. df_logodds <-
  50. df_compare_traits_groups %>%
  51. dplyr::arrange(num) %>%
  52. # Set the study variable to a factor to preserve order of appearance
  53. # Set class to factor to set order of display.
  54. dplyr::mutate(
  55. trait = factor(
  56. trait,
  57. levels = c("No chronic disease to One chronic disease", "No chronic disease to Comorbidity",
  58. "One chronic disease to Comorbidity", "One chronic disease to No chronic disease", "Comorbidity to One chronic disease",
  59. "Comorbidity to No chronic disease", "No chronic disease to Death", "One chronic disease to Death", "Comorbidity to Death")
  60. )
  61. )
  62. # Draw a forestplot of cross-sectional, linear associations.
  63. p=forestplot(
  64. df = df_logodds,
  65. estimate = beta,
  66. # pvalue = pvalue,
  67. # psignif = 0,
  68. logodds = TRUE,
  69. title = "The impact results of covariates",
  70. xlab = "Effect Sizie with 95% CI",
  71. colour = trait,
  72. shape=trait,
  73. xlim = c(0.1, 6),
  74. # You can explicitly define x-tick breaks
  75. xtickbreaks = c(0.1, 0.5, 0.8, 1.0,1.2, 1.5, 2.0, 3.0,4.0,5.0)
  76. ) +
  77. ggplot2::scale_shape_manual(
  78. values = c(6L, 6L, 6L, 2L, 2L,2L,21L,21L,21L),
  79. labels = c("No chronic disease to One chronic disease", "No chronic disease to Comorbidity",
  80. "One chronic disease to Comorbidity", "One chronic disease to No chronic disease", "Comorbidity to One chronic disease",
  81. "Comorbidity to No chronic disease", "No chronic disease to Death", "One chronic disease to Death", "Comorbidity to Death")
  82. )+
  83. # 修改字体大小
  84. ggplot2::theme(
  85. plot.title = ggplot2::element_text(size = 32, face = "bold"), # 标题字体
  86. axis.title = ggplot2::element_text(size = 28), # 轴标题字体
  87. axis.text = ggplot2::element_text(size = 28), # 轴刻度字体
  88. strip.text = ggplot2::element_text(size = 30), # facet标签字体
  89. legend.text = ggplot2::element_text(size = 24), # 图例字体
  90. legend.title = element_blank(), # 移除图例标题
  91. legend.position = "top", # 默认位置,右边
  92. )+
  93. # 使用 facet_wrap 进行分组显示,按 trait 进行分组
  94. ggplot2::facet_wrap(~group, scales = "free_y", ncol = 1)
  95. ggsave("forestplot_output.png", plot = p, width = 35, height = 30, dpi = 300, bg = "white")
  96. # library(ggplot2)
  97. # library(dplyr)
  98. # Assuming df_logodds is your data
  99. # df_logodds <- df_compare_traits_groups %>%
  100. # dplyr::arrange(num) %>%
  101. # dplyr::mutate(
  102. # trait = factor(
  103. # trait,
  104. # levels = c("No chronic disease to One chronic disease", "No chronic disease to Comorbidity",
  105. # "One chronic disease to Comorbidity", "One chronic disease to No chronic disease",
  106. # "Comorbidity to One chronic disease", "Comorbidity to No chronic disease",
  107. # "No chronic disease to Death", "One chronic disease to Death", "Comorbidity to Death")
  108. # )
  109. # )
  110. # # Draw the forestplot using ggplot2
  111. # p <- ggplot(df_logodds, aes(x = beta, y = trait, color = trait, shape = trait)) +
  112. # # Add points (the estimates)
  113. # geom_point(size = 5) +
  114. # # Add error bars (95% CI)
  115. # geom_errorbarh(aes(xmin = lower, xmax = upper), height = 0.2) +
  116. # # Customizing the shape of points
  117. # scale_shape_manual(
  118. # values = c(6, 6, 6, 2, 2, 2, 21, 21, 21),
  119. # labels = c("No chronic disease to One chronic disease", "No chronic disease to Comorbidity",
  120. # "One chronic disease to Comorbidity", "One chronic disease to No chronic disease",
  121. # "Comorbidity to One chronic disease", "Comorbidity to No chronic disease",
  122. # "No chronic disease to Death", "One chronic disease to Death", "Comorbidity to Death")
  123. # ) +
  124. # # Set the color palette for different traits
  125. # scale_color_manual(values = c("red", "blue", "green", "purple", "orange", "brown", "pink", "yellow", "gray")) +
  126. # # Add title and axis labels
  127. # labs(
  128. # title = "The impact results of covariates",
  129. # x = "Effect Size with 95% CI",
  130. # y = NULL # No y-axis title, it's already covered by trait names
  131. # ) +
  132. # # Adjust the x-axis limits to control the effect size range
  133. # xlim(0.1, 6) +
  134. # # Customize text size and legend position
  135. # theme(
  136. # plot.title = element_text(size = 32, face = "bold"), # Title font size and style
  137. # axis.title = element_text(size = 28), # Axis title font size
  138. # axis.text = element_text(size = 28), # Axis ticks font size
  139. # strip.text = element_text(size = 30), # Facet label font size (if any)
  140. # legend.text = element_text(size = 24), # Legend font size
  141. # legend.title = element_blank(), # Remove legend title
  142. # legend.position = "top", # Position of the legend
  143. # legend.key.size = unit(1.5, "lines"), # Adjust the size of the legend keys (points)
  144. # panel.grid.major = element_line(size = 0.5, linetype = "dashed", color = "gray90"),
  145. # panel.grid.minor = element_blank(), # Remove minor gridlines
  146. # panel.background = element_blank(), # White background for panel
  147. # plot.background = element_blank() # White background for plot
  148. # ) +
  149. # # Customize x-axis breaks
  150. # scale_x_continuous(
  151. # breaks = c(0.1, 0.5, 0.8, 1.0, 1.2, 1.5, 2.0, 3.0, 4.0, 5.0)
  152. # )
  153. # # Print the plot
  154. # print(p)