I am trying to make a table in LateX in which the background colour of my cells depend on the size of a number. I found some answers on Parametrize shading in table through TikZ but I cannot get them working in combination with Sweave. Below is the first answer given on the aformentioned page, which works up until the part where I use Sweave, though I have the impression the Sweave gives the correct output.
I have also tried other answers on this topic, but I can get none of them working in combination with Sweave. If someone can get the code below working, or knows of another way to do this with Sweave, it would be greatly appreciated!
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotstableset{
color cells/.style={
col sep=comma,
string type,
postproc cell content/.code={%
\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}\cellcolor{black!##1}\pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}%
},
columns/x/.style={
column name={},
postproc cell content/.code={}
}
}
}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,a1,b1
a,90,10.5,0,0
b,0,80,10,10
a1,0,0,95,5
b1,0,10,5,85
}
\end{table}
\begin{table}\caption{Correlation or something2}
\centering
\pgfplotstabletypeset[color cells]{
<<echo=FALSE, results=tex>>=
xx<-data.frame(a=c(90,10.5,0,0),b=c(0,80,10,10),a1=c(0,0,95,5),b1=c(0,10,5,85))
rownames(xx)=c('a','b','a1','b1')
for(i in 0: nrow(xx)){
if(i==0){
cat(c("x,",paste0(colnames(xx)[1:(ncol(xx)-1)],","),colnames(xx)[ncol(xx)]),"\n")
}else{
cat(paste0(rownames(xx)[i],","),paste0(xx[i,1:(nrow(xx)-1)],","),xx[i,nrow(xx)],"\n")
}
}
@
}
\end{table}
\end{document}