Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colour points in a plot differently depending on a vector of values

I'm plotting the figure below using R's plot() function. It is a plot of a vector shiftTime of shift in time. I have another vector intensity of the intensity values ranging from ~3 to ~9. I want to color my points in the plot based on those values with a color gradient. The examples I can find color on the value of the actual plotted points, so in this case the values of the vector shiftTime. Is it also possible to use a different vector, as long as the corresponding values are on the same index?

plot

like image 832
Niek de Klein Avatar asked Mar 30 '12 16:03

Niek de Klein


People also ask

How do you specify colors in a plot?

Color Name or Short Name — Specify the name of a color such as 'red' or 'green' . Short names specify a letter from a color name, such as 'r' or 'g' . RGB Triplet — Create a custom color by specifying a three-element row vector whose elements are the intensities of the red, green, and blue components of a color.

How do I change the color of my plot points in R?

Output: Now to change the colors of a scatterplot using plot(), simply select the column on basis of which different colors should be assigned to various points. Pass the column that will help differentiate between points to “col” attribute.

How do I change the color of a point in a scatter plot in R?

To change scatter plot color according to the group, you have to specify the name of the data column containing the groups using the argument groupName . Use the argument groupColors , to specify colors by hexadecimal code or by name .


1 Answers

Here's a solution using base R graphics:

#Some sample data x <- runif(100) dat <- data.frame(x = x,y = x^2 + 1)  #Create a function to generate a continuous color palette rbPal <- colorRampPalette(c('red','blue'))  #This adds a column of color values # based on the y values dat$Col <- rbPal(10)[as.numeric(cut(dat$y,breaks = 10))]  plot(dat$x,dat$y,pch = 20,col = dat$Col) 

enter image description here

like image 132
joran Avatar answered Nov 15 '22 23:11

joran