如何使用R中的forestplot包在森林图中的x轴上显示小数点?

问题描述 投票:0回答:1

我正在使用 R 中的

forestplot
包来创建森林图,并且我尝试在 x 轴刻度标签上显示小数点(例如,
1.0
代替
1
2.0
代替
 2
)。然而,尽管在
xticks
参数中指定了小数点,绘图仍然对这些值进行四舍五入,并且仅显示整数(
1
2
等)。

这是我的代码:

adjusted_rr <- c(
  NA, 1.00, 0.77, 1.46, 0.79, 0.92, NA,
  NA, 1.00, 0.69, 0.67, 0.94, NA,
  NA, 1.00, 1.21, 0.98, NA,
  NA, 1.17, 1.21, 1.09, 1.23, 1.21, 1.00, NA,
  NA, 1.00, 1.24, 1.22, 1.11, 1.28, 1.24, 0.97, 1.03, NA,
  NA, 1.20, 1.15, 1.00, NA,
  NA, 1.10, 1.14, 1.09, 1.02, 1.00, NA,
  NA, 1.00, 0.86, NA,
  NA, 1.00, 0.88, 0.87, 0.84, 0.97, NA
)

lower_ci <- c(
  NA, 1.00, 0.70, 1.38, 0.72, 0.78, NA,
  NA, 1.00, 0.61, 0.61, 0.88, NA,
  NA, 1.00, 1.15, 0.85, NA,
  NA, 1.09, 1.07, 0.96, 1.07, 1.10, 1.00, NA,
  NA, 1.00, 1.01, 1.12, 0.94, 1.12, 1.13, 0.78, 0.91, NA,
  NA, 1.13, 1.05, 1.00, NA,
  NA, 1.01, 1.06, 1.00, 0.94, 1.00, NA,
  NA, 1.00, 0.78, NA,
  NA, 1.00, 0.81, 0.80, 0.77, 0.90, NA
)

upper_ci <- c(
  NA, 1.00, 0.85, 1.55, 0.87, 1.08, NA,
  NA, 1.00, 0.78, 0.73, 0.99, NA,
  NA, 1.00, 1.29, 1.12, NA,
  NA, 1.27, 1.38, 1.25, 1.41, 1.34, 1.00, NA,
  NA, 1.00, 1.53, 1.34, 1.32, 1.46, 1.35, 1.21, 1.16, NA,
  NA, 1.27, 1.26, 1.00, NA,
  NA, 1.19, 1.24, 1.18, 1.11, 1.00, NA,
  NA, 1.00, 0.95, NA,
  NA, 1.00, 0.95, 0.94, 0.91, 1.04, NA
)

# Forest plot code
forestplot(labeltext = labels,
           mean = adjusted_rr, 
           lower = lower_ci, 
           upper = upper_ci,
           zero = 1.0,  # Center the plot at 1.0
           xlog = FALSE,
           xlab = "Adjusted Rate Ratio",
           boxsize = 0.2,
           is.summary = is.na(adjusted_rr),
           xticks = c(0.5, 1.0, 1.5, 2.0),  # Specify tick marks
           line.margin = 0.4,  # Compress horizontally
           graph.pos = 2, # Adjust vertical spacing
           new_page = TRUE,
           txt_gp = fpTxtGp(label = gpar(cex = 0.8), 
                            ticks = gpar(cex = 0.8), 
                            xlab = gpar(cex = 1)),
           grid = TRUE)

我还尝试使用

axis()
sprintf()
手动修改轴来强制使用小数点,但似乎没有任何效果。 x 轴仍显示为
0.5
1
1.5
2
,而不是所需的
0.5
1.0
1.5
2.0

如何强制 x 轴在森林图的刻度标签中显示小数点?任何建议或见解将不胜感激!

r ggplot2 visualization r-forestplot forest-plots
1个回答
0
投票

源代码中隐藏了一条注释,提供了解决方案(https://github.com/cran/forestplot/blob/21c3d27d522fff669fa5e3852b7e71decf358741/R/forestplot.R#L108):

library(forestplot)
#> Loading required package: grid
#> Loading required package: checkmate
#> Loading required package: abind
adjusted_rr <- c(
  NA, 1.00, 0.77, 1.46, 0.79, 0.92, NA,
  NA, 1.00, 0.69, 0.67, 0.94, NA,
  NA, 1.00, 1.21, 0.98, NA,
  NA, 1.17, 1.21, 1.09, 1.23, 1.21, 1.00, NA,
  NA, 1.00, 1.24, 1.22, 1.11, 1.28, 1.24, 0.97, 1.03, NA,
  NA, 1.20, 1.15, 1.00, NA,
  NA, 1.10, 1.14, 1.09, 1.02, 1.00, NA,
  NA, 1.00, 0.86, NA,
  NA, 1.00, 0.88, 0.87, 0.84, 0.97, NA
)

lower_ci <- c(
  NA, 1.00, 0.70, 1.38, 0.72, 0.78, NA,
  NA, 1.00, 0.61, 0.61, 0.88, NA,
  NA, 1.00, 1.15, 0.85, NA,
  NA, 1.09, 1.07, 0.96, 1.07, 1.10, 1.00, NA,
  NA, 1.00, 1.01, 1.12, 0.94, 1.12, 1.13, 0.78, 0.91, NA,
  NA, 1.13, 1.05, 1.00, NA,
  NA, 1.01, 1.06, 1.00, 0.94, 1.00, NA,
  NA, 1.00, 0.78, NA,
  NA, 1.00, 0.81, 0.80, 0.77, 0.90, NA
)

upper_ci <- c(
  NA, 1.00, 0.85, 1.55, 0.87, 1.08, NA,
  NA, 1.00, 0.78, 0.73, 0.99, NA,
  NA, 1.00, 1.29, 1.12, NA,
  NA, 1.27, 1.38, 1.25, 1.41, 1.34, 1.00, NA,
  NA, 1.00, 1.53, 1.34, 1.32, 1.46, 1.35, 1.21, 1.16, NA,
  NA, 1.27, 1.26, 1.00, NA,
  NA, 1.19, 1.24, 1.18, 1.11, 1.00, NA,
  NA, 1.00, 0.95, NA,
  NA, 1.00, 0.95, 0.94, 0.91, 1.04, NA
)

labels <- rep("label_tmp", 59)

# Forest plot code
forestplot(labeltext = labels,
           mean = adjusted_rr, 
           lower = lower_ci, 
           upper = upper_ci,
           zero = 1.0,  # Center the plot at 1.0
           xlog = FALSE,
           xlab = "Adjusted Rate Ratio",
           boxsize = 0.2,
           is.summary = is.na(adjusted_rr),
           xticks = c(0.5, 1.0, 1.5, 2.0),  # Specify tick marks
           line.margin = 0.4,  # Compress horizontally
           graph.pos = 2, # Adjust vertical spacing
           new_page = TRUE,
           txt_gp = fpTxtGp(label = gpar(cex = 0.8), 
                            ticks = gpar(cex = 0.8), 
                            xlab = gpar(cex = 1)),
           grid = TRUE)

# add a label attribute to the xticks vector:
my_ticks <- c(0.5, 1.0, 1.5, 2.0)
attr(my_ticks, "labels") <- c("0.5", "1.0", "1.5", "2.0")

# Forest plot code
forestplot(labeltext = labels,
           mean = adjusted_rr, 
           lower = lower_ci, 
           upper = upper_ci,
           zero = 1.0,  # Center the plot at 1.0
           xlog = FALSE,
           xlab = "Adjusted Rate Ratio",
           boxsize = 0.2,
           is.summary = is.na(adjusted_rr),
           xticks = my_ticks,  # Specify tick marks
           line.margin = 0.4,  # Compress horizontally
           graph.pos = 2, # Adjust vertical spacing
           new_page = TRUE,
           txt_gp = fpTxtGp(label = gpar(cex = 0.8), 
                            ticks = gpar(cex = 0.8), 
                            xlab = gpar(cex = 1)),
           grid = TRUE)

创建于 2024-11-28,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.