0

Though this problem has been 'solved' many times, it turns out there's always another problem.

Without the print function it runs with no errors, but with it I get the following:

Error in .subset2(x, i) : recursive indexing failed at level 2

Which I'm taking to mean it doesn't like graphs being created in two layers of iteration? Changing the method to 'qplot(whatever:whatever)' has the exact same problem.

It's designed to print a graph for every pairing of the variables I'm looking at. There's too many for them to fit in a singular picture, such as for the pairs function, and I need to be able to see the actual variable names in the axes.

load("Transport_Survey.RData")

variables <- select(Transport, "InfOfReceievingWeather", "InfOfReceievingTraffic", "InfOfSeeingTraffic", "InfWeather.Ice", "InfWeather.Rain", "InfWeather.Wind", "InfWeather.Storm", "InfWeather.Snow", "InfWeather.Cold", "InfWeather.Warm", "InfWeather.DarkMorn", "InfWeather.DarkEve", "HomeParking", "WorkParking", "Disability", "Age", "CommuteFlexibility", "Gender", "PassionReduceCongest")
varnames <- list("InfOfReceivingWeather", "InfOfReceivingTraffic", "InfOfSeeingTraffic", "InfWeather.Ice", "InfWeather.Rain", "InfWeather.Wind", "InfWeather.Storm", "InfWeather.Snow", "InfWeather.Cold", "InfWeather.Warm", "InfWeather.DarkMorn", "InfWeather.DarkEve", "HomeParking", "WorkParking", "Disability", "Age", "CommuteFlexibility", "Gender", "PassionReduceCongest")

counterx = 1
countery = 1

for (a in variables) {
  for (b in variables) {
    print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]],
    xlab=varnames[counterx], ylab=varnames[countery]))+
      geom_point())
    countery = countery+1
  counterx = counterx+1
  }
}

#variables2 <- select(Transport, one_of(InfOfReceivingWeather, InfOfReceivingTraffic, InfOfSeeingTraffic, InfWeather.Ice, InfWeather.Rain, InfWeather.Wind, InfWeather.Storm, InfWeather.Snow, InfWeather.Cold, InfWeather.Warm, InfWeather.DarkMorn, InfWeather.DarkEve, HomeParking, WorkParking, Disability, Age, CommuteFlexibility, Gender, PassionReduceCongest))

Here is a mini-data frame for reference, sampled from the columns I'm using:

structure(list(InfOfReceievingWeather = c(1, 1, 1, 1, 4), InfOfReceievingTraffic = c(1, 
1, 1, 1, 4), InfOfSeeingTraffic = c(1, 1, 1, 1, 4), InfWeather.Ice = c(3, 
1, 3, 5, 5), InfWeather.Rain = c(1, 1, 2, 2, 4), InfWeather.Wind = c(1, 
1, 2, 2, 4), InfWeather.Storm = c(1, 1, 1, 2, 5), InfWeather.Snow = c(1, 
1, 2, 5, 5), InfWeather.Cold = c(1, 1, 1, 2, 5), InfWeather.Warm = c(1, 
1, 1, 1, 3), InfWeather.DarkMorn = c(1, 1, 1, 1, 1), InfWeather.DarkEve = c(1, 
1, 1, 1, 1), HomeParking = c(1, 1, 3, 1, 1), WorkParking = c(1, 
4, 4, 5, 4), Disability = c(1, 1, 1, 1, 1), Age = c(19, 45, 35, 
40, 58), CommuteFlexibility = c(2, 1, 5, 1, 2), Gender = c(2, 
2, 2, 2, 1), PassionReduceCongest = c(0, 0, 2, 0, 2)), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))
Piomicron
  • 113
  • 4
  • Does your `variables` object is a dataframe or a list ? based on your code, it looks to be a list (you are using `variables[[a]]`). Can you edit your question to add a small [reproducible example of your dataset](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? (for example, the output of `dput(df[1:10,1:10])`) – dc37 Nov 30 '19 at 19:35
  • @dc37 Varnames is a list, but variables is a dataset. As for a minature dataset, I'll see what I can do... – Piomicron Nov 30 '19 at 20:14

1 Answers1

1

You get an error in the assignment of your a and b. Basically, when defining a and b in variables, they become the vector of values contained in columns of variables. Thus, in your aes mapping, when you are calling variables[[a]], basically, you are writing (for the first iteration of a in variables): variables[[c(1, 1, 1, 1, 4)]] instead of variables[["InfOfReceievingWeather"]]. So, it can't work.

To get over this issue, you have to either choose between:

for (a in variables) {
  for (b in variables) {
    print(ggplot(variables, mapping=aes(x=a, y=b)) ...

or

for (a in 1:ncol(variables)) {
  for (b in 1:ncol(variables)) {
    print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]])) ...

Despite the first one seems to be simpler, I will rather prefere the second option because it will allow you to recycle a and b as column indicator to extract colnames of variables for xlab and ylab. At the end, writing something like this should work:

for (a in 1:ncol(variables)) {
  for (b in 1:ncol(variables)) {
    print(ggplot(variables, mapping=aes(x=variables[[a]], y=variables[[b]])) +
            xlab(colnames(variables)[a])+
            ylab(colnames(variables)[b])+
            geom_point())
  }
}

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32