I could use some advice on controlling line types in a ggplot. I have a plot in which line types (and colors) are chosen automatically. I would like to have some control, in particular, I would like the last line to be solid. In what may be a complication, there are 7 lines.
you can use "scale_linetype_manual()" for this,
e.g. with scale_linetype_manual(values = c("dotted", "dashed", "dotted", "dashed", "dotted", "dashed", "solid")) the last entry should be solid.
If there are cases where the order or the number of elements might change it's better to define a named list, e.g.
scale_linetype_manual(values = c("AUS" = "dotted", "CAN" = "dashed", "FRA" = "dotdash", "USA" = "solid")) here you need to define at least the chosen countries, but I think the list can be longer and contain more countries..
The standard syntax has 7 named linetypes of which one (0) is blank i.e. invisible so effectively 6 fully named linetypes
0 = blank, 1 = solid, 2 = dashed, 3 = dotted, 4 = dotdash, 5 = longdash, 6 = twodash
but , according to the documentation ( and I tested and confirmed) 4 character length digit code to construct your own manual varieties; they give the following example
# An example with hex strings; the string "33" specifies three units on followed
# by three off and "3313" specifies three units on followed by three off followed
# by one on and finally three off.
p + geom_line(linetype = "3313")
This is about a 95 percent successful solution. The one problem is that there doesn't appear to be a way to specify a solid line, as one has to specify pairs of lengths on and off. (And zero is not an acceptable part of the specification.)
[In case it is helpful to others, vignette("ggplot2-specs") gives some further information about this.]
In case it helps anyone else, part of the magic in @nirgrahamuk's solution is that you can mix the hex specifications and names like "solid." The other part of the magic is that scales::linetype_pal() will give you up to 13 unique patterns.