Plotting several jpeg images in a single display - image

I need to plot and display several jpeg images in a single combined display (or canvas?). For example, suppose I have images {a,b,c,d}.jpg, each of different size, and I would like to plot them on one page in a 2x2 grid. It would be also nice to be able to set a title for each subplot.
I've been thoroughly looking for a solution, but couldn't find out how to do it, so any ideas would really help. I would preferably use a solution that is based on the EBImage package.

There are two ways how to arrange several plots with base graph functions, namely par(mfrow=c(rows,columns)) (substitute rows and columns with integers) and layout(mat) where mat is a matrix like matrix(c(1,2,3,4)).
For further info see ?par, ?layout, and especially Quick-R: Combining Plots.
However, as your question is about images I don't know if it helps you at all. If not, I am sorry for misinterpreting your question.

To add to Henriks solution, a rather convenient way of using the par() function is:
jpeg(filename="somefile.jpg")
op <- par(mfrow=c(2,2)
#plot the plots you want
par(op)
dev.off()
This way, you put the parameters back to the old state after you ran the code. Be aware of the fact this is NOT true if one of the plots gave an error.
Be aware of the fact that R always put the plots in the same order. Using mfrow fills the grid row by row. If you use mfcol instead of mfrow in the code, you fill up column by column.
Layout is a whole different story. Here you can define in which order the plots have to be placed. So layout(matrix(1:4,nrow=2) does the same as par(mfcol=c(2,2)). But layout(matrix(c(1,4,3,2),ncol=2)) places the first plot lefttop, the next one rightbottom, the third one righttop, and the last one leftbottom.
Every plot is completely independent, so the titles you specify using the option main are printed as well. If you want to have more flexibility, you should take a look at lattice plots.

If you do not want the images in a regular grid (the different sizes could imply this), then you might consider using the subplot function from the TeachingDemos package. The last example in the help page shows using an image as a plotting character, just modify to use your different images and sizes/locations.
The ms.image function (same package) used with my.symbols is another possibility.

Related

dc.js heatmap expanding data

I am trying to show machine states over time. Part of this is to reproduce/automate a report that used to be done by hand. It consists of coloring 2minute 'time slices' in Excel based on what the machine is doing.
(Sorry, not enough reputation to post a picture, but it is a classic heatmap where the state drives the color. Some non DC-JS fiddle: http://jsfiddle.net/ww6Lbnc5/4/)
I was able to generate most of what I want in the following jsfiddle:
http://jsfiddle.net/hwhfxz2t/14/
See fiddle for code.
The total state duration (for selected time frame) is shown in the pieChart, followed by the individual state lines and then the heatmap that people are used to. (the ZOOM and date selection buttons do not work in the fiddle but are there to select specific data ranges or zoom in if you like).
The line charts uses the original representation of the states, which consists of a time the state is entered and a duration.
In order to make the heat map work, I had to (I think) take the original data and convert it into individual minute chunks and mark them with a state. So for instance the original data specifying:
RUN state starting 14:30 for 300 seconds
becomes:
14:30=RUN, 14:31=RUN, 14:32=RUN, 14:33=RUN and 14:34=RUN
The code in lines 233-297 loops through the original data and generates a new one that does this. In cases where there is more than one state within a given minute, the last state survives.
This works okay but it seems that this code is exactly what is normally done in group().reduce(add,remove,init). But in this case I need to add multiple timeslots depending on the duration of a state.
Also, because it is now using a different crossfilter, maps do not update each other.
Here are my questions related to this:
Can I display a heatmap without supplying information for all individual
'cells'? (i.e. straddle cells based on a value, similar to rowspan in a table)
Can I add multiple values at once inside group().reduce()?
Is there an easy way to invert the yAxis so 0 is at the top?
When clicking a row in the heatmap, it selects a column and vice-versa?
I'm not sure if this should be in the crossfilter group. If so please ignore my rambling. If someone knows how to keep the charts linked by grouping better, please let me know.
--Nico
Concerning Question 3:
DC.js heatmaps currently do not support custom order functions on axis but there is a pull request that has been merged into the developing branch and should be accessible to the public soon.
You could manually edit the dc.js file to set the sorting in heatmaps to a custom function. In the latest (2.0.0-beta10) version it is the following line:
rowValues.sort(d3.ascending);
and accordingly
colValues.sort(d3.ascending);

VTK display multiple images

Does someone knows how to display multiple images using ActiViz or VTK?
I have a RenderWindowControl, I would like to either display all the images like in a list in there or add multiple RenderWindowControls to display the multiple images..
Thanks!
You have some choices, for the task of visualize multiple images. I hope that at least one of them represents what you want to achieve:
1- Using multiple render windows. Basically, the example provided in the link creates an array of vtkRenderWindowInteractor objects, so that you can visualize multiple images.
2 - Using a single render window with viewports. The render window is splitted into viewports (renderer->SetViewport is the key, here), so that you can visualize n images at a time, where n is the number of viewports.
3 - Using the "slices" approach (the example works on DICOM files). This implements the "list" behaviour, as you said in your question. Basically, you visualize an image at a time, but you can navigate through them simply by pressing the arrow keys. The example derives the vtkInteractorStyleImage class to catch the events regarding key pressures.

Find areas to output in pdf file

Problem: I have a scanned version of some tax report (e.g. https://docs.google.com/file/d/0B3TmJsb1vYhqclFFendvTlRoV28/edit?usp=sharing ), and I must fill it programmly. So, it`s needed to find empty areas and put to it some values in program.
Now I see only one solution - manually find coordinates of these empty areas, code it to program, and output values to these coordinates.
Is it possible to programmly find coordinates (may be by some pattern recognition or something else) of empty areas? May be use of some Ruby tools (e.g. Prawn) ?
So, there isn't complete automatic solution. I've solved this problem by searching areas to input, store its coordinates and then output to these areas

Manipulating subsections of an array

I am using R to plot trying to conditionally change parts of an array
based on the columns of the array.
I have worked out the following steps:
x<-array(1,dim=c(4,4,3))
r<-x[,,1]
g<-x[,,2]
b<-x[,,3]
r1<-apply(r[,2:3],1:2,function(f){return(0)})
g1<-apply(g[,2:3],1:2,function(f){return(0)})
b1<-apply(b[,2:3],1:2,function(f){return(0)})
r3<-cbind(r[,1],r1,r[,4])
g3<-cbind(g[,1],g1,g[,4])
b3<-cbind(b[,1],b1,b[,4])
# Pass to pixmapRGB
This works, but as I am new to R, I was wondering if
there was a more efficient way to manipulate parts
of an array.
For example, does apply know which element it is working on?
The bigger picture is that I want to graph a time-series scatter
plot over many pages.
I would like to have a thumbnail in the corner of the page that is
a graph of the whole series. I would like to color a portion of
that thumbnail a different color to indicate what range the
current page is examining.
There is alot of data, so it is not feasible to redraw a new plot
for the thumbnail on every page.
What I have done is to first write the thumbnail plot out to a tiff file.
Then I read the tiff file back in, used getChannels from pixmap
to break the picture into arrays, and used the above code to change
some of the pixels based on column.
Finally I then print the image to a viewport using
pixmapRGB/pixmapGrob/grid.draw
It seems like alot of steps. I would be grateful for any pointers
that would help me make this more efficient.
Maybe I don't understand your question, but if what you're trying to do is just "change some pixels based on column," why don't you just use the basic array indexing to do that?
This will do the same thing you have posted:
x<-array(1,dim=c(4,4,3))
r<-x[,,1]
g<-x[,,2]
b<-x[,,3]
r[,2:3]=0
g[,2:3]=0
b[,2:3]=0
Is that helpful?
Perhaps more of a comment than an answer, but when I try to plot over a number of pages I usually go left to right, breaking up the plots into quantiles and setting appropriate xlim (or ylim)
x <- rnorm(100000)
y <- rnorm(100000)
df <- data.frame(x,y)
seq1 <- quantile(df$x, probs = seq(0,1,0.1))
seq2 <- quantile(df$x, probs = seq(0,1,0.1))
for(x in 1:(length(seq1)-1)) {
plot(df, xlim=c(seq1[x],seq1[x+1]))
}
No idea how to overlay a thumbnail onto the graphs although I think you could do this with one of the rimage functions if you saved the thumbnail.
You could avoid having to read and paste a tiff thumbnail by actually replotting the whole chart at reduced scale. check out par(fig) , and then do something like
Rgames: plot(1:2,1:2)
Rgames: par(mar=c(.1,6,.1,.1),new=T,fig=c(0,.25,.5,.75))
Rgames: plot(1:2,1:2)
Rgames: polygon(c(1,2,2,1),c(1,1,2,2),col='red')
("Rgames:" is my prompt)
You'll have to play a bit with the margin values, but this will get your "mini-graph" set up.

Latex - Is it possible to have text on top of images?

I want to create something like a leaflet/magazine using Latex. Is it possible to place text on top of an image and style the text freely?
Any links to examples of something like this?
I usually do something like
\usepackage{tikz}
...
\begin{tikzpicture}
\draw (0, 0) node[inner sep=0] {\includegraphics[width=4cm]{imagefile.png}};
\draw (1, 1) node {Hello world};
\end{tikzpicture}
A very good toolset for manipulating images is pgf/TikZ pdf doc.
See \pgfimage for examples. It allows to mix text and image freely. There are many ways to do it. One of them is to use layers (p. 220 of pgfmanual). The pgf manual contains many simple examples, and is very precise.
Another solution is to use the lpic package: the homepage contains some examples.
You can find other examples for pfg and TikZ here and here for many impressive examples.
You can also define the text after the image and then offset it using negative vertical space. Because it follows after the image in the LaTeX source, it will be drawn on top of the image instead of underneath it.
\includegraphics[...]{...}
\vspace*{-20ex} % Tune this to the image height.
\begin{center}
Text
\end{center}
\vspace*{20ex} % The spacing above but without the minus.
Another solution is the textpos package which allows you to specify boxes at absolute positions on the page. The boxes can overlap, so you can put the figure in one box, and text in another box on top of it.
I realise that the question is old and answer is accepted, but for completeness would like to propose an alternative approach for making leaflets in LaTeX.
Specifically, the leaflet document style served me really well for this purpose.
A good blog post wrt background image can be found here. There are a couple of packages that are required to use in order to make the proposed approach work, but were not mentioned in the blog post:
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{transparent}
As already suggested, you can annotate the different parts of the figure using TikZ. However, sometimes it might even better to use numbers to reference the different parts and explain them in the figure caption.
To easily get the precise relative positions (which is often tedious) and to generate LaTeX code automatically, you could use the new web-based LaTeX Overlay Generator, which I built for such cases. This is just a small interactive tool, which helps you to find the right locations.
Another way to do this, admittedly not using LaTeX, would simply be to edit the image with photoshop or gimp or something like that. I guess your option in terms of typesetting mathematics might be fairly limited doing it this way.
Otherwise I'd endorse using tikz.
Are you sure you want to do it in LaTeX? Desktop publishing software might be more suited to your needs... Something like scribus might be easier than tikz in terms of learning curve, depending on what you want to do.
I'd like to add on to #midtiby's answer...
You can also specify the text position using a relative position, like so:
\usepackage{tikz}
...
\begin{tikzpicture}
\node[inner sep=0] (image) at (0,0) {\includegraphics[width=4cm]{imagefile.png}};
\node[above=0 of image] {Hello world};
\end{tikzpicture}

Resources