adayim / forestploter

Draw forestplot in R
Other
79 stars 10 forks source link

The reference line disappears #67

Closed daydayfei closed 1 month ago

daydayfei commented 2 months ago

I found that when there are many lines, the reference line can't run through the whole data frame, but it is displayed in the form of vertical center, and there will be several lines at the top and bottom without reference line. image image

adayim commented 2 months ago

Hi, can you send me a reproduceable example so I can replicate the issue?

daydayfei commented 2 months ago
# Load required `libraries`
library(forestploter)

# Generate random data
set.seed(123)  # Set random seed to ensure reproducibility
n <- 200  # Number of rows in the data
estimates = rnorm(n, mean = 0, sd = 1)

# Generate random data
data <- data.frame(
  study = paste0("Study_", 1:n),
  estimates = estimates,
  lower = estimates - abs(rnorm(n, mean = 0, sd = 0.5)),
  upper = estimates + abs(rnorm(n, mean = 0, sd = 0.5))
)
data$` ` <- paste(rep(" ", 30), collapse = " ")  # Add a blank column for spacing

# Define custom theme
tm <- forest_theme(base_size = 10,
                   # Confidence interval point shape, line type/color/width
                   ci_pch = 15,
                   ci_col = "#762a83",
                   ci_fill = "black",
                   ci_alpha = 0.8,
                   ci_lty = 1,
                   ci_lwd = 1.5,
                   ci_Theight = 0.2, # Set a T end at the end of CI 
                   # Reference line width/type/color
                   refline_lwd = gpar(lwd = 1, lty = "dashed", col = "grey20"),
                   # Vertical line width/type/color
                   vertline_lwd = 1,
                   vertline_lty = "dashed",
                   vertline_col = "grey20",
                   # Change summary color for filling and borders
                   summary_fill = "#4575b4",
                   summary_col = "#4575b4",
                   # Footnote font size/face/color
                   footnote_gp = gpar(cex = 0.6, fontface = "italic", col = "blue"))

# Save the forest plot as a PDF
cairo_pdf(
  file = paste0("Forestplot1.pdf"), onefile = FALSE,
  width = 12,
  height = 50, family = "serif"
)

# Create the forest plot
p <- forestploter::forest(
  data,
  est = data$estimates,
  lower = data$lower,
  upper = data$upper,
  ref_line = 1,
  theme = tm,
  cex = 5,  # Globally control the size of points
  ci_column = 5
)

# Print and save the plot
print(p)
dev.off()
daydayfei commented 2 months ago

Forestplot1.pdf

adayim commented 2 months ago

I think this was because I want to have a gap for vertical line. But I never thought one would have 200 observations. I have removed the gap. You can try it now and the following parameters in the theme function should be corrected. I have made a mistake in the vignette.

refline_lwd = gpar(lwd = 1, lty = "dashed", col = "grey20")

to

refline_gp = gpar(lwd = 1, lty = "dashed", col = "grey20"),]
daydayfei commented 2 months ago

Thanks for the quick fix! ๐Ÿ‘๐Ÿ‘๐Ÿ‘I completely understand the rationale behind the initial design of the gap, which indeed can enhance the readability and aesthetics of the plot in certain scenarios๏ผŒbut yeah, 200 or other large number obs can mess that up. This gap seems to be a scale of 0.02. In 100 rows, 1 line of reference lines at the top and bottom disappears. I think being able to set this gap to a fixed value of 0.1 or some other numerical line spacing might be a good compromise for visuals and functionality.But it's okay now.๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š