1

I'm trying to analyse weather information to practice my R skills but i'm a bit stuck. i have a data frame wit the Columns Temp, Day and month. Now i need try to plot the data frame with the days on the X-axis and the temp on the Y axis and the month should determine which shape/colour the dot gets( so month 5 is red, month 6 is green. etc). i searched Stack Overfow for the answer and tried some of the code but didn't work out very well.

i tried for instance:

set.seed(1); plot(x$Day, x$Temp, pch=paste(5:9)) but gave me random numbers on the chart ( sorry can't upload image, reputation is too low ...)

as i set the numbers in paste to 1:5 all the numbers in the plot changed so i think that puts in a random number (?). the factor is:

f <- factor(m)

with m as the months (5:9)

    Day Temp Month
20   20   62     5
21   21   59     5
22   22   73     5
23   23   61     5
24   24   61     5
25   25   57     5
26   26   58     5
27   27   57     5
28   28   67     5
29   29   81     5
30   30   79     5
31   31   76     5
32    1   78     6
33    2   74     6
34    3   67     6
35    4   84     6
36    5   85     6
37    6   79     6
38    7   82     6
39    8   87     6
40    9   90     6
41   10   87     6
42   11   93     6
43   12   92     6
44   13   82     6
45   14   80     6
46   15   79     6
47   16   77     6
48   17   72     6
49   18   65     6

so the days in the plot with the month number 5 in the data frame need to have the number 5 in the plot, month number 6 days need number 6 in the plot and that for all 5 months.

in short: I'm trying to get different dots/ numbers for each month which is located in a factor.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
Daslicey
  • 111
  • 3
  • Try sharing some data. [See here for tips (use `dput`!)](http://stackoverflow.com/q/5963269/903061). Enter `paste(5:9)` in you console to see what it outputs. (Hint: it's not random.) Also, when you say "the factor is `f <- factor(m)`, but you need to define `m` first for it to make sense. – Gregor Thomas Mar 05 '14 at 22:39
  • They aren't random. You are telling it to plot the numbers 5,6,7,8, and 9 where your points should be going. – rawr Mar 05 '14 at 22:44
  • yes sorry. m are the 5 months in where the data is aquired, so month 5 till month 9 ( so in total 153 days which means 3x a 31 day month and 2 times a 30 day month) – Daslicey Mar 05 '14 at 22:45
  • try `pchvec <- 5:9; plot(...,pch=pchvec[as.numeric(f)])` – Ben Bolker Mar 05 '14 at 22:50
  • @ben Bolker that seems to work but the dots are now figures so i can't make out which figure is what month.. is there a way to turn those figures to numbers? – Daslicey Mar 05 '14 at 22:59
  • `as.character()`, see answer below. – Ben Bolker Mar 05 '14 at 23:04

1 Answers1

0

How about:

set.seed(101)
d <- data.frame(x=rnorm(100),y=rnorm(100),m=sample(5:9,replace=TRUE))
pchvec <- as.character(5:9)
with(d,plot(x,y,pch=pchvec[as.numeric(factor(m))]))

Or in a couple more steps, without the with() magic:

m.ind <- as.numeric(factor(d$m))
pchvec2 <- pchvec[m.ind]
plot(y~x,data=d,pch=pchvec2)

Or in your case just

with(x,plot(Day,Temp,pch=as.character(Month)))

should work.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453