Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scatterplot with color groups - base R plot

Tags:

r

I have a simple scatter plot

x<-rnorm(100)
y<-rnorm(100)
z<-rnorm(100)

I want to plot the plot(x,y) but the color of the points should be color coded based on z.

Also, I would like to have the ability to define how many groups (and thus colours) z should have. And that this grouping should be resistant to outliers (maybe split the z density into n equal density groups).

Till now I do this manually, is there any way to do this automatically?

Note: I want to do this with base R not with ggplot.

like image 476
ECII Avatar asked Dec 12 '11 13:12

ECII


People also ask

How do I color a scatter plot in a group in R?

The different color systems available in R have been described in detail here. 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 .

How do you make a scatterplot in base R?

A scatter plot can be created using the function plot(x, y). The function lm() will be used to fit linear models between y and x. A regression line will be added on the plot using the function abline(), which takes the output of lm() as an argument. You can also add a smoothing line using the function loess().

How do I Group A scatterplot in R?

If you have a grouping variable you can create a scatter plot by group passing the variable (as factor) to the col argument of the plot function, so each group will be displayed with a different color.

How do you specify colors in a scatter plot?

scatter( x , y , sz , c ) specifies the circle colors. You can specify one color for all the circles, or you can vary the color. For example, you can plot all red circles by specifying c as "red" .


1 Answers

You can pass a vector of colours to the col parameter, so it is just a matter of defining your z groups in a way that makes sense for your application. There is the cut() function in base, or cut2() in Hmisc which offers a bit more flexibility. To assist in picking reasonable colour palettes, the RColorBrewer package is invaluable. Here's a quick example after defining x,y,z:

z.cols <- cut(z, 3, labels = c("pink", "green", "yellow"))
plot(x,y, col = as.character(z.cols), pch = 16)

enter image description here

You can obviously add a legend manually. Unfortunately, I don't think all types of plots accept vectors for the col argument, but type = "p" obviously works. For instance, plot(x,y, type = "l", col = as.character(z.cols)) comes out as a single colour for me. For these plots, you can add different colours with lines() or segments() or whatever the low level plotting command you need to use is. See the answer by @Andrie for doing this with type = "l" plots in base graphics here.

like image 153
Chase Avatar answered Nov 06 '22 10:11

Chase