0

I'm doing some basic statistics in R and I'm trying to have a different color for each iteration of the loop. So all the data points for i=1 should have the same color, all the data points for i=2 should have the same color etc. The best would be to have different colors for the varying i ranging from yellow to blue for exemple. (I already tried to deal with Colorramp etc. but I didn't manage to get it done.)

Thanks for your help.

library(ggplot2)

#dput(thedata[,2])
#c(1.28994585412464, 1.1317747077577, 1.28029504741834, 1.41172820353708, 
#1.13172920065253, 1.40276516298315, 1.43679599499374, 1.90618019359643, 
#2.33626745030772, 1.98362330686504, 2.22606615548188, 2.40238822720322)
#dput(thedata[,4])
#c(NA, -1.7394747097211, 2.93081902519318, -0.33212717268786, 
#-1.78796119503752, -0.5080871442002, -0.10110379236627, 0.18977632798691, 
#1.7514277696687, 1.50275797771879, -0.74632159611221, 0.0978774103243802)

#OR
#dput(thedata[,c(2,4)])
#structure(list(LRUN74TTFRA156N = c(1.28994585412464, 1.1317747077577, 
#1.28029504741834, 1.41172820353708, 1.13172920065253, 1.40276516298315, 
#1.43679599499374, 1.90618019359643, 2.33626745030772, 1.98362330686504, 
#2.22606615548188, 2.40238822720322), SELF = c(NA, -1.7394747097211, 
#2.93081902519318, -0.33212717268786, -1.78796119503752, -0.5080871442002, 
#-0.10110379236627, 0.18977632798691, 1.7514277696687, 1.50275797771879, 
#-0.74632159611221, 0.0978774103243802)), row.names = c(NA, 12L
#), class = "data.frame")

x1=1
xn=x1+3

plot(0,0,col="white",xlim=c(0,12),ylim=c(-5,7.5))
for(i in 1:3){
y=thedata[x1:xn,4]
x=thedata[x1:xn,2]
reg<-lm(y~x)
points(x,y,col=colors()[i])
abline(reg,col=colors()[i])
x1=x1+4
xn=x1+3
}
  • 2
    It will be much easier to help you if you make your question reproducible by sharing some sample data and the other constants you use (e.g., `n`). See [this post for lots of good tips](https://stackoverflow.com/q/5963269/903061). – Gregor Thomas Jan 30 '19 at 03:04
  • Thank you for this advice. I have checked your link and edited the code, but I wasn't quite sure how to create a dataframe with only two columns, so I put the data in the comments. –  Jan 30 '19 at 20:53
  • 1
    Thanks for posting the `dput`, this is much improved. To just get those to columns, you could use `dput(thedata[, c(2, 4)])`. A couple other issues: (a) is `x2` supposed to be `xn`? (b) Please *don't* post `rm(list = ls())` in your question. No one wants to accidentally copy/paste/run that and delete all their data. – Gregor Thomas Jan 30 '19 at 21:22
  • I don't have time to work on an answer just now, but I'll check back later on and add one if there still seem to be gaps. – Gregor Thomas Jan 30 '19 at 21:23
  • You're absolutely right. I've edited it. It is great to have some help as a beginner, I really appreciate it. –  Jan 30 '19 at 21:51
  • @Robby I still don't get what your problem is... I've run your code, and it runs as I would expect it to. In a comment to my answer you said that you don't know how to create a dataframe, but you just got one! Please let us know *explicitly* what your problem is; help us help you. – PavoDive Jan 31 '19 at 16:21
  • It is now solved. Thank you for your adivce. I will try to be more explicit the next time. I really appreciate this forum. –  Feb 01 '19 at 22:12

2 Answers2

0

Besides of lacking a reproducible example, you seem to have some misconceptions.

First, the function colors doesn't take a numeric argument, see ?colors. So if you want to fetch a different color in each iteration, you need to call it like colors()[i]. The code should look something similar to this (in absence of a reproducible example):

for (i in 20:30){
    plot(1:10, 1:10, col = colors()[i])
}

Please bear in mind that the call of x1 and xn in your first and second lines inside the for loop, before defining them will cause an error too.

PavoDive
  • 6,322
  • 2
  • 29
  • 55
  • Thank you. I've edited the question and put the data in the comments as I don't know yet how to create a suitable dataframe. It would be nice if you could look at it again how I could fix it in my case. I'm trying exactly what is written in the comment just above. –  Jan 30 '19 at 21:08
0

The basic idea of colorRamp and colorRampPalette is that they are functionals - they are functions that return functions.

From the help page:

colorRampPalette returns a function that takes an integer argument (the required number of colors) and returns a character vector of colors (see rgb) interpolating the given sequence (similar to heat.colors or terrain.colors).

So, we'll get a yellow-to-blue palette function from colorRampPalette, and then we'll give it the number of colors we want along that ramp to actually get the colors:

# create the palette function
my_palette = colorRampPalette(colors = c("yellow", "blue"))
# test it out, see how it works
my_palette(3)
# [1] "#FFFF00" "#7F7F7F" "#0000FF"
my_palette(5)
# [1] "#FFFF00" "#BFBF3F" "#7F7F7F" "#3F3FBF" "#0000FF"

# Now on with our plot
x1 = 1
xn = x1 + 3
# Set the number of iterations (number of colors needed) as a variable:
nn = 3
# Get the colors from our palettte function
my_cols = my_palette(nn)

# type = 'n' means nothing will be plotted, no points, no lines
plot(0, 0, type = 'n',
     xlim = c(0, 12),
     ylim = c(-5, 7.5))

# plot
for (i in 1:nn) {
  y = thedata[x1:xn, 2]
  x = thedata[x1:xn, 1]
  reg <- lm(y ~ x)
  # use the ith color
  points(x, y, col = my_cols[i])
  abline(reg, col = my_cols[i])
  x1 = x1 + 4
  xn = x1 + 3
}

You can play with just visualizing the palette---try out the following code for different n values. You can also try out different options, maybe different starting colors. I like the results better with the space = "Lab" argument for the palette.

n = 10
my_palette = colorRampPalette(colors = c("yellow", "blue"), space = "Lab")
n_palette = my_palette(n)
plot(1:n, rep(1, n), col = n_palette, pch = 15, cex = 4)

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you! I'll check it out when I'm at home. –  Feb 01 '19 at 07:54
  • I've looked at it now and it works perfectly! When I worked with colorRampPalette my trouble was that for i=3 for example the program printed out the data points in three colors in this iteration. Now it seems to be like I wanted it to have (one color for one iteration). I'm just wondering why (if you have an idea...). A last question would be if it was possible to have colors that go smoothly from yellow to blue so that it can be seen how "old" the data points are (first iteration yellow, last iteration blue for exemple)? You really do a great job:). –  Feb 01 '19 at 21:30
  • For why it didn't work before, you probably were getting a new palette each time - I get a `n` color palette with `my_cols = my_palette(nn)`. You were probably getting a 1-color palette for the first group, a 2-color palette for the 2nd group, etc. – Gregor Thomas Feb 01 '19 at 21:34
  • Oh yes, you're right. Sorry, I still get confused in R sometimes. Thanks a lot for you help and patience! –  Feb 01 '19 at 22:00