HappyPy
HappyPy

Reputation: 10697

create smooth plots from different variables of same data set

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

Upvotes: 0

Views: 40

Answers (1)

Zhiqiang Wang
Zhiqiang Wang

Reputation: 6769

How about this?

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

enter image description here

Upvotes: 1

Related Questions