0

I'm trying to plot each variable in a set against every other variable in a set.

My code is as follows:

library(tidyverse)

load("Transport_Survey.RData")

variables <- select(Transport_Survey, one_of("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 = 0
countery = 0

for (x in variables) {
  for (y in variables) {
    plot(Transport_Survey[[x]],Transport_Survey[[y]],
    xlab=varnames[counterx], ylab=varnames[countery])
    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))

Unfortunately, I keep getting the following error:

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

I assume it's got something to do with the layered for loops or something, but I don't know what .subset2(x, i) is, nor how to approach solving it

Piomicron
  • 113
  • 4
  • 1
    Hello and welcome to stack. Firstly when you write a question can you provide a https://stackoverflow.com/help/minimal-reproducible-example i.e. so that someone without any of your data can run your code (using `load` is no use for other people without your data file, best to provide `dput(Transport_Survey)` or `dput(head(Transport_Survey))`). Without your data a couple of guesses: `variables` is still a data.frame not the names of the columns so `Transport_Survey[[x]]` is looking for a whole df within the df. Also you are starting counterx at 0 but the first element is 1 – Sarah Nov 25 '19 at 02:32
  • Are you trying to do [this](https://stackoverflow.com/questions/36582772/r-plotting-each-column-against-each-column)? – R. Schifini Nov 25 '19 at 03:28

2 Answers2

0

There might be some existing packages (functions) to do what you want. Just help you fix your code (indexing error), I use mtcars data:

library(tidyverse)
variables <- select(mtcars, c(mpg, hp, disp)) 
varnames <- list(names(variables))
for (x in varnames) {
    for (y in varnames) {
        plot(variables[x], variables[y]) 
    }
}

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
0

Zhiqiang has already helped you to fix the code but as R. Schifini has mentioned the best way is to use pairs to plot every combination of two variables. You can also extract all combinations of columns using combn and then plot them one by one or put them together using par as below:

Plotting using pairs might be the best

df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')
pairs(df[,1:4]) # you only need the first four columns as you don't need class

If needed to remove duplicate plots

pairs(df[,1:4],upper.panel=NULL,pch=42)

enter image description here

If needed to plot them all in one plot using a loop (you can easily avoid plotting duplicates). Then you can do it with just one loop.

cm <- combn(names(df[,1:4]), 2, simplify=FALSE)
par(mfrow=c(2,3))
for(i in 1:length(cm)){
  plot(df[cm[[i]][1]][[1]], df[cm[[i]][2]][[1]], xlab = cm[[i]][1], ylab=cm[[i]][1])
}

enter image description here

Majid
  • 1,836
  • 9
  • 19