Gerrit Eichner
2012-Jun-27 11:16 UTC
[R] formula version of sunflowerplot() fails when axis label specified
Hello, R-help, does anybody have already a work-around for the problem that the formula version of sunflowerplot() throws an error when provided with a value for xlab (or ylab) different from NULL:> sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A")Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data = iris, : variable lengths differ (found for '(xlab)') And are you -- the one with the work-around -- willing to share it? :) Best regards -- Gerrit --------------------------------------------------------------------- Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner
sina rueeger
2012-Jun-27 14:09 UTC
[R] formula version of sunflowerplot() fails when axis label specified
Hi Gerrit I did the following: access the function via graphics:::sunflowerplot.formula, and then commented some lines to see, where the problem lies. I guess that it is the chunk with "mf <- eval(m, parent.frame())", but to be honest, I do not know why. My workaround is as follows: sunflowerplot.formula2 <- function (formula, data = NULL, xlab = NULL, ylab = NULL, ..., subset, na.action = NULL) { if (missing(formula) || (length(formula) != 3L)) stop("formula missing or incorrect") ## >> replaced all the part where "m" is defined, and write instead: mf <- model.frame(formula, data) require(stats, quietly = TRUE) if (NCOL(mf) != 2L) stop("'formula' should specify exactly two variables") if (is.null(xlab)) xlab <- names(mf)[2L] if (is.null(ylab)) ylab <- names(mf)[1L] sunflowerplot(mf[[2L]], mf[[1L]], xlab = xlab, ylab = ylab, ...) } I know, the solution is cheap and not as sophisticated as the original function, but it does the job (for your example). Best, Sina -- View this message in context: http://r.789695.n4.nabble.com/formula-version-of-sunflowerplot-fails-when-axis-label-specified-tp4634608p4634626.html Sent from the R help mailing list archive at Nabble.com.
David L Carlson
2012-Jun-27 15:12 UTC
[R] formula version of sunflowerplot() fails when axis label specified
It seems to be a bug when you specify an x-label. # No xlab= works fine, but uses variable names to label x and # y axes> sunflowerplot(Sepal.Length~Sepal.Width, data=iris)# These all throw the error message> sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="A") > sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="") > sunflowerplot(Sepal.Length~Sepal.Width, data=iris, xlab="Sunflowers")The work around would be to set up the plot and with plot() and then add the sunflowerplot> x <- range(iris$Sepal.Width) > y <- range(iris$Sepal.Length) > plot(x, y, type="n", xlab="A", ylab="") > sunflowerplot(Sepal.Length~Sepal.Width, data=iris, add=TRUE)---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Gerrit Eichner > Sent: Wednesday, June 27, 2012 6:17 AM > To: R-Help > Subject: [R] formula version of sunflowerplot() fails when axis label > specified > > Hello, R-help, > > does anybody have already a work-around for the problem that the > formula > version of sunflowerplot() throws an error when provided with a value > for > xlab (or ylab) different from NULL: > > > sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A") > Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data > = iris, : > variable lengths differ (found for '(xlab)') > > And are you -- the one with the work-around -- willing to share it? :) > > Best regards -- Gerrit > > --------------------------------------------------------------------- > Dr. Gerrit Eichner Mathematical Institute, Room 212 > gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen > Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany > Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
David L Carlson
2012-Jun-27 15:15 UTC
[R] formula version of sunflowerplot() fails when axis label specified
Or just avoid the formula version:> with(iris, sunflowerplot(Sepal.Width, Sepal.Length, xlab="A"))---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Gerrit Eichner > Sent: Wednesday, June 27, 2012 6:17 AM > To: R-Help > Subject: [R] formula version of sunflowerplot() fails when axis label > specified > > Hello, R-help, > > does anybody have already a work-around for the problem that the > formula > version of sunflowerplot() throws an error when provided with a value > for > xlab (or ylab) different from NULL: > > > sunflowerplot( Sepal.Length ~ Sepal.Width, data = iris, xlab = "A") > Error in model.frame.default(formula = Sepal.Length ~ Sepal.Width, data > = iris, : > variable lengths differ (found for '(xlab)') > > And are you -- the one with the work-around -- willing to share it? :) > > Best regards -- Gerrit > > --------------------------------------------------------------------- > Dr. Gerrit Eichner Mathematical Institute, Room 212 > gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen > Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany > Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Dear R Users, I ask the following question in order to learn more on the use of 'assign' and 'paste' functions and for loop; otherwise what I am asking could be solved by binding the various first differences of the series using the 'ts.union' operator. The problem is: I have several variables in my dataset, which I should model dynamically - i.e., with lags of differences of the time series in the regression equation. Consequently, I used a loop (on which I got help from Sarah Goslee) to difference them. Using the same variable as in my previous post, the first differences are computed as follows:> DCred1 <- diff(Cred, difference=1) #call this the FIRST LOOP > for(i in 2:5){+ print(assign(paste("DCred", i, sep=""), diff(get(paste("DCred", i-1, + sep="")), difference=1))) + } NB: I converted the series to time series using 'ts' before differencing. Now after obtaining first differences, I try to use the 'assign' and 'paste' function in two 'for loops' to adjust the lengths of lagged terms (DCred1, DCred2, etc) to have the same length to be used in a regression model. My code is: for(i in 1:3){ #call this the SECOND LOOP for(j in 3:1){ print(assign(paste("Dcre", i, sep=""), get(paste("DCred", i, sep=""))[j:(136-i)])) } } NB: The length of the original series Cred (before differencing) equals 136. This is why the last term in the assign expression is (136 - i). NB: I run this loop after running the first loop which computes the first differences, so that the 'get' operator obtains DCred1, DCred2, etc from the results of the first loop. With this code, I expected to get DCred1 (whose length is 135) and adjust its length to equal that of DCred3 (which is 133) and call the result 'Dcre1'. Similarly, I intended to get DCred2 (whose length is 134) and adjust its length to 133 (the same as the length of DCred3) and call it Dcre2. Lastly, I would get DCred3 of length 133 and call it Dcre3, with length 133. When I run this code, it runs succesfully. However, when I then check the lengths of Dcre1, ..., Dcre3, I get:> length(Dcre1)[1] 135> length(Dcre2)[1] 134> length(Dcre3)[1] 133 This shows that my code did NOT achieve the intended outcome. Please assist. Thanks. Lexi [[alternative HTML version deleted]]