Reputation: 415
Im trying to loop a lot of graphs in R. The code im using looks like this:
#--------------------------------------------------------------------
#### Read data & Converting factors ####
dat <- read.table("Complete RING.txt", header =TRUE)
str(dat)
dat$Vial <- as.factor(dat$Vial)
dat$Line <- as.factor(dat$Line)
dat$Fly <- as.factor(dat$Fly)
dat$Temp <- as.factor(dat$Temp)
str(dat)
meltet <- melt(dat, id=c("Concentration","Sex","Line","Vial", "Fly", "Temp", "Vial_nr"))
meltet1 <- subset(meltet, Line=="20")
meltet1$variable <- as.factor(meltet1$variable)
AllConcentrations <- levels(unique(meltet1$Concentration))
for (i in AllConcentrations) {
meltet.i <- meltet1[meltet1$Concentration ==i,]
quartz()
print(dotplot(value~variable|Temp, group=Sex, data = meltet.i ,xlab="Time", ylab="Fly position", main=paste("Line 20 concentraion", AllConcentrations[i]))) }
The concentrations im using are denoted A, B, C, D, E, X and Y - how do i add a title to the plot that changes with the plots, i have tried to use the main=" " function but then i get the same name in each graph. The title i get now is: Line 20 concentration NA in each graph
EDIT: updated the code
Upvotes: 1
Views: 3349
Reputation: 38500
I believe that the AllConcentrations vector which you are looping through is a of class "character." If you want to use this in your title, use main=AllConcentrations[i]
. You can also make this fancier with the paste function: main=paste("Graph of", AllConcentrations[i])
.
Upvotes: 3