This helps me keep track of things in the output. When I know I’m going to be looping through character vectors I like to use named vectors. I’ll do the latter since the different types of variables are grouped together. My options are to either write the vectors out manually or pull the names out by index. If all of your response or explanatory variables share some unique pattern in the variable names there are some clever ways to pull out the names with some of the select helper functions in dplyr::select(). I’m going to use vectors of the variable names for this, one vector for the response variables and one for the explanatory variables. The plan is to loop through the variables and make the desired plots. I’ve deemed the first three variables in the dataset to be the response variables ( elev, resp, grad). The goal is to make scatterplots for every response variable vs every explanatory variable. Head(dat) # elev resp grad slp lat long nt (The real dataset had 9 response and 9 explanatory variables.) set.seed(16)ĭat = ame(elev = round( runif(20, 100, 500), 1), Today I’m going to make an example dataset with 3 response ( y) variables and 4 explanatory ( x) variables for plotting. This post is based on an example that involves plotting bivariate relationships between many continuous variables. To get them started I will provide students who need to automate plotting in R some example code (with explanation). So while I often assure students working under time constraints that it is perfectly OK to use software they already know rather than spending the time to learn how to do something in R, making many plots is a special case. I know I invariably have to re-make even exploratory plots, and it’d be a bummer if I had to remake them all manually rather than re-running some code. The efficiency of using a software program you already know is quickly out-weighed by being unable to easily reproduce the plots when needed. Unfortunately making plots manually can backfire. It can look so daunting, in fact, that it can appear easier to manually make the plots (like in Excel) rather than using R at all. However, the coding approach needed to automate plots can look pretty daunting to a beginner R user. When you have a lot of variables and need to make a lot exploratory plots it’s usually worthwhile to automate the process in R instead of manually copying and pasting code for every plot.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |