Colin Kohoutek
Colin Kohoutek

Reputation: 45

Plotting in R with variables

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.

Upvotes: 0

Views: 50

Answers (1)

Luke C
Luke C

Reputation: 10336

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

Upvotes: 1

Related Questions