arun
2013-Mar-07 03:12 UTC
[R] multiple plots and looping assistance requested (revised codes)
Hi Irucka,
I tried it and was able to plot it without any errors.? Here, your code
indicates you need two lines. temper[[i]][1]
?temper[[1]][1] # which is the column 1.
? Month
1???? 1
2???? 2
3???? 3
?temper[[1]][2]
#? Data1
#1?? 1.5
#2? 12.3
#3? 11.4
Suppose I use names(temper) instead of seq_along(temper)
pdf("irucka.pdf")
?lapply(names(temper),function(i)
{plot(as.matrix(temper[[i]][1]),as.matrix(temper[[i]][2]),main="Fluxmaster
versus EGRET/WRTDS \n Seasonal FLux Sum",sub=i,xlab="Calendar Year
Timesteps",ylab="Total Flux (kg/season)"); lines(temper[[i]][1]);
lines(temper[[i]][2])})
dev.off()
which may not be the one you wanted.
A.K.
________________________________
From: Irucka Embry <iruckaE at mail2world.com>
To: smartpink111 at yahoo.com
Sent: Wednesday, March 6, 2013 9:32 PM
Subject: Re: [R] multiple plots and looping assistance requested (revised codes)
Hi Arun, I was only able to plot by changing from names(temper) to
seq_along(temper) and by providing a numeric column entry for the [i] index. My
problem has been trying to figure out how to index each column by skipping
column 1. Do you have any suggestions?
> tempernow <- lapply(seq_along(temper),function(i)
{plot(as.matrix(temp[[i]][1]), as.matrix(temp[[i]][2]), 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]][2])})
Error in xy.coords(x, y) :
(list) object cannot be coerced to type 'double'
Thank you.
Irucka
-------------- next part --------------
A non-text attachment was scrubbed...
Name: irucka.pdf
Type: application/pdf
Size: 5735 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20130306/49891a78/attachment.pdf>
arun
2013-Mar-07 03:57 UTC
[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: Irucka2.pdf
Type: application/pdf
Size: 9785 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20130306/2e26b39f/attachment.pdf>
arun
2013-Mar-07 05:38 UTC
[R] multiple plots and looping assistance requested (revised codes)
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>
Irucka Embry
2013-Mar-07 06:50 UTC
[R] multiple plots and looping assistance requested (revised codes)
Hi Arun, that worked perfectly for the smaller data set, but it has
failed to plot in the whole data set where I have missing columns
(either 1 or 2 columns missing).
How can I ask any NAs not to be plotted? I have attempted to use
na.omit(temper2[[i]]), but it did not work. See below:
pdf("SeasonalFluxComparisonDataSet.pdf")
par(mfrow=c(1,2))
lapply(names(temper2),function(i)
lapply(na.omit(temper2[[i]]),function(x)
{plot(x[,1],x[,2],main="Seasonal Flux
Sum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year
Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])}))
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning
-Inf> dev.off()
> options("na.action")
$na.action
[1] "na.omit"
Thank you for your quick responses.
Irucka
<-----Original Message-----> >From: arun [smartpink111@yahoo.com]
>Sent: 3/6/2013 11:38:37 PM
>To: iruckaE@mail2world.com
>Cc: r-help@r-project.org
>Subject: Re: [R] multiple plots and looping assistance requested
(revised codes)>
>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@mail2world.com>
>To: smartpink111@yahoo.com
>Cc: r-help@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
>
<span id=m2wTl><p><font face="Arial, Helvetica,
sans-serif" size="2"
style="font-size:13.5px">_______________________________________________________________<BR>Get
the Free email that has everyone talking at <a href=http://www.mail2world.com
target=new>http://www.mail2world.com</a><br> <font
color=#999999>Unlimited Email Storage – POP3 – Calendar
– SMS – Translator – Much
More!</font></font></span>
[[alternative HTML version deleted]]
Irucka Embry
2013-Mar-08 06:15 UTC
[R] multiple plots and looping assistance requested (revised codes)
Hi Arun, thanks for the responses. 1) I added more data to the data set located at http://stackoverflow.com/questions/11548368/making-multiple-plots-in-r-f rom-one-textfile to more resemble the actual data set that I have. The example data set is called tempdata. I have used the most recent code suggestions for plotting on the example data set and I am receiving the same error message as with the full data set. I figured that the plot function is stopping at Atlantacitydata and it is not going further because Atlantacitydata is a blank plot. How do I get plot to keep plotting after encountering a blank plot in a multi plot series? I have looked online and I have not found an answer yet. # tempdata is the simpler data set that is similar to the real data set> dput(tempdata)structure(list(`:Bostoncitydata` = structure(list(Month = c(1L, 2L, 3L, NA), Data1 = c(1.5, 12.3, 11.4, NA), Data2 = c(9.1342, 12.31, 3.5, NA)), .Names = c("Month", "Data1", "Data2"), class "data.frame", row.names = c(NA, -4L)), `:Chicagocitydata` = structure(list(Month = c(1L, 2L, 3L, 4L, 5L, NA), Data1 = c(1.52, 12.63, 20.34, 12.83, 3.34, NA ), Data2 = c(19.41342, 13.031, 0.80021, 12.63104, 19.20021, NA )), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA, -6L)), `:NewYorkcitydata` = structure(list(Month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, NA), Data1 = c(NA, NA, NA, NA, NA, NA, NA, NA), Data2 = c(3.1342, 1.31, 13.5, 1.31, 2.40021, 0.25, 26.3, NA)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA, -8L)), `:Philadelphiacitydata` = structure(list(Month = c(1L, 2L, 3L, 4L, 5L, 6L, NA), Data1 = c(1.65, 11.63, 5.0434, 13.73, 3.0234, 34.209, NA), Data2 = c(2.61342, 16.331, 19.040021, 17.831, 10.1010021, 3.80742, NA)), .Names = c("Month", "Data1", "Data2" ), class = "data.frame", row.names = c(NA, -7L)), `:Atlantacitydata` structure(list( Month = c(1L, 2L, 3L, NA), Data1 = c(NA, NA, NA, NA), Data2 = c(NA, NA, NA, NA)), .Names = c("Month", "Data1", "Data2"), class "data.frame", row.names = c(NA, -4L)), `:Baltimorecitydata` = structure(list(Month = 1:2, Data1 c(11.325, 32.433), Data2 = c(49.71342, 52.4031)), .Names = c("Month", "Data1", "Data2"), class = "data.frame", row.names = c(NA, -2L))), .Names c(":Bostoncitydata", ":Chicagocitydata", ":NewYorkcitydata", ":Philadelphiacitydata", ":Atlantacitydata", ":Baltimorecitydata"))> tempdata read.funkyfile("/home/cptr/IE2/Documents/USGS-2012-Work/SPARROW-Summer-2012-Work/EGRET/calibration/datamore.csv", "Header", header=TRUE, sep="\t")> tempdata1<-lapply(tempdata,function(x) x[complete.cases(x),]) > tempdata2<-lapply(tempdata1,function(temperNew)lapply(names(temperNew)[-1], function(i){x1<-cbind(temperNew[,1],temperNew[,i]); colnames(x1)<- c("CYEAR_DECIMAL",i);x1}))>pdf("/home/cptr/IE2/Documents/USGS-2012-Work/SPARROW-Summer-2012-Work/EG RET/calibration/Calibration_Results/Nitrogen/Graphics/seasonalFluxCompar isonDataSet.pdf")> par(mfrow=c(1,2)) > lapply(names(tempdata2),function(i) lapply(tempdata2[[i]],function(x){plot(x[,1],x[,2],main="Seasonal Flux Sum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])})) Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf 2) I attempted the code that you sent in your recent response, but there were errors in both cases. Please see below:> temp1<-lapply(temp,function(x) x[complete.cases(x),]) #NA values areremoved> temp1[[2]]<- temp1[[2]][1:2] #Some columns are removed > temp1[[3]]<- temp1[[3]][c(1,3)] # Some columns removed > temp3<-temp1[lapply(temp1,ncol)==3] > temp3New<-lapply(temp3,function(x) lapply(names(x)[-1],function(i){x1<-cbind(temp3[,1],temp3[,i]); colnames(x1)<- c("CYEAR_DECIMAL",i);x1})) Error in temp3[, 1] : incorrect number of dimensions> temp2<-temp1[lapply(temp1,ncol)==2] > temp2New<-lapply(temp2,function(x) lapply(names(x)[-1],function(i){x1<-cbind(temp2[,1],temp2[,i]); colnames(x1)<- c("CYEAR_DECIMAL",i);x1})) Error in temp2[, 1] : incorrect number of dimensions Thank you. Irucka <-----Original Message----->>From: arun [smartpink111@yahoo.com] >Sent: 3/7/2013 9:02:22 AM >To: iruckaE@mail2world.com >Cc: r-help@r-project.org >Subject: Re: [R] multiple plots and looping assistance requested(revised codes)> >Hi Irucka, > >Regarding the first question. > >If you look at the ouput of temp1, it already strips off any NA thatwas left in the columns. It is always to>give an example dataset that is similar to the real dataset. > >Here, I am guessing the situation is similar to this: >temp1<-lapply(temp,function(x) x[complete.cases(x),]) #NA values areremoved>temp1[[2]]<- temp1[[2]][1:2] #Some columns are removed > temp1[[3]]<- temp1[[3]][c(1,3)] # Some columns removed >#Subsetting based on 3 columns and 2 columns >#3 columns > >temp3<-temp1[lapply(temp1,ncol)==3] >temp3New<-lapply(temp3,function(x) lapply(names(x)[-1],function(i){x1<-cbind(temp3[,1],temp3[,i]);>colnames(x1)<- c("CYEAR_DECIMAL",i);x1})) > >pdf("Irucka3.pdf") >par(mfrow=c(1,2)) >lapply(names(temp3New),function(i) lapply(temp3New[[i]],function(x){plot(x[,1],x[,2],main="Fluxmaster>versus EGRET/WRTDS \n Seasonal FLuxSum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year>Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])})) >dev.off() ># 2 columns >temp2<-temp1[lapply(temp1,ncol)==2] >temp2New<-lapply(temp2,function(x) lapply(names(x)[-1],function(i){x1<-cbind(temp2[,1],temp2[,i]);>colnames(x1)<- c("CYEAR_DECIMAL",i);x1})) > >pdf("Irucka2.pdf") >par(mfrow=c(1,1)) >lapply(names(temp2New),function(i) lapply(temp2New[[i]],function(x){plot(x[,1],x[,2],main="Fluxmaster>versus EGRET/WRTDS \n Seasonal FLuxSum",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@mail2world.com> >To: smartpink111@yahoo.com >Cc: r-help@r-project.org >Sent: Thursday, March 7, 2013 1:50 AM >Subject: Re: [R] multiple plots and looping assistance requested(revised codes)> > >Hi Arun, that worked perfectly for the smaller data set, but it hasfailed to plot in the whole data set>where I have missing columns (either 1 or 2 columns missing). > >How can I ask any NAs not to be plotted? I have attempted to usena.omit(temper2[[i]]), but it did not>work. See below: > >pdf("SeasonalFluxComparisonDataSet.pdf") >par(mfrow=c(1,2)) >lapply(names(temper2),function(i)lapply(na.omit(temper2[[i]]),function(x)>{plot(x[,1],x[,2],main="Seasonal FluxSum",sub=paste(i,colnames(x)[2],sep="_"),xlab="Calendar Year>Timesteps",ylab="Total Flux (kg/season)");lines(x[,1],x[,2])})) >Error in plot.window(....) : need finite 'xlim' values >In addition: Warning messages: >1: In min(x) : no non-missing arguments to min; returning Inf >2: In max(x) : no non-missing arguments to max; returning -Inf >3: In min(x) : no non-missing arguments to min; returning Inf >4: In max(x) : no non-missing arguments to max; returning -Inf >> dev.off() > >> options("na.action") >$na.action >[1] "na.omit" > >Thank you for your quick responses. > >Irucka<span id=m2wTl><p><font face="Arial, Helvetica, sans-serif" size="2" style="font-size:13.5px">_______________________________________________________________<BR>Get the Free email that has everyone talking at <a href=http://www.mail2world.com target=new>http://www.mail2world.com</a><br> <font color=#999999>Unlimited Email Storage – POP3 – Calendar – SMS – Translator – Much More!</font></font></span> [[alternative HTML version deleted]]
arun
2013-Mar-08 15:31 UTC
[R] multiple plots and looping assistance requested (revised codes)
HI Irucka,
The situation is slightly different here.? I was under the assumption that the
list elements sometimes had one two columns or three columns.? Here, all the
columns are present, but some with entire rows of missing values.? Also, there
was a mistake in the code when I updated the code.
If the real data is similar to the one posted here, I guess it should work.
temp<- structure(list(`:Bostoncitydata` = structure(list(Month = c(1L,
2L, 3L, NA), Data1 = c(1.5, 12.3, 11.4, NA), Data2 = c(9.1342,
12.31, 3.5, NA)), .Names = c("Month", "Data1",
"Data2"), class = "data.frame", row.names = c(NA,
-4L)), `:Chicagocitydata` = structure(list(Month = c(1L, 2L,
3L, 4L, 5L, NA), Data1 = c(1.52, 12.63, 20.34, 12.83, 3.34, NA
), Data2 = c(19.41342, 13.031, 0.80021, 12.63104, 19.20021, NA
)), .Names = c("Month", "Data1", "Data2"), class =
"data.frame", row.names = c(NA,
-6L)), `:NewYorkcitydata` = structure(list(Month = c(1L, 2L,
3L, 4L, 5L, 6L, 7L, NA), Data1 = c(NA, NA, NA, NA, NA, NA, NA,
NA), Data2 = c(3.1342, 1.31, 13.5, 1.31, 2.40021, 0.25, 26.3,
NA)), .Names = c("Month", "Data1", "Data2"), class
= "data.frame", row.names = c(NA,
-8L)), `:Philadelphiacitydata` = structure(list(Month = c(1L,
2L, 3L, 4L, 5L, 6L, NA), Data1 = c(1.65, 11.63, 5.0434, 13.73,
3.0234, 34.209, NA), Data2 = c(2.61342, 16.331, 19.040021, 17.831,
10.1010021, 3.80742, NA)), .Names = c("Month", "Data1",
"Data2"
), class = "data.frame", row.names = c(NA, -7L)), `:Atlantacitydata` =
structure(list(
Month = c(1L, 2L, 3L, NA), Data1 = c(NA, NA, NA, NA), Data2 = c(NA,
NA, NA, NA)), .Names = c("Month", "Data1",
"Data2"), class = "data.frame", row.names = c(NA,
-4L)), `:Baltimorecitydata` = structure(list(Month = 1:2, Data1 = c(11.325,
32.433), Data2 = c(49.71342, 52.4031)), .Names = c("Month",
"Data1",
"Data2"), class = "data.frame", row.names = c(NA, -2L))),
.Names = c(":Bostoncitydata",
":Chicagocitydata", ":NewYorkcitydata",
":Philadelphiacitydata",
":Atlantacitydata", ":Baltimorecitydata"))
temp1<-lapply(temp,function(x) {x1<-x[,colSums(is.na(x))!=nrow(x)];
if(is.data.frame(x1)) x1[complete.cases(x1),] else x1[complete.cases(x1)] })
# temp1<-lapply(temp1,function(x) x[is.data.frame(x)])
#3 columns subset
temp3<-temp1[lapply(temp1,length)==3]
?temp3New<-lapply(temp3,function(x) lapply(names(x)[-1], function(i)
{x1<-cbind(x[,1],x[,i]);colnames(x1)<- c("Month",i);x1} ))
pdf("Irucka3.pdf")
par(mfrow=c(1,2))
lapply(names(temp3New),function(i) lapply(temp3New[[i]],function(x)
{plot(x[,1],x[,2],main="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()
#2 columns subset
temp2<-temp1[lapply(temp1,length)==2]
temp2New<-lapply(temp2,function(x) lapply(names(x)[-1], function(i)
{x1<-cbind(x[,1],x[,i]);colnames(x1)<- c("Month",i);x1} ))
?
pdf("Irucka2.pdf")
par(mfrow=c(1,1))
lapply(names(temp2New),function(i) lapply(temp2New[[i]],function(x)
{plot(x[,1],x[,2],main="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()
Please let me know if there are problems.
A.K.
________________________________
From: Irucka Embry <iruckaE at mail2world.com>
To: smartpink111 at yahoo.com
Cc: r-help at r-project.org
Sent: Friday, March 8, 2013 1:15 AM
Subject: Re: [R] multiple plots and looping assistance requested (revised codes)
Hi Arun, thanks for the responses.
1) I added more data to the data set located at
http://stackoverflow.com/questions/11548368/making-multiple-plots-in-r-from-one-textfile
to more resemble the actual data set that I have. The example data set is called
tempdata. I have used the most recent code suggestions for plotting on the
example data set and I am receiving the same error message as with the full data
set.
I figured that the plot function is stopping at Atlantacitydata and it is not
going further because Atlantacitydata is a blank plot.
How do I get plot to keep plotting after encountering a blank plot in a multi
plot series?
I have looked online and I have not found an answer yet.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Irucka2.pdf
Type: application/pdf
Size: 4880 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20130308/9d271204/attachment.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Irucka3.pdf
Type: application/pdf
Size: 8503 bytes
Desc: not available
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20130308/9d271204/attachment-0001.pdf>