Dear useRs, I'm trying to plot spray quantities against dates, and label the points on the plot. Basically quite simple, but sometimes two chemicals have been used and are listed in separate rows in the table as below; then the labels are written on top of each other.> spraySprayDate PD Trt Qwater Qai 1 2005-03-09 Spidermites Pegasus 1300 1.04 2 2005-03-10 Powdery MKP 800 0.40 3 2005-03-10 Powdery Nimrod 250 EC 800 2.40 4 2005-03-12 Spidermites Pegasus 1300 1.04 5 2005-03-16 Spidermites Pegasus 1300 1.04 6 2005-03-17 Powdery Stroby 50 WG 800 0.40 7 2005-03-21 Spidermites Dynamec 1.8 EC 1250 0.62 8 2005-03-21 Spidermites Apollo 50 SC 1250 0.62 9 2005-03-30 Whiteflies Pegasus 1000 0.60 I came up with the work around below which gave me my plot for some of the data (despite warnings). The idea was to split the second chemical for each date into a separate file and plot the text a little higher:> spray1 <- spray[spray$PD=="Spidermites", ] > spray2 <- spray1[spray1$SprayDate==unique(spray1$SprayDate), ]Warning message: longer object length is not a multiple of shorter object length in: spray1$SprayDate == unique(spray1$SprayDate)> spray3 <- spray1[spray1$SprayDate!=unique(spray1$SprayDate), ]Warning message: longer object length is not a multiple of shorter object length in: spray1$SprayDate != unique(spray1$SprayDate)> plot(as.Date(spray2$SprayDate), spray2$Qwater,+ xlim = c(as.Date("2005-03-08"), as.Date("2005-03-24")), ylim = c(0,1500)) NULL> text(as.Date(spray2$SprayDate), spray2$Qwater,+ labels = spray2$Trt, pos = 4, srt = 45) NULL> text(as.Date(spray3$SprayDate), spray3$Qwater+100,+ labels = spray3$Trt, pos = 4, srt = 45) NULL But it didn't work for everything:> spray1 <- spray[spray$PD=="Powdery", ] > spray2 <- spray1[spray1$SprayDate==unique(spray1$SprayDate), ]Warning message: longer object length is not a multiple of shorter object length in: spray1$SprayDate == unique(spray1$SprayDate)> spray2SprayDate PD Trt Qwater Qai 2 2005-03-10 Powdery MKP 800 0.4> spray3 <- spray1[spray1$SprayDate!=unique(spray1$SprayDate), ]Warning message: longer object length is not a multiple of shorter object length in: spray1$SprayDate != unique(spray1$SprayDate)> spray3SprayDate PD Trt Qwater Qai 3 2005-03-10 Powdery Nimrod 250 EC 800 2.4 6 2005-03-17 Powdery Stroby 50 WG 800 0.4 Does anyone have a better way of splitting the second reading for each date off, or another way of printing two labels for one plotted point? Any help would be greatly appreciated. cheers, Lisbeth [[alternative HTML version deleted]]
"Lisbeth Riis" <lriis at scarabconsult.com> writes:> Dear useRs, > > I'm trying to plot spray quantities against dates, and label the points > on the plot. Basically quite simple, but sometimes two chemicals have > been used and are listed in separate rows in the table as below; then > the labels are written on top of each other. > > > spray > SprayDate PD Trt Qwater Qai > 1 2005-03-09 Spidermites Pegasus 1300 1.04 > 2 2005-03-10 Powdery MKP 800 0.40 > 3 2005-03-10 Powdery Nimrod 250 EC 800 2.40 > 4 2005-03-12 Spidermites Pegasus 1300 1.04 > 5 2005-03-16 Spidermites Pegasus 1300 1.04 > 6 2005-03-17 Powdery Stroby 50 WG 800 0.40 > 7 2005-03-21 Spidermites Dynamec 1.8 EC 1250 0.62 > 8 2005-03-21 Spidermites Apollo 50 SC 1250 0.62 > 9 2005-03-30 Whiteflies Pegasus 1000 0.60 > > I came up with the work around below which gave me my plot for some of > the data (despite warnings). The idea was to split the second chemical for each date into > a separate file and plot the text a little higher: > > > spray1 <- spray[spray$PD=="Spidermites", ] > > spray2 <- spray1[spray1$SprayDate==unique(spray1$SprayDate), ] > Warning message: > longer object length > is not a multiple of shorter object length in: spray1$SprayDate == unique(spray1$SprayDate)I don't think this does what I think you think it does... Consider> x <- c(1,1,2,3) > x == unique(x)[1] TRUE FALSE FALSE FALSE Warning message: longer object length is not a multiple of shorter object length in: x == unique(x) unique(x) is c(1,2,3) so you end up with c(1,1,2,3)==c(1,2,3,1) Were you perhaps intending> !duplicated(x)[1] TRUE FALSE TRUE TRUE ? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Another option is to combine the sprays used as a single string, and plot that directly, rather than subsetting the data. This has the advantages of not having to worry about how much to offset the second label by, and should also work if you got more than two sprays per day. spray$SprayDate <- as.Date(spray$SprayDate) spray.sub <- spray[spray$PD=="Spidermites",] ## Collapse, so there is only a single row per date spray.col <- spray.sub[unique(tapply(spray.sub$SprayDate, spray.sub$SprayDate)),] ## And paste together any treatments used on a single day, separated ## by a newline txt <- tapply(spray.sub$Trt, spray.sub$SprayDate, paste, collapse="\n") plot(spray.col$SprayDate, spray.col$Qwater, xlim=c(as.Date("2005-03-08"), as.Date("2005-03-24")), ylim=c(0,1500)) text(spray.col$SprayDate, spray.col$Qwater, txt, pos=4, srt=45) If you wanted the order of the chemicals used changed, you could insert a new function into the tapply() call, e.g. tapply(spray.sub$Trt, spray.sub$SprayDate, function(x) paste(rev(x), collapse="\n")) Cheers, Rich On Apr 10, 2005 8:24 PM, Lisbeth Riis <lriis at scarabconsult.com> wrote:> Dear useRs, > > I'm trying to plot spray quantities against dates, and label the points > on the plot. Basically quite simple, but sometimes two chemicals have > been used and are listed in separate rows in the table as below; then > the labels are written on top of each other....> Does anyone have a better way of splitting the second reading for each > date off, or another way of printing two labels for one plotted point?-- Rich FitzJohn rich.fitzjohn <at> gmail.com | http://homepages.paradise.net.nz/richa183 You are in a maze of twisty little functions, all alike