Search code examples
pdfr-markdownknitrkablekableextra

How to change line color of kableExtra::spec_pointrange when knitting PDF


kableExtra creates a table with some plots, as its manual demonstrates. We will have the following table with a pointrange plot with kableExtra::spec_pointrange(), running the code at the bottom of this post.

But how can I change colour of lines of the plot (Note, NOT the colour of the centre dots), when knitting a PDF. It should be awesome if we can freely specify the colour of the error bar and vertical dot line also in PDF, since somehow it seems to be possible to change the line colour interactively when knitting HTML as suggested in a blog post.

enter image description here

coef_table <- data.frame(
  Variables = c("var 1", "var 2", "var 3"),
  Coefficients = c(1.6, 0.2, -2.0),
  Conf.Lower = c(1.3, -0.4, -2.5),
  Conf.Higher = c(1.9, 0.6, -1.4)
)
data.frame(
  Variable = coef_table$Variables,
  Visualization = ""
) %>%
  kbl(booktabs = T) %>%
  kable_classic(full_width = FALSE) %>%
  column_spec(
    2, 
    image = spec_pointrange(
      x = coef_table$Coefficients,
      xmin = coef_table$Conf.Lower,
      xmax = coef_table$Conf.Higher,
      vline = 0
    )
)

Solution

  • The author of the package Hao Zhu added line_col to spec_pointrange. So now the kableExtra supports the line colour configuration.