public
Created

Basic Plot in R with Conditional Coloring

  • Download Gist
outliers.r
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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)
)

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.