I am just trying to make a simple plot using mtcars and ggplot:
ggplot(data=mtcars, aes(x=mpg,y=hp))+geom_line(mpg,hp,col=cyl)
but I get the er
My suggestion is to specify all aesthetics in one place and to coerce cyl
to factor
for colour coding as categorial variable.
ggplot(data=mtcars, aes(x=mpg, y=hp, colour=factor(cyl))) + geom_line()
will create
See if you get better results passing hte argument to aes
inside geom_line
:
ggplot(data=mtcars, aes(x=mpg,y=hp))+geom_line(aes(x=mpg,y=hp,colour=cyl) )
I think the results are a bit clearer if you add a grouping parameter to the basic aes
spec:
ggplot(data=mtcars, aes(x=mpg,y=hp, group=cyl)) +
geom_line(aes(x=mpg,y=hp,colour=cyl) )
And even better legend anotation with a factor() on cyl:
ggplot(data=mtcars, aes(x=mpg,y=hp, group=cyl))+
geom_line(aes(x=mpg,y=hp,col=factor(cyl)) )