ggplot2 - How to format the scatterplots of data series in R -


i have been struggling in creating decent looking scatterplot in r. wouldn't think difficult. after research, seemed me ggplot have been choice allowing plenty of formatting. however, i'm struggling in understanding how works. i'd create scatterplot of 2 data series, displaying points 2 different colours, , perhaps different shapes, , legend series names. here attempt, based on this:

year1 <- mpg[which(mpg$year==1999),] year2 <- mpg[which(mpg$year==2008),]  ggplot() +    geom_point(data = year1, aes(x=cty,y=hwy,color="yellow"))  +   geom_point(data = year2, aes(x=cty,y=hwy,color="green")) +   xlab('cty') +   ylab('hwy') 

now, looks ok, non-matching colors (unless became color-blind). why that? also, how can add series names , change symbol shapes?

enter image description here

don't build 2 different dataframes:

df <- mpg[which(mpg$year%in%c(1999,2008)),] df$year<-as.factor(df$year) ggplot() +    geom_point(data = df, aes(x=cty,y=hwy,color=year,shape=year))  +   xlab('cty') +   ylab('hwy')+   scale_color_manual(values=c("green","yellow"))+   scale_shape_manual(values=c(2,8))+   guides(colour = guide_legend("year"),          shape = guide_legend("year")) 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -