r - Using colors in aes() function in ggplot2 -


i new ggplot2. trying understand how use ggplot. reading wickham's book , still trying wrap head around how use aes() function. in related thread, discussed should try avoid using variables inside aes() i.e. "don't put constants inside aes() - put mappings actual data columns."

my objective observe behavior of ggplots when have color inside aes() labeling (as described in wickham's book) , override color print color.

i started this:

library(ggplot2) data(mpg) ggplot(mpg, aes(displ, hwy)) +   geom_point() +   geom_smooth(aes(colour = "loess"), method = "loess", se = false) +   geom_smooth(aes(colour = "lm"), method = "lm", se = false) +   labs(colour = "method") 

this nicely plots graphs , labels them. however, unhappy colors used. so, experimented using overriding colors again:

windows() ggplot(mpg, aes(displ, hwy)) +   geom_point() +   geom_smooth(aes(colour = "loess"), method = "loess", se = false, color = "magenta") +   geom_smooth(aes(colour = "lm"), method = "lm", se = false, color = "red") +     labs(colour ="method") 

i added color = "red" , can see labs() or aes(color()) doesn't have effect. why happen? i'm curious. i'd appreciate thoughts.

when specify, colour outside aes() gg_plot not consider color information being part of data (and overwrites previous information) , there no legend display anymore.

if want specify own colors , keep colour information "relevant data" , not "display information", should add scale_colour_manual() command specify legend colours , leave colour attribute in aes:

ggplot(mpg, aes(displ, hwy)) +     geom_point() +     geom_smooth(aes(colour = "loess"), method = "loess", se = false) +     geom_smooth(aes(colour = "lm"), method = "lm", se = false) +       labs(colour ="method") + scale_colour_manual(values = c("loess" = "magenta", "lm" = "red")) 

enter image description here


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -