Skip to content

Problems getting symbols() to show table data

5 messages · Guy Green, Gabor Grothendieck

#
Hello,

I am trying to create a graphic to help me visualise data.  A (very
simplified) sample of the data is 
http://n4.nabble.com/file/n1839676/circle_data.txt circle_data.txt :

              Aug-07         Nov-07      Feb-08
data1	          1              1.5           -1
data2          1              1.2             1.6
data3          1.3            1.4            1.8
data4	          1.3          -1.2            1

What I am trying to achieve is:

 * Circles representing each item of data,
 * The circle size represents the number,
 * The data is arranged in rows and columns, similar to the table layout
(i.e. dates along the top or bottom),
 * Positive numbers are blue circles, negative red circles.

I have used info from lots of previous posts on symbols() to get me some of
the way, but I am still far off getting it to actually work, with the result
that I think I may be heading in completely the wrong direction.  I can
create a grid of circles with the right number of dimensions:

Read_data=read.table("C:/files/circle_data.txt", head = T)
number_rows = nrow(Read_data)
number_cols = ncol(Read_data)
xaxis = rep(seq(1,ncol(Read_data),1),nrow(Read_data))
yaxis = rep(seq(1,nrow(Read_data),1),ncol(Read_data))
zvalues = rep(0.1,(number_rows*number_cols))	#This is just a dummy vector
symbols(xaxis, yaxis, circles = zvalues, inches = FALSE, xlab="", ylab="")
axis(2, at=c(1:number_rows), rownames(Read_data), las = 1)
axis(1, at=c(1:number_cols), colnames(Read_data))

BUT... this only uses dummy values - I can't think how to get the table into
a form the symbols() function can use.  I tried as.vector() unsuccessfully. 
Also, this code simply overwrites the x and y-axis labels, which looks
terrible: again, I can't think how to get this right from the start.

On getting positive values to be blue and negative red, I had assumed I
could just split the data into two and plot separately:

Data_positive = replace(Read_data, Read_data<0,0)
Data_negative = -replace(Read_data, Read_data>0,0)

However, this quick trial below gives little dots for zero values, so it is
not a perfect result:

zvalues_pos = rep(c(0.1,0.2,0), 4)	#Dummy vector for positives
zvalues_neg = rep(c(0,0,0.3), 4)	#Dummy vector for negatives
symbols(xaxis, yaxis, circles = zvalues_pos, inches = FALSE, xlab="",
ylab="", bg="blue")
symbols(xaxis, yaxis, circles = zvalues_neg, inches = FALSE, xlab="",
ylab="", bg="red", add=TRUE)

[Incidentally, I have tried to work through this example (
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=152
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=152 ) but I am
out of my depth in making sense of the code.]

Any help would be gratefully received.  Thanks,

Guy
#
Try this:

library(gplots)
m <- matrix(c(1, 2, -3, -6, 5, 4), 3)
tm <- t(m)
balloonplot(row(tm), col(tm), abs(tm),
   dotcolor = c("blue", "red")[(c(tm) > 0) + 1],
   show.margins = FALSE, cum.margins = FALSE,
   xlab = "Cols", ylab = "Rows", main = "m")
On Wed, Apr 14, 2010 at 7:58 AM, Guy Green <guygreen at netvigator.com> wrote:
1 day later
#
Thanks, balloonplot() is great and gets me really close to what I am after.

However it then brings me to a slightly different problem - I wonder if
anyone can suggest where I am going wrong?

Again with simplified data (
http://n4.nabble.com/file/n1890724/test-data.txt test-data.txt ):

           Aug-03   Nov-03   Feb-04   May-04   Aug-04
Row1       1         -1.1         1.2        1.3        -1.4
Row2       3         -3.1         3.2        3.3        -3.4
Row3       5         -5.1         5.2       -5.3         5.4

library(gplots)
Read_data=read.table("C:/files/test-data.txt", head = T)
number_rows = nrow(Read_data)
number_cols = ncol(Read_data)
matrix_data = as.matrix(Read_data)
row_names = rep(rownames(matrix_data),number_cols)
col_names = rep(colnames(matrix_data),number_rows)
x11(width=120, height=80)
balloonplot(col_names, row_names, abs(matrix_data),
	dotcolor = c("lightblue", "red")[(c(matrix_data) < 0) + 1],
	show.margins = FALSE, cum.margins = FALSE, 
	xlab = "", ylab = "", label=T, label.lines= F, colsrt=90, sorted = F,
	rowmar=3, colmar=2.5,) 

This gives this graphic:  http://n4.nabble.com/file/n1890724/R_upload.jpeg
R_upload.jpeg .

The data colours go in the "right" places within the grid - i.e. with the
correct numbers.  However the numbers themselves are mixed up within each
row.  Transposing the matrix_data within balloonplot() doesn't correct it.

Can someone see something simple that I am missing here?  Thanks,

Guy
#
Try this:

library(gplots)
URL <- "http://n4.nabble.com/file/n1890724/test-data.txt"
DF <- read.table(URL)

balloonplot(colnames(DF)[col(DF)], rownames(DF)[row(DF)],
	abs(as.matrix(DF)),
	dotcolor = c("lightblue", "red")[(c(matrix_data) < 0) + 1],
	show.margins = FALSE, cum.margins = FALSE,
	xlab = "", ylab = "", label = TRUE, label.lines = FALSE,
	colsrt = 90, sorted = FALSE, rowmar = 3, colmar = 2.5)
On Thu, Apr 15, 2010 at 9:23 AM, Guy Green <guygreen at netvigator.com> wrote: