I have a dataset in which I need to plot each variable. I made a loop for this since the dataset contains like 50 variables. This is what I got thus far:
invisible(lapply(colnames(Data),function(x){
plot(Data[,x],main=x,type="l", xlab= 'Week number', ylab='Amount')
}))
This automatically plots all the variables in my dataset in separate plots. However, I need to plot lines in all of these plots. The idea is that I add an average line for each x. So a horizontal line on the height whichs corresponds to the value of the mean x. I tried to do this by adding the abline(h=mean(x)) function to the function above but this doesn't work. Do you guys have any ideas on how to do this?
you should use
abline(h=mean(Data[,x]))
not
abline(h=mean(x))
since each time x is a colnames. to check use this code
Data<-matrix(sqrt(1:20),ncol=4)
colnames(Data)<-c(letters[1:4])
par(mfrow=c(2,2))
invisible(lapply(colnames(Data),function(x){
plot(Data[,x],main=x,type="l", xlab= 'Week number', ylab='Amount')
abline(h=mean(Data[,x]))
}))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.