1

Hi I have used following code to generate a plot a data in a matrix

#load the data
data <- read.table("hedge.txt",sep="\t",header=TRUE,row.names=1)
data_matrix <- data.matrix(data)

#plot bcl2
plot(data_matrix["BCL2",],col="blue")

and I got following as the plot. But I need to plot 1:6, 7:49, 50:76 as three different colors?

2 Answers2

1

Answer without know what data and columns are inside your dataset is complicated. You asked how to plot different groups with different colors, does your dataset have a column "group"? If yes you can use unclass instead of select the rows of each groups. Please see a minimal reproducible dataset

df1=data.frame(val_x=runif(100,0,1),
               val_y=runif(100,0,1),
               group=c(rep("group1",33),
                       rep("group2",33),
                       rep("group3",34)))

and my suggestion for the plot

plot(df1[,"val_x"],col=c("red","green","blue")[unclass(df1$group)])
alb_alb
  • 58
  • 5
  • You should try: ```plot(df1[,"val_x"],df1[,"val_y"], col=c("red","green","blue")[as.factor(df1$group)])``` instead, as this actually produces a plot from your example. – Emil11 Jun 13 '22 at 10:59
0

1.Create minimal reproducible example:

m <- matrix(runif(76*2), ncol=2)

2.Solution using base R:

This first call to plot serves just as a quick 'hack' to get the right sized axis

plot(m, color="white")

Now we plot the actual points with their colors:

points(m[1:6, ],col="red")
points(m[7:49, ],col="blue")
points(m[50:76, ],col="green")

This generates the following plot:

enter image description here

dario
  • 6,415
  • 2
  • 12
  • 26