如何解决 grid.Call.graphics(C_segments, x$x0, x$y0, x$x1, x$y1, x$arrow) 中的错误

How to solve Error in grid.Call.graphics(C_segments, x$x0, x$y0, x$x1, x$y1, x$arrow)

我通过修改位于 ggpubr 包中的一个名为 show_line_types() 的函数来创建此函数。我希望此函数具有线型的数字代码:

lineas_r <- function () 
{
    lt <- c("1 blank", "2 solid", "3 dashed", "4 dotted", "5 dotdash", 
        "6 longdash", "7 twodash")
    d <- data.frame(lt = factor(lt, levels = lt))
    ggplot() + scale_x_continuous(name = "", limits = c(0, 1), 
        breaks = NULL) + scale_linetype_identity() + geom_segment(data = d, 
        mapping = aes(x = 0, xend = 1, y = lt, yend = lt, linetype = lt)) + 
        labs(y = "") + theme(axis.text.y = element_text(face = "bold", 
        color = "black"))
}

一旦它是 运行,我得到这个错误:

Error in grid.Call.graphics(C_segments, x$x0, x$y0, x$x1, x$y1, x$arrow) : invalid line type: must be length 2, 4, 6 or 8

然而,当我对修改后的对象lt执行相同操作时,如下所示:

lt <- c("blank", "solid", "dashed", "dotted", "dotdash", 
        "longdash", "twodash")

我没有收到这个错误。

我尝试修改 ggplot() 函数中的 limitsmapping 参数,但没有成功。

我怎样才能让这张图片印上数字代码和他们的名字?

问题是您将线型映射到不包含预期线型的向量。这是一种可能的解决方案:

lt <- c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
lt_names <- c("1 blank", "2 solid", "3 dashed", "4 dotted", "5 dotdash", "6 longdash", "7 twodash")

d <- data.frame(lt, lt_names)

ggplot() + scale_x_continuous(name = "", limits = c(0, 1), breaks = NULL) + 
  scale_linetype_identity() + 
  geom_segment(data = d, mapping = aes(x = 0, xend = 1, y = lt_names, yend = lt_names, linetype = lt)) + 
  labs(y = "") + 
  theme(axis.text.y = element_text(face = "bold", color = "black"))