Hrishi lokhande
2011-Jul-12 23:34 UTC
[R] problem plotting points based on different values
Hello Friends, I am new to R and stuck with a problem. i have two columns drug_A and drug_B, i have plotted a scatter plot using the ggplot2 function. My problem is with the third column, it is the p-value column. I want to color and size points differently based on the p_value, the p_value range is between 0<0.0001< 0.001<0.05<1. I used a script using the ifelse loop, but it considers only two conditions at one time. Please tell me a way to fulfill all the conditions. here is the script: library(lattice) qplot(Drug_A, Drug_B, data=dsmall, size = p_value, colour ifelse(p_value > 0, ">0", "<0.0001"), + ifelse(p_value1 > 0.001, ">0.001", "<0.01") + ifelse(p_value1 > 0.001, ">0.001", "<0.05" ))
Hi: A few things that are worth considering in generating a plot of this type: (1) I suspect you would want larger point sizes with smaller p-values. (2) To generate intervals of p-value categories, it's easier to generate a factor in the data frame. (3) It would probably make sense to have some sort of 'nice' color gradient to associate with the p-value intervals. To that end, I generated a small data frame and tried the following. This assumes you can live with strict inequalities in the p-values. dd <- data.frame(x = rnorm(30), y = rnorm(30), p = rexp(30, 30)) table(dd$p) # see what you've got for p-value ranges # Generate a set of p-value intervals using the cut() function: dd$pval <- cut(dd$p, c(0, 0.0001, 0.001, 0.05, 1)) # Reverse the levels and generate some labels for the legend dd$pv <- factor(dd$pval, levels = rev(levels(dd$pval)), labels = c('p > 0.05', '0.001 < p < 0.05', '0.0001 < p < 0.001', 'p < 0.0001')) # plot # scale_colour_manual() is used to select a specific color scheme ggplot(dd, aes(x = x, y = y)) + theme_bw() + geom_point(aes(size = pv, colour = pv)) + labs(size = 'p-value', colour = 'p-value') + scale_colour_manual(breaks = levels(dd$pv), values = topo.colors(10)) If you want the <= values in the legend in a nice 'mathematics-like' inequality, that's not quite so easy... Just for fun, here's another take on the plot above, pulling the legend inside the plot region to increase the plotting area: ggplot(dd, aes(x = x, y = y)) + geom_point(aes(size = pv, colour = pv)) + labs(size = 'p-value', colour = 'p-value') + scale_colour_manual(breaks = levels(dd$pv), values = topo.colors(10)) + opts(legend.position = c(0.85, 0.88), legend.background = theme_rect(fill = 'white', colour = 'white')) If you don't like the white background on the legend, take out the legend.background = statement in its entirety. Its advantage, though, is that it 'paints over' several of the grid lines. HTH, Dennis On Tue, Jul 12, 2011 at 4:34 PM, Hrishi lokhande <hrishi27n at gmail.com> wrote:> Hello Friends, > > I am new to R and stuck with a problem. > > i have two columns drug_A and drug_B, i have plotted a scatter plot > using the ggplot2 function. > > My problem is with the third column, it is the p-value column. > > I want to color and size points differently based on the p_value, the > p_value range is between 0<0.0001< 0.001<0.05<1. > > I used a script using the ifelse loop, but it considers only two > conditions at one time. > > Please tell me a way to fulfill all the conditions. > > here is the script: > library(lattice) > > ?qplot(Drug_A, Drug_B, data=dsmall, size = p_value, colour > ifelse(p_value > 0, ">0", "<0.0001"), > + ifelse(p_value1 > 0.001, ">0.001", "<0.01") > + ifelse(p_value1 > 0.001, ">0.001", "<0.05" > )) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >