0

I am trying to use a for loop to make a bunch of plots, but can't seem to get it to work. My y axis should be the variable.

plot(data$date1,data$abc,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25, 
main=reg,xlab="Date",ylab="Percent")
reg
[1] "abc"
plot(data$date1,data$reg,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25, 
main=reg,xlab="Date",ylab="Percent")

Is there a way to use reg to plot this data instead of abc? Adding some example data and more info. I am trying to loop through all the nps and plot them by date.

df <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "11-MAY-2017", class = "factor"), 
    log = structure(1:6, .Label = c("707406D:01", "707406D:02", 
    "707406D:03", "707406D:04", "707406D:05", "707406D:06"), class = "factor"), 
    count = c(1L, 1L, 1L, 1L, 1L, 1L), np. = c(90.465, 89.444, 
    91.714, 88.082, 89.784, 90.692), npB = c(0.123, 0, 0.122, 
    0, 0.246, 0.122), npC = c(0, 0, 0.123, 0, 0.126, 0), npD = c(0.248, 
    0.502, 0, 0.252, 0, 0.492), npE = c(0L, 0L, 0L, 0L, 0L, 0L
    ), npF = c(0, 0, 0, 0, 0.252, 0), npH = c(0L, 0L, 0L, 0L, 
    0L, 0L), npI = c(0.341, 0.568, 0.795, 0.908, 0.114, 0.681
    )), .Names = c("date", "log", "count", "np.", "npB", "npC", 
"npD", "npE", "npF", "npH", "npI"), class = "data.frame", row.names = c(NA, 
-6L))

> df
         date        log count    np.   npB   npC   npD npE   npF npH   npI
1 11-MAY-2017 707406D:01     1 90.465 0.123 0.000 0.248   0 0.000   0 0.341
2 11-MAY-2017 707406D:02     1 89.444 0.000 0.000 0.502   0 0.000   0 0.568
3 11-MAY-2017 707406D:03     1 91.714 0.122 0.123 0.000   0 0.000   0 0.795
4 11-MAY-2017 707406D:04     1 88.082 0.000 0.000 0.252   0 0.000   0 0.908
5 11-MAY-2017 707406D:05     1 89.784 0.246 0.126 0.000   0 0.252   0 0.114
6 11-MAY-2017 707406D:06     1 90.692 0.122 0.000 0.492   0 0.000   0 0.681

for (reg in an) {
  plot(data$date1,data$npB,xlim=c(xmin,xmax),ylim=c(ymin,ymax),cex=.25, 
  main=reg,xlab="Date",ylab="Percent")
  graphics.off()
}

In the example above for my plot I would like to use the reg variable instead of the actual column name npB to be able to use a for loop. Hope this helps.

Luke C
  • 10,081
  • 1
  • 14
  • 21
  • Could you provide an example of your data? Check out [this guide](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on question formatting. Is your expected output from the code example two plots, where you have one plot of `data$abc` vs `data$date` and one plot of `data$reg` vs `data$Date`? Could you clarify what you mean by "use reg to plot this data instead of abc"? – Luke C Jul 11 '17 at 23:52
  • Luke, I added some more example. Does that help clarify? – Colin Kohoutek Jul 12 '17 at 02:51
  • I'm getting a better picture for sure! A few more questions to help me understand, if you don't mind. Is `an` the data frame you have shown in your edit, or is that a separate list of names or something? Specifically, what is the `reg` variable? – Luke C Jul 12 '17 at 03:02
  • @LukeC ohh yeah forgot to include that an is a list of the column names. So reg will be aliased to each column name that I want to plot. an [1] "npB" "npC" "npD" "npE" "npF" "npH" "npI" "npJ" "npL" "npN" – Colin Kohoutek Jul 12 '17 at 03:21
  • That makes sense- I edited my answer. See if that's closer to what you're after. – Luke C Jul 12 '17 at 03:30
  • 1
    Sweet thanks that did it. – Colin Kohoutek Jul 12 '17 at 03:44

1 Answers1

1

Not sure exactly what you're after, but maybe this is on the right track:

date<- seq(2007:2016)
abc <- sample(1:100, 10)
def <- sample(50:100, 10)
reg <- sample(40:60, 10)


df <- data.frame(date, abc, def, reg)

for(i in names(df[,2:4])){
  plot(df$date, df[,i], xlab = "Date", ylab = "Percent", main = i,
       ylim = (c(0,100)))
}

Edit

Following up from your comments, using this example data:

df2 <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "11-MAY-2017", class = "factor"), 
    log = structure(1:6, .Label = c("707406D:01", "707406D:02", 
    "707406D:03", "707406D:04", "707406D:05", "707406D:06"), class = "factor"), 
    count = c(1L, 1L, 1L, 1L, 1L, 1L), np. = c(90.465, 89.444, 
    91.714, 88.082, 89.784, 90.692), npB = c(0.123, 0, 0.122, 
    0, 0.246, 0.122), npC = c(0, 0, 0.123, 0, 0.126, 0), npD = c(0.248, 
    0.502, 0, 0.252, 0, 0.492), npE = c(0L, 0L, 0L, 0L, 0L, 0L
    ), npF = c(0, 0, 0, 0, 0.252, 0), npH = c(0L, 0L, 0L, 0L, 
    0L, 0L), npI = c(0.341, 0.568, 0.795, 0.908, 0.114, 0.681
    )), .Names = c("date", "log", "count", "np.", "npB", "npC", 
"npD", "npE", "npF", "npH", "npI"), class = "data.frame", row.names = c(NA, 
-6L))

For the example data, pull the column names from npB to the end:

an <- names(df[,5:ncol(df)])

> an
[1] "npB" "npC" "npD" "npE" "npF" "npH" "npI"

Then, not much of a change from the first example I showed, just use an instead of the colnames() argument:

for(i in an){
  plot(df2$date, df2[,i], xlab = "Date", ylab = "Percent", main = i,
       ylim = (c(min(df2[,i]),max(df2[,i]))))
}

Gives plots that look like those below:

enter image description here

Luke C
  • 10,081
  • 1
  • 14
  • 21