Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top