Help with setting line types in ggplot

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.

My code is

long_leverage |> ggplot(aes(x=theDate, y = Leverage, color = Country,
                            linetype = Country)) +
  geom_line() +
  theTheme + labs(color = "Countries", linetype = "Countries") +
  xlab("") + ylab("leverage")

The picture that results is


I would like the US to be solid rather than Australia. (Not a reprex because the data has 2000+ rows.)

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..

I appreciate this, but scale_linetype_manual seems to only allow 6 line types. Left to itself, ggplot seems to have plotted 7 unique line types.

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

Thank you @nirgrahamuk .

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.]

you might tackle this by sharing fewer theDate values

anyway; here is an example of building a scale, and then adjusting it.



example_df <- expand_grid(
  dates = 1:50,
  grp=factor(1:7)) |> arrange(grp,dates) |> rowwise() |> mutate(
    rand_val=sample.int(n=2,size=1) - 1) |> group_by(grp)|>
  mutate(yval=cumsum(rand_val))|>ungroup()



my_scale <- scales::linetype_pal()(7)

(grp_vals <- unique(example_df$grp))

names(my_scale) <- grp_vals
my_scale

#manually adjust swap 1 and 7 ?
tmp <- my_scale[1] 
my_scale[1] <- my_scale[7]
my_scale[7] <- tmp
my_scale

ggplot(data=example_df) +
  aes(x=dates,
      color=grp,
      linetype=grp,
      y=yval) +
  geom_line() +
  scale_linetype_manual(values=my_scale)
1 Like

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.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.