[R] multiple plots and looping assistance requested (revised codes)
arun
smartpink111 at yahoo.com
Thu Mar 7 06:38:37 CET 2013
HI,
Try this:
temp1<-lapply(temp,function(x) x[complete.cases(x),])
temp2<-lapply(temp1,function(tempNew) lapply(names(tempNew)[-1], function(i){x1<-cbind(tempNew[,1],tempNew[,i]); colnames(x1)<- c("CYEAR_DECIMAL",i);x1}))
pdf("Irucka3.pdf")
par(mfrow=c(1,2))
lapply(names(temp2),function(i) lapply(temp2[[i]],function(x) {plot(x[,1],x[,2],main="Fluxmaster versus EGRET/WRTDS \n Seasonal FLux Sum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])}))
dev.off()
A.K.
________________________________
From: Irucka Embry <iruckaE at mail2world.com>
To: smartpink111 at yahoo.com
Cc: r-help at r-project.org
Sent: Wednesday, March 6, 2013 11:49 PM
Subject: Re: [R] multiple plots and looping assistance requested (revised codes)
Hi Arun, thank you for your assistance.
I have successfully ran your suggested revised code (with some minor changes). Thank-you very much!
Is there a way to print either "load_00600_W" or "load_00600_F" after "sub = i" for each plot?
For example, can the subtitle be "02143500 load_00600_W" which is included in temp2?
> dput(temp2)
structure(list(`02143500` = structure(list(load_00600_W = structure(c(2000.875,
2001.125, 2001.375, 2001.625, 2001.875, 2002.125, 2002.375, 2002.625,
2002.875, 2003.125, 2003.375, 2003.625, 2003.875, 2004.125, 2004.375,
2004.625, 227675.73764, 92777.682029, 84827.680295, 193298.65669,
170799.05034, 103666.8759, 107485.71333, 213765.87505, 472307.65662,
799500.99994, 754868.43185, 454078.02653, 171521.77777, 265827.90007,
120401.25989, 194000.26057), .Dim = c(16L, 2L), .Dimnames = list(
NULL, c("CYEAR_DECIMAL", "x"))), load_00600_F = structure(c(2000.875,
2001.125, 2001.375, 2001.625, 2001.875, 2002.125, 2002.375, 2002.625,
2002.875, 2003.125, 2003.375, 2003.625, 2003.875, 2004.125, 2004.375,
2004.625, 4202.7437226, 16214.840538, 7371.9290209, 3114.1090754,
2464.1114951, 9380.6352081, 3859.2809055, 901.86146915, 22377.413599,
53563.26564, 148264.35049, 103538.36278, 18142.045363, 14672.031667,
18796.93618, 75313.330193), .Dim = c(16L, 2L), .Dimnames = list(
NULL, c("CYEAR_DECIMAL", "x")))), .Names = c("load_00600_W",
"load_00600_F")), `02169000` = structure(list(load_00600_W = structure(c(2000.875,
2001.125, 2001.375, 2001.625, 2001.875, 2002.125, 2002.375, 2002.625,
2002.875, 2003.125, 2003.375, 2003.625, 2003.875, 2004.125, 2004.375,
2004.625, 61152.4563009734, 3087159.69237187, 83271.1072869206,
78182.9098615795, 10344.3815353876, 2246.4450929669, 989.220241325,
2318.3488271707, 24134.6766402373, 34350.75952138, 39041.6413152116,
28188.925323908, 2281.7849286464, 1756.7094755792, 357.3578073865,
238.1185010405), .Dim = c(16L, 2L), .Dimnames = list(NULL, c("CYEAR_DECIMAL",
"x"))), load_00600_F = structure(c(2000.875, 2001.125, 2001.375,
2001.625, 2001.875, 2002.125, 2002.375, 2002.625, 2002.875, 2003.125,
2003.375, 2003.625, 2003.875, 2004.125, 2004.375, 2004.625, 3940.9979246,
14888.612602, 5551.1377638, 5055.93881, 1689.0656165, 1436.5965696,
849.85774965, 879.76270422, 11452.39104, 62140.01855, 67190.304212,
47562.345621, 3535.1744243, 21923.947402, 2059.0406953, 1685.1384101
), .Dim = c(16L, 2L), .Dimnames = list(NULL, c("CYEAR_DECIMAL",
"x")))), .Names = c("load_00600_W", "load_00600_F")), `02172300` = structure(list(
load_00600_W = structure(c(2001.875, 2002.125, 2002.375,
2002.625, 2002.875, 2003.125, 2003.375, 2003.625, 2003.875,
2004.125, 2004.375, 2004.625, 16150.219744, 18927.53716,
15884.341197, 14102.537116, 11611.582491, 14662.106089, 10576.561683,
7699.1718611, 19837.60763, 22396.338619, 34876.278129, 23822.715775
), .Dim = c(12L, 2L), .Dimnames = list(NULL, c("CYEAR_DECIMAL",
"x"))), load_00600_F = structure(c(2001.875, 2002.125, 2002.375,
2002.625, 2002.875, 2003.125, 2003.375, 2003.625, 2003.875,
2004.125, 2004.375, 2004.625, 899.46439731, 1667.869248,
952.12246929, 543.12231908, 1663.8731209, 3123.5771201, 4338.1307758,
4469.2046052, 2642.983822, 2863.529214, 2131.6573312, 2476.4154125
), .Dim = c(12L, 2L), .Dimnames = list(NULL, c("CYEAR_DECIMAL",
"x")))), .Names = c("load_00600_W", "load_00600_F")), `21SC60WQ.CW-206` = structure(list(
load_00600_W = structure(c(2001.875, 2002.125, 2002.375,
2002.625, 23693281.5300403, 66710782.7389879, 49036165.5298238,
27086129.9582558), .Dim = c(4L, 2L), .Dimnames = list(NULL,
c("CYEAR_DECIMAL", "x"))), load_00600_F = structure(c(2001.875,
2002.125, 2002.375, 2002.625, 145481.6737, 370915.04829,
282387.90945, 179357.3242), .Dim = c(4L, 2L), .Dimnames = list(
NULL, c("CYEAR_DECIMAL", "x")))), .Names = c("load_00600_W",
"load_00600_F"))), .Names = c("02143500", "02169000", "02172300",
"21SC60WQ.CW-206"))
I have included the successful code below:
temp = read.funkyfile("sitesmore.csv", "station_id", header=TRUE, sep="\t")
temp1<-lapply(temp,function(x) x[complete.cases(x),])
temp2<-lapply(temp1,function(tempNew) lapply(tempNew[,-1],function(x) {x1<-cbind(CYEAR_DECIMAL=tempNew[,1],x)}))
pdf("Irucka2.pdf")
par(mfrow=c(1,2))
lapply(names(temp2),function(i) lapply(temp2[[i]],function(x) {plot(x[,1],x[,2],main="Seasonal Flux Sum",sub=i,xlab="Calendar Year Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])}))
dev.off()
Thank you.
Irucka
<-----Original Message----->
>From: arun [smartpink111 at yahoo.com]
>Sent: 3/6/2013 9:57:41 PM
>To: iruckaE at mail2world.com
>Cc: r-help at r-project.org
>Subject: Re: [R] multiple plots and looping assistance requested (revised codes)
>
>
>
>HI Irucka,
>
>Try this:
>temp1<-lapply(temp,function(x) x[complete..cases(x),])
> temp2<-lapply(temp1,function(tempNew) lapply(tempNew[,-1],function(x) {x1<-
>cbind(CYEAR_DECIMAL=tempNew[,1],x)}))
>pdf("Irucka2.pdf")
> par(mfrow=c(1,2))
>lapply(names(temp2),function(i) lapply(temp2[[i]],function(x) {plot(x[,1],x[,2],main="Fluxmaster versus
>EGRET/WRTDS \n Seasonal FLux Sum",sub=i,xlab="Calendar Year Timesteps",ylab="Total Flux
>(kg/season)");lines(x[,1],x[,2])}))
>dev.off()
>A.K.
>________________________________
>From: Irucka Embry <iruckaE at mail2world.com>
>To: smartpink111 at yahoo.com
>Sent: Wednesday, March 6, 2013 5:24 PM
>Subject: Re: [R] multiple plots and looping assistance requested (revised codes)
>
>
>Hi Arun, thanks for the note. Thank you especially for noting the use of the ";" and "{}." I have updated my
>own code and the possible reproducible example with those changes.
>
>The attempt at the reproducible code previously was incorrect so I am borrowing from the data stated in
>the first comment.
>
># attempt at reproducible code using data from http://stackoverflow.com/questions/11548368/making-
>multiple-plots-in-r-from-one-textfile
>> dput(temper)
>structure(list(`:Bostoncitydata` = structure(list(Month = 1:3,
>Data1 = c(1.5, 12.3, 11.4), Data2 = c(9.1342, 12.31, 3.5),
>Data3 = c(8.1231, 1.129, 45.4321)), .Names = c("Month", "Data1",
>"Data2", "Data3"), class = "data.frame", row.names = c(NA, -3L
>)), `:Chicagocitydata` = structure(list(Month = 1:3, Data1 = c(1.5,
>12.3, 0.34), Data2 = c(9.1342, 12.31, 9.0021), Data3 = c(8.1231,
>1.129, 24.98)), .Names = c("Month", "Data1", "Data2", "Data3"
>), class = "data.frame", row.names = c(NA, -3L))), .Names = c(":Bostoncitydata",
>":Chicagocitydata"))
>
>temper = read.funkyfile("data.txt", "Header", header=TRUE, sep="\t")
>
>tempernow <- lapply(names(temper),function(i) {plot(as.matrix(temp[[i]][1]), as.matrix(temp[[i]][-1]),
>main="Fluxmaster versus EGRET/WRTDS \n Seasonal Flux Sum", sub = i, xlab="Calendar Year Timesteps",
>ylab="Total Flux (kg/season)"); lines(temp[[i]][1], temp[[i]][-1])})
>
>
># below is the revised code that I am trying to implement
>> dput(temp)
>structure(list(`02143500` = structure(list(Calendar_Year_Timesteps = c(2000.875,
>2001.125, 2001.375, 2001.625, 2001.875, 2002.125, 2002.375, 2002.625,
>2002.875, 2003.125, 2003.375, 2003.625, 2003.875, 2004.125, 2004.375,
>2004.625, NA), load_00600_W = c(227675.73764, 92777.682029, 84827.680295,
>193298.65669, 170799.05034, 103666.8759, 107485.71333, 213765.87505,
>472307.65662, 799500.99994, 754868.43185, 454078.02653, 171521.77777,
>265827.90007, 120401.25989, 194000.26057, NA), load_00600_F = c(4202.7437226,
>16214..840538, 7371.9290209, 3114.1090754, 2464.1114951, 9380.6352081,
>3859.2809055, 901.86146915, 22377.413599, 53563.26564, 148264.35049,
>103538.36278, 18142.045363, 14672.031667, 18796.93618, 75313.330193,
>NA)), .Names = c("Calendar_Year_Timesteps", "load_00600_W", "load_00600_F"
>), class = "data.frame", row.names = c(NA, -17L)), `02169000` = structure(list(
>Calendar_Year_Timesteps = c(2000.875, 2001.125, 2001.375,
>2001.625, 2001.875, 2002.125, 2002.375, 2002.625, 2002.875,
>2003.125, 2003.375, 2003.625, 2003.875, 2004.125, 2004.375,
>2004.625, NA), load_00600_W = c(61152.4563009734, 3087159.69237187,
>83271.1072869206, 78182.9098615795, 10344.3815353876, 2246.4450929669,
>989.220241325, 2318.3488271707, 24134.6766402373, 34350.75952138,
>39041.6413152116, 28188.925323908, 2281.7849286464, 1756.7094755792,
>357.3578073865, 238.1185010405, NA), load_00600_F = c(3940.9979246,
>14888.612602, 5551.1377638, 5055.93881, 1689.0656165, 1436.5965696,
>849.85774965, 879.76270422, 11452.39104, 62140.01855, 67190.304212,
>47562.345621, 3535.1744243, 21923.947402, 2059.0406953, 1685.1384101,
>NA)), .Names = c("Calendar_Year_Timesteps", "load_00600_W",
>"load_00600_F"), class = "data.frame", row.names = c(NA, -17L
>)), `02172300` = structure(list(CYEAR_Decimal = c(2001.875, 2002.125,
>2002.375, 2002.625, 2002.875, 2003.125, 2003.375, 2003.625, 2003.875,
>2004.125, 2004.375, 2004.625, NA), load_00600_W = c(16150.219744,
>18927.53716, 15884.341197, 14102.537116, 11611.582491, 14662.106089,
>10576.561683, 7699.1718611, 19837.60763, 22396.338619, 34876.278129,
>23822.715775, NA), load_00600_F = c(899.46439731, 1667.869248,
>952.12246929, 543.12231908, 1663.8731209, 3123.5771201, 4338.1307758,
>4469.2046052, 2642.983822, 2863.529214, 2131.6573312, 2476.4154125,
>NA)), .Names = c("CYEAR_Decimal", "load_00600_W", "load_00600_F"
>), class = "data.frame", row.names = c(NA, -13L)), `21SC60WQ.CW-206` = structure(list(
>CYEAR_Decimal = c(2001.875, 2002.125, 2002.375, 2002.625),
>load_00600_W = c(23693281.5300403, 66710782.7389879, 49036165.5298238,
>27086129.9582558), load_00600_F = c(145481.6737, 370915.04829,
>282387.90945, 179357.3242)), .Names = c("CYEAR_Decimal",
>"load_00600_W", "load_00600_F"), class = "data.frame", row.names = c(NA,
>-4L))), .Names = c("02143500", "02169000", "02172300", "21SC60WQ.CW-206"
>))
>
>temp = read.funkyfile("sitesmore.csv", "station_id", header=TRUE, sep="\t")
>
># i have successfully graphed like this for one of the columns, but I need to do this for both columns in all
>of the sites (station_ids):
>plot(as.matrix(temp[[1]][1]), as.matrix(temp[[1]][2]))
>lines(as.matrix(temp[[1]][1]), as.matrix(temp[[1]][2]))
>
># this is the code that I am using to plot through lapply which is still giving me problems
>resnow <- lapply(names(temp),function(i) {plot(as.matrix(temp[[i]][1]), as.matrix(temp[[i]][-1]),
>main="Fluxmaster versus EGRET/WRTDS \n Seasonal Flux Sum", sub = i, xlab="Calendar Year Timesteps",
>ylab="Total Flux (kg/season)"); lines(temp[[i]][1], temp[[i]][-1])})
>Error in xy.coords(x, y, xlabel, ylabel, log) :
>'x' and 'y' lengths differ
>
># I want to have side by side graphs (on the same page) of Calendar_Year_Timesteps as the x axis versus
>load_00600_W as the y axis & Calendar_Year_Timesteps as the x axis versus load_00600_P as the y axis
>for each of the data frames. The graphs will be a scatterplot matrix with a line connecting all of the points.
>
>
>Thank you.
>
>Irucka
>
>
><-----Original Message----->
>>From: arun [smartpink111 at yahoo.com]
>>Sent: 3/6/2013 2:18:52 PM
>>To: iruckaE at mail2world.com
>>Subject: Re: [R] multiple plots and looping assistance requested
>>
>>Hi,
>>Not sure whether I understand it correctly: Your example dataset and code gives me scatterplot matrix
>>
>>
>>set.seed(15)
>>repro1 <- data.frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>>rnorm(16, 4))
>>set.seed(25)
>>repro2 <- data..frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>>rnorm(16, 4))
>>
>>set.seed(181)
>>repro3 <- data.frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>>rnorm(16, 4))
>>
>>set.seed(2052)
>>repro4 <- data.frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>>rnorm(16, 4))
>>
>>reproList <- list(repro1, repro2, repro3, repro4)
>>pdf("Irucka.pdf")
>>lapply(reproList,function(x){plot(x);lines(x)}) #here I am getting the scatterplot.
>>dev.off()
>>
>>#From your code:
>> set.seed(15)
>> repro <- data.frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>rnorm(16,
>>4))
>> reproList <- list(repro, repro, repro, repro)
>>pdf("Irucka2..pdf")
>>lapply(seq_along(repro),function(i) {plot(reproList[[i]]);lines(reproList[[i]])}) #your code didn't had
>closing
>>brackets
>> dev.off()
>>A.K.
>>
>>
>>
>>
>>
>>
>>
>>________________________________
>>From: Irucka Embry <iruckaE at mail2world.com>
>>To: smartpink111 at yahoo.com
>>Sent: Wednesday, March 6, 2013 2:15 PM
>>Subject: Re: [R] multiple plots and looping assistance requested
>>
>>
>>Hey Arun, thanks.
>>
>>I did some back end work in LibreOffice Calc with the previous data set so what I had mentioned in the
>>previous post does not apply here.
>>
>>Part of the data set is included in the object temp. I am working on the small data set first with the code
>>and then I'll use the whole data set.
>>
>>Thank you.
>>
>>Irucka
>>
>>
>>> str(temp)
>>List of 4
>>$ 02143500 :'data.frame': 17 obs. of 3 variables:
>>..$ Calendar_Year_Timesteps: num [1:17] 2000.875 2001.125 2001.375 2001.625 2001.875 ... # the
>>decimals
>>...$ load_00600_W : num [1:17] 227676 92778 84828 193299 170799 ...
>>..$ load_00600_F : num [1:17] 4203 16215 7372 3114 2464 ...
>>$ 02169000 :'data.frame': 17 obs. of 3 variables:
>>..$ Calendar_Year_Timesteps: num [1:17] 2000.875 2001..125 2001.375 2001.625 2001.875 ...
>>..$ load_00600_W : num [1:17] 61152 3087160 83271 78183 10344 ...
>>..$ load_00600_F : num [1:17] 3941 14889 5551 5056 1689 ...
>>$ 02172300 :'data.frame': 13 obs. of 3 variables:
>>..$ CYEAR_Decimal: num [1:13] 2001..875 2002.125 2002.375 2002.625 2002.875 ...
>>..$ load_00600_W : num [1:13] 16150 18928 15884 14103 11612 ...
>>..$ load_00600_F : num [1:13] 899 1668 952 543 1664 ...
>>$ 21SC60WQ.CW-206:'data.frame': 4 obs. of 3 variables:
>>..$ CYEAR_Decimal: num [1:4] 2001.875 2002.125 2002.375 2002.625
>>..$ load_00600_W : num [1:4] 23693282 66710783 49036166 27086130
>>..$ load_00600_F : num [1:4] 145482 370915 282388 179357
>>
>>
>>temp = read.funkyfile("sitesmore.csv", "station_id", header=TRUE, sep="\t")
>>resnow <- lapply(names(temp),function(i) plot(as.matrix(temp[[i]][1]), as.matrix(temp[[i]][-1]),
>>main="Fluxmaster versus EGRET/WRTDS \n Seasonal Flux Sum", sub = i, xlab="Calendar Year
>Timesteps",
>>ylab="Total Flux (kg/season)") lines(temp[[i]][1], temp[[i]][-1]))
>>Error in xy.coords(x, y, xlabel, ylabel, log) :
>>'x' and 'y' lengths differ
>># I want to have side by side graphs (on the same page) of Calendar_Year_Timesteps as the x axis
>versus
>>load_00600_W as the y axis & Calendar_Year_Timesteps as the x axis versus load_00600_P as the y axis
>>for each of the data frames.
>>
>>
>># attempt at a partial reproducible code
>>repro <- as.data.frame(stat1 = rnorm(16, 10), stat2 = rnorm(16, 10), stat3 = rnorm(16,10), stat4 =
>>rnorm(16, 4))
>>reproList <- list(repro, repro, repro, repro)
>>repronow <- lapply(seq_along(repro) ,function(i) plot(reproList[[i]] lines(reproList[[i]])))
>>
>>
>>
>>
>>
>><-----Original Message----->
>>>From: arun [smartpink111 at yahoo.com]
>>>Sent: 3/6/2013 12:53:17 PM
>>>To: iruckaE at mail2world.com
>>>Subject: Re: [R] multiple plots and looping assistance requested
>>>
>>>
>>>
>>>HI Irucka,
>>>I am fine.
>>>Thanks.
>>>
>>>In the link,I couldn't find the dataset. Or is it the one from the previous email.
>>>Arun
>>>
>>>
>>>
>>>
>>>________________________________
>>>From: Irucka Embry <iruckaE at mail2world.com>
>>>To: smartpink111 at yahoo.com
>>>Sent: Wednesday, March 6, 2013 1:46 PM
>>>Subject: Re: [R] multiple plots and looping assistance requested
>>>
>>>
>>>Hi Arun, how are you?
>>>
>>>I have revised my previous 2nd question and created this post here:
>>>http://r.789695.n4.nabble.com/trouble-with-lapply-plot-labels-and-indexing-in-multiple-plots-
>>tp4660512.html
>>>
>>>Thank-you.
>>>
>>>Irucka
>>>
>>><-----Original Message----->
>>>>From: arun [smartpink111 at yahoo.com]
>>>>Sent: 3/5/2013 8:26:10 PM
>>>>To: iruckaE at mail2world.com
>>>>Subject: Re: [R] multiple plots and looping assistance requested
>>>>
>>>>HI Irucka,
>>>>No problem.
>>>>
>>>>What's your second question?
>>>>Arun _______________________________________________________________
Get the Free email that has everyone talking at http://www.mail2world.com
Unlimited Email Storage – POP3 – Calendar – SMS – Translator – Much More!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Irucka3.pdf
Type: application/pdf
Size: 9834 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130306/c456c5dc/attachment.pdf>
More information about the R-help
mailing list