Categorizing samples in R, and plotting them in different colors

1.3k Views Asked by At

I am new to learning R. I wanted to know how I can asssign a categorical value to observations I have read in as a dataframe. For eg I have data for m variables from n samples and I want to assign some samples as group 1 and some samples as group 2 and so on. Also, how can I visualise different groups in different colors when I plot them?

1

There are 1 best solutions below

2
On

Let's say you have the following data:

spam = data.frame(value = runif(100))

you can assign random group membership like this:

spam[["group"]] = sample(c("group1", "group2"), nrow(spam), replace = TRUE)

> head(spam)
      value  group
1 0.1385715 group1
2 0.1785452 group1
3 0.7407510 group2
4 0.5867080 group1
5 0.1514461 group1
6 0.3009905 group1

Plotting the groups with different colors can easily be done using ggplot2:

require(ggplot2)
ggplot(aes(x = 1:nrow(spam), y = value, color = group), data = spam) + 
  geom_point()

enter image description here