0

I'm trying to plot the VALUE column and then label them with the LABEL column

X   VALUE   LABEL   COLOR
1   78  T041N2  3
2   77  T018N3  2
3   97  T014N3  1
4   0   T149N4  1
5   62  T043N1  3
6   66  T018N3  3
7   56  T145N4  3
8   63  T019N4  1
9   82  T039N0  1
10  75  T018N3  1
11  76  T018N3  1
12  63  T043N1  2
13  0   T149N4  2
14  73  T019N4  2
15  77  T019N4  3
16  100 T149N4  3
17  92  T043N1  3

I read the data then plot the VALUE data and then put the LABEL labels with the text command using the following code and works fine

my_data <- read.table("mydata.dat", header=T, sep="\t")
plot(my_data$VALUE, type="o")
text(my_data$VALUE, y = NULL, labels = my_data$LABEL, adj = NULL,pos = 3, offset = 0.5, vfont = NULL,cex = 0.5, col = NULL, font = NULL)

But now I was asked to paint each data point (that now are just black circles) according to the COLOR vector (1 is red, 2 is green and 3 is yellow).

How could I paint the points according to that COLOR vector?

zx8754
  • 42,109
  • 10
  • 93
  • 154
user3437823
  • 341
  • 1
  • 13

3 Answers3

0

First create a column that has the text of each color you want:

color_names <- c("red", "green", "yellow")

my_data$color_name <- color_names[my_data$COLOR]

Then add a col argument to your plot call:

plot(my_data$VALUE, type="o", col=my_data$color_name)

MDe
  • 2,358
  • 3
  • 18
  • 26
0

to complete the previous answer you can also modify the "palette"

palette(c("red", "green", "yellow"))
plot(my_data$VALUE, type="o", col=my_data$COLOR)

HTH

droopy
  • 2,578
  • 1
  • 11
  • 11
0

Try this:

#dummy data
my_data <- read.table(text="X   VALUE   LABEL   COLOR
1   78  T041N2  3
2   77  T018N3  2
3   97  T014N3  1
4   0   T149N4  1
5   62  T043N1  3
6   66  T018N3  3
7   56  T145N4  3
8   63  T019N4  1
9   82  T039N0  1
10  75  T018N3  1
11  76  T018N3  1
12  63  T043N1  2
13  0   T149N4  2
14  73  T019N4  2
15  77  T019N4  3
16  100 T149N4  3
17  92  T043N1  3", header=TRUE)

mycols<-c("red","green","yellow")

#using base plot
plot(my_data$VALUE, pch=19, bty="n",col=mycols[my_data$COLOR],main="Using base R")
lines(my_data$VALUE, type="b")
text(my_data$VALUE, y = NULL,
     labels = my_data$LABEL,
     adj = NULL, pos = 3,
     offset = 0.5, vfont = NULL,cex = 0.5, col = NULL, font = NULL)

enter image description here

zx8754
  • 42,109
  • 10
  • 93
  • 154
  • Thanks! how could I plot them if I had VALUE2 LABEL2 and COLOR2 too? because when I try to plot a second group of data points I lost the first one – user3437823 Mar 19 '14 at 20:52
  • If you need to plot on top of existing plot then see [here](http://stackoverflow.com/questions/2564258/plot-2-graphs-in-same-plot-in-r). Also, I suggest start using `ggplot()`, it will make your life so much easier. – zx8754 Mar 21 '14 at 10:43