Scatterplot with Color Groups - Base R Plot

Scatterplot with color groups - base R plot

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)

Sample Image

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.

How to give color to a class in scatter plot in R?

Using

txt <- "ACTIVITY     LAT            LONG
Resting 21.14169444 70.79052778
Feeding 21.14158333 70.79313889
Resting 21.14158333 70.79313889
Walking 21.14163889 70.79266667
Walking 21.14180556 70.79222222
Sleeping 21.14180556 70.79222222"
dat <- read.table(text = txt, header = TRUE)

One option is to index into a vector of colours of length nlevels(ACTIVITY) using the ACTIVITY variable as the index.

cols <- c("red","green","blue","orange")
plot(LAT ~ LONG, data = dat, col = cols[dat$ACTIVITY], pch = 19)
legend("topleft", legend = levels(dat$ACTIVITY), col = cols, pch = 19, bty = "n")

This produces

Sample Image

To see why this works, cols is expanded to

> cols[dat$ACTIVITY]
[2] "green" "red" "green" "orange" "orange" "blue"

because ACTIVITY is a factor but stored numerically as 1,2,...,n.

Other higher-level solutions are available, so consider the ggplot2 package for simple creation of the same plot.

library("ggplot2")
plt <- ggplot(dat, aes(x = LONG, y = LAT, colour = ACTIVITY)) +
geom_point()
plt

which produces

Sample Image

How to separate scatterplot by different colors by group in pairs.panels() in R

You can adjust the background colour of the dots via the "bg" option if you first change the dot 'shape' (the pch; e.g. see https://www.datanovia.com/en/blog/pch-in-r-best-tips/):

library(psych)
n = 1000
kindness <- rnorm(n)
beauty <- rnorm(n)
score <- kindness + beauty
c50 <- quantile(score, .50)
marriage <- ifelse(score>=c50, 0, 1)
df <- data.frame(marriage, beauty, kindness)

pairs.panels(df, digits = 3, pch = 21,
bg=c("red","purple")[factor(df$marriage)])

example_1.png

Or you can use a colour palette like viridis or RColorBrewer, e.g.

pairs.panels(df, digits = 3, pch = 21,
bg=viridis::viridis(2)[factor(df$marriage)])

example_2.png

Colouring plot by factor in R

data<-iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1)

should do it for you. But I prefer ggplot2 and would suggest that for better graphics in R.



Related Topics



Leave a reply



Submit