Flaherty, Sarah (NIH/NCI) [F]
2019-Jul-05 15:15 UTC
[R] How to set scale of color scatterplot points relative to a third variable?
Hello Kind and Generous Volunteers, I'm trying to use a scatter plot to visualize three variables: time, radius, and intensity. I would like each point on the scatterplot to be colored according to a gradient, in which red indicates an intensity less than 1, white indicates an intensity of 1, and green indicates an intensity more than 1. ``` #A1 scatter plot colors <- c("firebrick2", "white", "darkolivegreen3") class <- classIntervals(A1.matrix[,3], 10) #A1.matrix[,3] is "Intensity" colorcode <- findColours(class, colors) plot(A1.matrix[,1], A1.matrix[,2]) #A1.matrix[,1] is "Time" and A1.matrix[,2] is "Radius" points(A1.matrix[,1], A1.matrix[,2], pch = 19, col = colorcode) ``` It is very close to what I am looking for. Firebrick2 and darkolivegreen3 correspond to the minimum and maximum values in A1.matrix[,3] respectively. But white does not correspond to 1. How can I set white equal to 1? Ideally, the graph would also have a color bar scale to show what the min and max values are. I?ve read that findColours returns two attributes for use in constructing a legend, but I haven?t been able to find much more information than that. I've had success doing something similar with a levelplot heatmap, using at=unique as shown below. However, I just copied that from somewhere online and it doesn't seem to work in the points function, I can?t find it using help() or google, and I'm not sure how it works or if it could somehow be applied. The colorbar scale is automatically part of the graph with levelplot. ``` #A1 heat map levelplot(nbin.A1, cuts = 50, at= unique(c(seq(A1min,1, length = 100), seq(1,A1max, length=100))), col.regions = colorRampPalette(c("firebrick2", "white", "darkolivegreen3"))(1e3), xlab = "Radius (Microns)", ylab = "Time") ``` Thank you for reading! Your volunteer work is SO appreciated!! Best, Sarah Flaherty [[alternative HTML version deleted]]
Jim Lemon
2019-Jul-06 07:34 UTC
[R] How to set scale of color scatterplot points relative to a third variable?
Hi Sarah, The size_n_color function in the plotrix package does something like this. To get the colors that you want using "color.scale" in the same package, look at the help page for that function. The values in A1.matrix[,3] probably don't range from 0 to 2, so getting exactly what you want can be a bit tricky with 1=white. If you can post a representative sample of these values, it should be doable. Jim On Sat, Jul 6, 2019 at 2:56 AM Flaherty, Sarah (NIH/NCI) [F] via R-help <r-help at r-project.org> wrote:> > Hello Kind and Generous Volunteers, > > I'm trying to use a scatter plot to visualize three variables: time, radius, and intensity. I would like each point on the scatterplot to be colored according to a gradient, in which red indicates an intensity less than 1, white indicates an intensity of 1, and green indicates an intensity more than 1. > > ``` > #A1 scatter plot > > colors <- c("firebrick2", "white", "darkolivegreen3") > class <- classIntervals(A1.matrix[,3], 10) #A1.matrix[,3] is "Intensity" > colorcode <- findColours(class, colors) > plot(A1.matrix[,1], A1.matrix[,2]) #A1.matrix[,1] is "Time" and A1.matrix[,2] is "Radius" > points(A1.matrix[,1], A1.matrix[,2], pch = 19, col = colorcode) > > ``` > > It is very close to what I am looking for. Firebrick2 and darkolivegreen3 correspond to the minimum and maximum values in A1.matrix[,3] respectively. But white does not correspond to 1. How can I set white equal to 1? > > > Ideally, the graph would also have a color bar scale to show what the min and max values are. I?ve read that findColours returns two attributes for use in constructing a legend, but I haven?t been able to find much more information than that. > > I've had success doing something similar with a levelplot heatmap, using at=unique as shown below. However, I just copied that from somewhere online and it doesn't seem to work in the points function, I can?t find it using help() or google, and I'm not sure how it works or if it could somehow be applied. The colorbar scale is automatically part of the graph with levelplot. > > ``` > #A1 heat map > > levelplot(nbin.A1, cuts = 50, at= unique(c(seq(A1min,1, length = 100), seq(1,A1max, length=100))), col.regions = colorRampPalette(c("firebrick2", "white", "darkolivegreen3"))(1e3), xlab = "Radius (Microns)", ylab = "Time") > > ``` > > Thank you for reading! Your volunteer work is SO appreciated!! > > Best, > Sarah Flaherty > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.