Reputation: 2800
I am creating this function by modifying one located in ggpubr
package, called show_line_types()
. I want this function to have the number code of the 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"))
}
Once it is run, I get this error:
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
However, when I do the same with the object lt
modified, as shown here:
lt <- c("blank", "solid", "dashed", "dotted", "dotdash",
"longdash", "twodash")
I do not get this error.
I tried by modifying the limits
and the mapping
arguments inside the ggplot()
function, but unsuccessfully.
How can I get this image to have the number code along with their names printed?
Upvotes: 2
Views: 6993
Reputation: 7724
The issue is that you are mapping the linetype to a vector which does not contain linetypes as expected. This is one possible solution:
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"))
Upvotes: 4