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

时间:2019-07-27 06:31:29

标签: r function ggplot2 ggpubr

我正在通过修改位于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")

我没有收到此错误。

我尝试通过修改limits函数内的mappingggplot()参数,但未成功。

如何获取此图像以打印数字代码及其名称?

1 个答案:

答案 0 :(得分:2)

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

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"))

enter image description here

相关问题