0

I'm very new to R and have tried to search around for an answer to my question, but couldn't find quite what I was looking for (or I just couldn't figure out the right keywords to include!). I think this is a fairly common task in R though, I am just very new.

I have a x vs y scatterplot and I want to color those points for which there is at least a 2-fold enrichment, ie where x/y>=2 . Since my values are expressed as log2 values, the the transformed value needs to be x/y>=4.

I currently have the scatterplot plotted with

plot(log2(counts[,40], log2(counts[,41))

where counts is a .csv imported files and 40 & 41 are my columns of interested.

I've also created a column for fold change using

counts$fold<-counts[,41]/counts[,40]

I don't know how to incorporate these two pieces of information... Ultimately I want a graph that looks something like the example here: http://s17.postimg.org/s3k1w8r7j/error_messsage_1.png where those points that are at least two-fold enriched will colored in blue.

Any help would be greatly appreciated. Thanks!

moxed
  • 343
  • 1
  • 6
  • 16
  • 1
    For future reference, you're more likely to get help that's tailored directly to your problem if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), especially a sample of your data, the code you've tried so far, and the results you're trying to achieve. – eipi10 Sep 24 '14 at 21:51

1 Answers1

3

Is this what you're looking for:

# Fake data
dat = data.frame(x=runif(100,0,50), y = rnorm(100, 10, 2))

plot(dat$x, dat$y, col=ifelse(dat$x/dat$y > 4, "blue", "red"), pch=16)

The ifelse statement creates a vector of "blue" and "red" (or whatever colors you want) based on the values of dat$x/dat$y and plot uses that to color the points.

This might be helpful if you've never worked with colors in R.

enter image description here

Another option is to use ggplot2 instead of base graphics. Here's an example:

library(ggplot2)
ggplot(dat, aes(x,y, colour=cut(x/y, breaks=c(-1000,4,1000), 
                                labels=c("<=4",">4")))) + 
  geom_point(size=5) + 
  labs(colour="x/y")

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285