Instantly share code, notes, and snippets.

# cdesante/outliers.r Created Sep 9, 2012

Basic Plot in R with Conditional Coloring
 #Plotting Random Variables #rnorm(N, mean, sd): generates a #random normal variable of length N #with specified mean and std. dev. (sd) # x/ylim = range of X/Y axis. #col: colors, specified with an ifelse() #pch, plot symbol to use #pch list: http://voteview.com/symbols_pch.htm X <- rnorm(500) #draw var 1 Y <- rnorm(500) #draw var 2 plot(X, Y, xlim=c(-3,3),ylim=c(-3,3), col=ifelse(((abs(X)>1.65 & abs(Y)>1.65)),"red", "black"), main="Two Dimensional Outliers (in red)" , pch=ifelse(((abs(X)>1.65 & abs(Y)>1.65)), 17, 1) )

### bhive01 commented Oct 9, 2012

 Just for S's and giggles. ```require(ggplot2) require(scales) X <- rnorm(500) #draw var 1 Y <- rnorm(500) #draw var 2 xy<-data.frame(X,Y) #merge into data.frame ggplot(xy, aes(x=X, y=Y , color=ifelse(((abs(X)>1.65 & abs(Y)>1.65)),"A", "B") #names of levels are arbitrary, putting in alphabetical order , shape=ifelse(((abs(X)>1.65 & abs(Y)>1.65)),"A", "B")))+ geom_point()+ #drawing a scatterplot scale_color_manual(guide=FALSE, values=c("red", "black")) + #turn off the legend, define the colors scale_shape_manual(guide=FALSE, values=c(17, 1)) #turn off legend, define shapes #fin```