Skip to content

Instantly share code, notes, and snippets.

@cdesante
Created September 9, 2012 14:52
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cdesante/3684833 to your computer and use it in GitHub Desktop.
Save cdesante/3684833 to your computer and use it in GitHub Desktop.
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
Copy link

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

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