0

I'm trying to create a smooth plot in which color represents manufacturer and the lines represent the overall relationship between disp and hwy. I can do that with this:

df <- subset(mpg, manufacturer==c("audi","chevrolet"))
df$year <- factor(df$year)
ggplot(df, aes(displ, hwy, color = manufacturer)) +
      geom_smooth(se = FALSE, method = lm)

Now I would like df$year to be plotted with the same colors for audi and chevrolet in the plot above. So two (1998 and 2009) redish lines for audi and two (1998 and 2009) blueish lines for chevrolet.

I tried adding this line to the command above

+ geom_smooth(aes(color=year), se = FALSE, method = lm)

but it creates a separate legend for year with different colors, whereas I want the lines to match the audi and chevrolet colors from the plot above

HappyPy
  • 9,839
  • 13
  • 46
  • 68
  • Can you provide a reproducible example of the dataset you are trying to plot ? See this: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Also, can you add an image showing the plot you are getting so far. – dc37 Dec 10 '19 at 03:33

1 Answers1

1

How about this?

p <- ggplot(df, aes(displ, hwy, color = manufacturer, 
                    linetype = year)) +
  geom_smooth(se = FALSE, method = lm) 
p

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27