Wednesday, July 25, 2012

Quick and dirty way to plot error bars in R

If you have data that looks as follows, a one line command can draw error bars to the plot.
-x---T-----y-------------error-----
p5 315 2.513402 0.5711628                                                      
p5 330 2.173868 0.4711594                                                      
p5 345 2.132954 0.634045                                                       
p5 360 2.092686 0.4851832                                                      
p5 375 2.966971 0.9165328                                                      
p5 390 2.214886 0.4288291

r<-read.table("data.txt")
plot(r[,2],r[,3],ylim=c(0,4))
lines(r[,2],r[,3])
for(i in 1:length(r[,1])) {segments(r[i,2],r[i,3]+r[i,4],r[i,2],r[i,3]-r[i,4])}

I am drawing a simple line from (x,y+err) to (x,y-err). The result is not as fancy as other efforts but it is enough for my purposes.

Monday, July 23, 2012

Wednesday, July 11, 2012

Adding labels to figures using ImageMagick



This is a 1200x1200 pixel blank file to which I added labels as an exercise in using the annotate flag in ImageMagick's convert.
 
convert blank.gif -pointsize 50 \
-gravity south -append -annotate +000+000 '0s' \
-gravity south -append -annotate +100+100 '1s' \
...

-gravity south -append -annotate +900+900 '9s' \
-gravity north -append -annotate +000+000 '0n' \
-gravity north -append -annotate +100+100 '1n' \
...

-gravity north -append -annotate +900+900 '9n' \
-gravity northwest -append -annotate +000+000 '0nw' \
-gravity northwest -append -annotate +100+100 '1nw' \
...

-gravity northwest -append -annotate +900+900 '9nw' \
-gravity northeast -append -annotate +000+000 '0ne' \
-gravity northeast -append -annotate +100+100 '1ne' \
...

-gravity northeast -append -annotate +900+900 '9ne' \
out.png

Joining images using Latex

Do not forget to use pdflatex (not pdftex) to compile tex files which contain non-eps figures.
 
\documentclass{article}
\usepackage{rotating}
\usepackage{graphicx,subfig}

% Default margins are too wide all the way around. I reset them here
\setlength{\topmargin}{-.5in}
\setlength{\textheight}{9in}
\setlength{\oddsidemargin}{.125in}
\setlength{\textwidth}{6.25in}

\begin{document}


\begin{sidewaysfigure}
\begin{tabular}{cc...c}
\includegraphics[scale=0.10, bb= 0 0 669 834]{view1_mol0} &
\includegraphics[scale=0.10, bb= 0 0 669 834]{view1_mol10} \\

\includegraphics[scale=0.10, bb= 0 0 669 834]{view0_mol0} &
\includegraphics[scale=0.10, bb= 0 0 669 834]{view0_mol10} \\

\includegraphics[scale=0.10, bb= 0 0 669 834]{view2_mol0} &
\includegraphics[scale=0.10, bb= 0 0 669 834]{view2_mol10} \\

\end{tabular}
\end{sidewaysfigure}

\end{document}

Joining images using ImageMagick's convert

convert +append list_of_figures out_1.png
convert +append list_of_figures out_2.png
convert +append list_of_figures out_3.png
convert -append out_1.png out_2.png out_3.png final_out.png

+append puts images side by side while -append adds them top down.
convert -border 2x2 in.png out.png
This puts a light border around images which comes in handy when creating a grid of images from a bunch of individual images which have no border.

Wednesday, July 4, 2012

Empty plots in R

When plotting a grid of graphs in R, I needed leave some cells empty. For this the following commands come handy;
plot(0,xaxt='n',yaxt='n',bty='n',pch='',ylab='',xlab='')
plot.new()
grid.newpage()
grid()