Making multiple multi-line plots from one matrix - r

I'm wondering if there's a way to combine the apply function along with the matplot function to generate a new plot for every n columns in a matrix.
I currently have a matrix with 1350 rows with 640 columns. I'm plotting the values for all the rows for every 8 columns using matplot:
png("cmpd1.png")
matplot(data[,1:8], type="l", y-lab="z-score", axes = F)
axis(side=2)
dev.off()
I want to automate this a bit and have the column values shift by eight each loop and the label on the png to change by one.
Can someone please give directions?

Here is an answer with vapply() and formatted plot names. The closed device id will be returned.
data <- matrix(rnorm(100*128),nrow = 100,ncol = 128)
vapply(seq(from=1,to=ncol(data)-8,by=8),
FUN = function (x) {
png(paste0("cmpd",formatC(width = 3, format = "d", flag="0", x=x%/%8),".png"))
matplot(data[,x:(x+8)], type="l", ylab="z-score", axes = F)
invisible(dev.off())
},
FUN.VALUE=integer(1)
)

I think there is a version with lapply() but a loop does the job as well
for(i in seq(1,ncol(data),8)){
png(paste0("cmpd",i,".png"))
matplot(data[,i:(i+7)], type="l", ylab="z-score", axes = F)
axis(side=2)
dev.off()
}

Related

plot all rows of matrix in the same plot in R

I want to create a plot.
x values are matt 's colnames : count.4, count.5, ...
y values are range of matt elements
the scatter POINTS on plot are corresponding matt[i,j] elemnts, such that points for each row have the same color.
for (rr in 2:8) {
rownames(matt)[rr-1] <- paste('class', rr, sep='.')
for(cc in 4:20) {
matt[rr-1, cc-3] <- rr * cc
colnames(matt)[cc-3] <- paste('count', cc, sep='.')
}
}
I am really stuck at how to get such plot. Any help or hint is very appreciated. I found
matplot (c(4:20), cbind(matt[1,]:mat[7,]), pch = 19, ylim = range(c(matt[1,]:mat[7,]))
I see cbind(matt[1,]:matt[7,]) is not of correct format. but I do not know how to write this such than for a matrix with many more rows I do not have to write all matt[j,]
how can I get to this?
Edit. this is the plot I get when I use cbind(matt[1,],matt[2,],...,matt[7,])
Other than the efficient way coding this, I do not know why there are two sets of black points. can I label colors to corresponding "class" so that the plot gets easier to be read.
Normally series are stored in columns, not in rows, but if they are in rows just use transpose. There are other color palettes available at ?rainbow and a long vector of colors available via colors() if you don't like the colors we used here. (We used the input in the Note at the end.)
col <- rainbow(length(rr))
matplot(cc, t(mat), pch = 19, col = col, type = "o")
legend("topleft", legend = rownames(mat), pch = 19, col = col, lty = 1)
Note
rr <- 2:8
cc <- 4:20
mat <- outer(rr, cc)
dimnames(mat) <- list(paste0("class.", rr), paste0("count.", cc))

Understanding l_ply from plyr

From the documentation:
All output is discarded. This is useful for functions that you are calling purely for their side effects like displaying plots or saving output.
I've spent some time playing around and trying to find a suitable use case but haven't(yet).
Looking at the examples hasn't helped me better understand it.
Sample usage:
l_ply(iris[1:5,1], function(x) print(summary(x)))
This will work.
However, under what circumstances might one need to print and then discard these results?
Consider the following
X <- matrix (c (rnorm (50)), ncol = 5);
Assume each colmn of X indicates a series which you want to overplot.
You can do it as following, by first creating an empty plot and then plotting the series corresponding to each column, using lapply. Although lapply will return the values returned by the plot call that we do not want.
plot (NULL, ylim = range (X), xlim = c (1, nrow (X)));
lapply (1:ncol (X), function (i) points (X[,i], type = "o", col = i));
Instead you can use
plot (NULL, ylim = range (X), xlim = c (1, nrow (X)));
l_ply (1:ncol (X), function (i) points (X[,i], type = "o", col = i));
This has the same effect but does not return the values returned by plot. Here, the "side effect" is the plot function plotting on the device.

Plot a frame when encountering empty vectors in R?

Suppose I have three vectors, one empty (here x.y1) and others not empty (here x.y2 and x.y3). My goal is for empty vectors, an empty plot be plotted (just a frame with nothing in it).
Given my plotting plan detailed below, is it possible that before the final plotting by lapply I could have a function that would check if the vector is empty and then lpply just create a frame for plot (e.g., plot.new() ; box()) for the empty vectors to be plotted?
x.y1 = c()
x.y2 = c(2, 3)
x.y3 = c(6, 2)
m = matrix(1:3); layout(m)
plot.names = noquote(paste0("x.y", 1:3))
lapply(plot.names, plot)
# Error: need finite 'ylim' values # Right now I get this error due to the empty vector!
par(mfrow = c(1,3))
lapply(mget(plot.names), function(a)
if(is.null(a)){
plot(0,0, type = "n")
}else{
plot(a)
})

R statistical Programing

I am trying to write R codes for the histogram plot and save each histogram separate file using the following command.
I have a data set "Dummy" and i want to plot each histogram by a column name and there will be 100 histogram plots in total...
I have the following R codes that draws the each Histogram...
library(ggplot2)
i<-1
for(i in 1:100)
{
jpeg(file="d:/R Data/hist.jpeg", sep=",")
hist(Dummy$colnames<-1, ylab= "Score",ylim=c(0,3),col=c("blue"));
dev.off()
i++
if(i>100)
break()
}
As a start, let's get your for loop into R a little better by taking out the lines trying to change i, your for loop will do that for you.
We'll also include a file= value that changes with each loop run.
for(i in 1:100)
{
jpeg(file = paste0("d:/R Data/hist", i, ".jpeg"))
hist(Dummy[[i]], ylab = "Score", ylim = c(0, 3), col = "blue")
dev.off()
}
Now we just need to decide what you want to plot. Will each plot be different? How will each plot extract the data it needs?
EDIT: I've taken a stab at what you're trying to do. Are you trying to take each of 100 columns from the Dummy dataset? If so, Dummy[[i]] should achieve that (or Dummy[,i] if Dummy is a matrix).

R plot of a matrix of complex numbers

I have a matrix of complex values.
If I issue the command:
plot(myMatrix)
then it shows on the graphics device a kind of scatterplot, with X-axis labeled Re(myMatrix) and Y-axis with Im(myMatrix). This shows the information I'm looking for, as I can see distinct clusters, that I cannot see with only one column.
My questions are :
I assume there is one point per matrix row. Is it right ?
How is calculated Re(myMatrix) for each row vector ?
It is not Re(myMatrix[1,row]), but seems to be a mix of all values of row vector. I would like to be able to get these values, so to know how to compute them with R.
No, there is one point for each matrix element.
set.seed(42)
mat <- matrix(complex(real = rnorm(16), imaginary = rlnorm(16)), 4)
plot(mat)
points(Re(mat[1,1]), Im(mat[1,1]), col = "red", pch = ".", cex = 5)
Look for the red dot:
You'd get the same plot, if you plotted a vector instead of a matrix, i.e., plot(c(mat)).
This happens because plot.default calls xy.coords and that function contains the following code:
else if (is.complex(x)) {
y <- Im(x)
x <- Re(x)
xlab <- paste0("Re(", ylab, ")")
ylab <- paste0("Im(", ylab, ")")
}
else if (is.matrix(x) || is.data.frame(x)) {
This means, that the fact that input is complex takes priority over it being a matrix.

Resources