Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

create smooth plots from different variables of same data set

$
0
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


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>