I want to add a new column consisting different colors to my data frame. So that I can use that column in the arg col
while plotting the points of that variable.
I want to assign same color for a values falling in same range and different colors for those falling in different range.
For example, I have a certain dataset consisting of following values:
> CN
A X
1 0.05
2 0.09
3 NA
4 0.12
5 0.35
6 0.21
7 NA
8 0.01
9 0.14
10 0.32
11 0.43
12 0.19
I would like to add a separate column (Col) to my dataframe CN
> CN$Col <= "white"
Its a success in creating a new column. I would like to assign different colors to each of these values. And assign either white
or a transparent color for NA
values. i.e
<0.05 -- red
0.05 - 0.1 -- orange
0.1 - 0.2 -- yellow
0.2 - 0.3 -- green
0.3 - 0.4 -- darkviolet
0.4 - 0.5 -- blue
Here is the code I have written
CN$Col <- "white"
for (i in 1:length(CN)) {
d <- is.na(CN$X[i])
if (d == "FALSE") {
if (CN$X[i] <= 0.05)
CN$Col[i] <- "red"
else if (CN$X[i] <= 0.1 && CN$X[i] > 0.05)
CN$Col[i] <= "orange"
else if (CN$X[i] <= 0.2 && CN$X[i] > 0.1)
CN$Col[i] <- "yellow"
else if (CN$X[i] <= 0.3 && CN$X[i] > 0.2)
CN$Col[i] <- "green"
else if (CN$X[i] <= 0.4 && CN$X[i] > 0.3)
CN$Col[i] <- "darkviolet"
else if (CN$X[i] <= 0.5 && CN$X[i] > 0.4)
CN$Col[i] <- "blue"
}
else
CN$Col[i] <- rgb(0, 0 , 0, alpha = 0) # rgb command can be replaced with color white
}
This code does not overwrite the white I have fed to the column earlier.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire