Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using R to analyze data and have used the xtable package within R to produce a TeX output for a correlation matrix. I have also implemented the dcolumn package within TeX so that I would be able to align my data by decimal points. My data have both positive and negative numbers. It seems the output produced is aligned somewhat correctly. The positive numbers are aligned by the decimal point to other positive numbers and negative numbers are also aligned to other negative numbers. I believe the issue is related to the inclusion of an additional non-numerical character (the negative sign) for the negative numbers such that the string is bigger than that of positive numbers. Can anyone help me correct this issue?

My code follows:

% latex table generated in R 2.14.1 by xtable 1.7-0 package
% Tue Mar 20 13:44:16 2012
\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{2.2}}
\newcolumntype{s}{D{.}{.}{1.2}}
\usepackage{color}
\usepackage{underscore}
\usepackage{rotating}
\usepackage{caption}
\usepackage{dcolumn}
\usepackage{siunitx}
\begin{document}
\begin{sidewaystable}[ht]
\caption*{Correlation Matrix}
\begin{center}
\scalebox{0.4}{
\begin{tabular}{|rr*{39}{d}|}   
\hline
   &     &   1   &   2   &   3   &   4   &   5   &   6   &   7   &   8   &   9   &   10       &   11   &   12   &   13   &   14   &   15   &   16   &  17   &   18   &   19   &   20   &       21   &   22   &   23   &   24   &   25   &   26   &   27   &   28   &   29   &   30   &       31   &   32   &   33   &   34   &   35   &   36   &   37   &   38   &   39   \\ \hline


\multicolumn{1}{|r|}{1}   &  \multicolumn{1}{r|}{ APCcom }  &   \   &  \multicolumn{1}    {c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.09 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{2}   &  \multicolumn{1}{r|}{ APCemp }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.33 } }  &   \   &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.18 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.73 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.14 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{3}   &  \multicolumn{1}{r|}{ APCrule }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.20 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.33 } }  &   \   &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.81 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.23 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{4}   &  \multicolumn{1}{r|}{ ATFtot }  &  \multicolumn{1}{c}{ \textcolor{black}{ -0.03 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.27 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.17 } }  &   \   &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.40 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.40 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.01 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.09 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

\multicolumn{1}{|r|}{5}   &  \multicolumn{1}{r|}{ flstot }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.37 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.24 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.02 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ -0.32 } }  &   \   &  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.10 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.08 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.05 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 0.21 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.02 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{red}{ 0.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c}{ \textcolor{black}{ 1.00 } }  &  \multicolumn{1}{c|}{ \textcolor{black}{ 1.00 } }  \\  

% ... table shortened for space considerations
\hline
\end{tabular}
}
\caption*{r-values below the diagonal, p-values above, \\
significant r-values and p-values appear in \textcolor{red}{red}}
\label{Matrix}
\end{center}
\end{sidewaystable}
\end{document}
share|improve this question
    
Welcome to TeX.sx! Please can you make your code into a minimal working example (MWE): as it is, there is a lot of extra material here. In particular, I can't see any negative values. –  Joseph Wright Mar 21 '12 at 21:23
    
Welcome to TeX.SE! I've edited your code a bit to (i) make it more easily compilable for anyone who might be tempted to do so and (ii) replace the long string ddd...ddd (39 copies total) with *{39}{d}. Separately, it's the custom in this group not to end questions with expressions of appreciation and thanks; nothing rude or curt, just a custom. The best way to say thank you is to upvote good answers and to accept the one you think is best. –  Mico Mar 21 '12 at 21:33

2 Answers 2

up vote 6 down vote accepted

You should better use the facilities of siunitx: here's a minimal example

\begin{tabular}{|r|l|SS|}
\hline
\multicolumn{2}{|c}{}   &   1  & 2 \\
\hline
1 & APCcom  &                  & \color{red} 0.01 \\
2 & APCemp  & \color{red} 0.33 &                  \\
3 & APCrule & \color{red} 0.20 & \color{red} 0.33 \\
4 & ATFtot  &            -0.03 & \color{red} 0.27 \\
\hline
\end{tabular}

on which you can build up. enter image description here

Declaring d columns and then overriding them with \multicolumn{1}{c}{...} is the main cause of your problem.

share|improve this answer
    
Thank you very much! –  Daniel Mar 21 '12 at 21:33
    
Isn't the use of the \multicolumn{}{}{} statements the sole, and not just the main, cause of the OP's table alignment problems? –  Mico Mar 21 '12 at 23:06
    
@Mico No, also \textcolor. :) –  egreg Mar 21 '12 at 23:06
    
Good point ... the OP's numbers are doubly hidden from view! –  Mico Mar 21 '12 at 23:12

The reason why the negative numbers -- actually, all numbers -- are not aligned on the decimal point is because every single number is currently doubly hidden from view of either the dcolumn and siunitx packages by the nested \multicolumn{}{}{} and \textcolor{}{} statements.

You state that the table is generated, in its current elaborate format, by R. Can you figure out how to instruct R to (i) suppress all those unneeded and actually counterproductive \multicolumn statements and (ii) generate \color{red} xyz and \color{black} abc statements instead of \textcolor{red}{xyz} and \textcolor{black}{abc}? (Actually, because "black" is the default color, you might want to instruct R not provide any color-related statement if the color in use is black.)

With these modifications, you could use the S column type, of the siunitx package, to get all numbers nicely aligned on their decimal markers. For your specific table, I would not recommend you use the dcolumn package's d column type because it doesn't handle the color-changing commands correctly, at least not without a lot of extra programming. In contrast, the S column type handles \color{red} commands with ease, as @egreg's answer demonstrates.

Finally, a recommendation that's not related to aligning the numbers on their decimal markers but which regards the overall legibility of a table that has 41 [!] columns. LaTeX's default amount of intercolumn whitespace in a tabular environment is 10pt (or 2\tabcolsep); this adds up to a lot of white-space when you have 40 intercolumn spaces. If you were to reduce the amount of white-space, say with the instruction

\setlength\tabcolsep{3pt}  % default: 5pt

you could, in turn, change the first argument of the \scalebox command from 0.4 to ca. 0.55 and still make the entire table fit in the text block. Your readers will, hopefully, be grateful to you for making this change.

share|improve this answer
    
Yes, within R I adapted my conditional formatting function to reduce the number of {}s which seems to have fixed the doubly hidden issue. Oh, and I must confess, the table is meant for internal use only to share the initial correlations for a study we conducted. No one will be unnecessarily subjected to such a monstrous table. :) –  Daniel Mar 22 '12 at 22:15
    
Glad to know you were able to get R to print out less overhead information. Regarding the audience for your table: even if it's only for internal use, you may still see fit to reduce the interline whitespace so that you don't have to reduce the numbers's size as drastically. :-) –  Mico Mar 22 '12 at 22:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.