yarrr icon indicating copy to clipboard operation
yarrr copied to clipboard

problems when columns identified in a loop

Open oscci opened this issue 6 years ago • 1 comments

I've got a script that works fine with individual pirate plots. I want to do 3 pirateplots side by side, so I am specifying the column name for x variable as mycol[i]. This works fine with a beanplot. It also works if there's just one pirateplot. Using the loop with pirateplot, though, I get the error message Error in var(y) : 'x' is NULL

Here is the script - I think it's the initial command to select data that throws it, rather than the use of i index in later bits of script.

for (i in 1:3){ pirateplot(all.data[,mycol[i]]~ all.data$randomtwininclude, point.o = .5, bar.f.o=.0, inf.f.o=.2, bean.b.o=.5, jitter=.1, ylab=mypheno[i], ylim=c(myylim[i,1],myylim[i,2]), main=paste0(mypheno[i]), data=all.data ) }

oscci avatar May 05 '18 10:05 oscci

Hi there @oscci , the problem is in how you're specifying the formula. Formulas should only contained unquoted names of variables. In your code, you're including vectors in the formula -- this won't work.

If you want to create plots side by side, you could consider reshaping your data so that your 3 columns are represented as rows in a new variable called group (see the gather() function in the tidyverse https://tidyr.tidyverse.org/reference/gather.html) . Then, you can include the new variable as a second dependent variable in your formula.

# Get latest version from github to get the beside argument
devtools::install_github("ndphillips/yarrr", build_vignettes = TRUE)

library(yarrr)
library(tidyverse)

pirateplot(formula = weight ~ Diet + Time, 
                 data = ChickWeight %>% filter(Time < 5), 
                 beside = FALSE)

This will return the following plot:

image

ndphillips avatar May 09 '18 11:05 ndphillips