Thomas Philips
Thomas Philips

Reputation: 1089

Correct syntax for manually scaling the width of lines in ggplot

I plot a melted dataframe using the following code (just a minimal fragment shown for clarity):

ggplot(df_melt, aes(x = Date, y= value)) +
          geom_line(aes(color = Variable, size = Variable)) +
          scale_color_manual(values = c("dark green", "azure3", "goldenrod4")) +
          scale_size_manual(values = c(.6, .8, .6)) +
          .
          .
          .

but get the following error message:

Warning message:
Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated. 

But if I change size = Variable to linewidth = Variable, I get a different warning:

Warning messages:
1: Using linewidth for a discrete variable is not advised. 

If I further change scale_size_manual to scale_linewidth_manual, I get an error message:

Error in scale_linewidth_manual(values = c(0.6, 0.7, 0.6, 0.3)) : 
  could not find function "scale_linewidth_manual"

What is the correct syntax here? Unfortunately, ggplot2's help got me nowhere, so I would be very appreciative for some guidance.

Upvotes: 8

Views: 3276

Answers (1)

teunbrand
teunbrand

Reputation: 37913

Edit

Since ggplot2 3.4.1, scale_linewidth_manual() (and the identity variant) exists.

Original answer

The current preferred option seems to be to use scale_discrete_manual(), see also https://github.com/tidyverse/ggplot2/issues/5050. To demonstrate:

library(ggplot2)

ggplot(economics_long, aes(date, value01)) +
  geom_line(
    aes(colour = variable, linewidth = variable)
  ) +
  scale_discrete_manual("linewidth", values = seq(0.1, 3, length.out = 5))

Created on 2022-12-10 by the reprex package (v2.0.1)

Upvotes: 11

Related Questions