Instantly share code, notes, and snippets.

What would you like to do?
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:
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)

This comment has been minimized.

Copy link

bhive01 commented Oct 9, 2012

Just for S's and giggles.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment