2

I'm very very new to R. I have a csv file that looks like

A   B   C   D
A1   a   v   a
A2   v   v   a
A3   a   a   a

I'd like to make a co occurrence plot using it but I couldn't figure out how to plot it when the input has characters and not numbers.

I tried using a few packages (I couldn't get any of them to work), out of which one is cooccur. It gave me the following error "Error in rowSums(spp_site_mat, na.rm = T) : 'x' must be numeric". I'd really really appreciate if you point me toward anything helpful or give any code suggestions.

EDIT:

Rows would have the values from column A, columns would be their corresponding values, "a" and "v" something as shown in this link.

enter image description here

Also I have hundreds of columns so I cannot change each column to numeric using as.numeric(table$B)

user20650
  • 24,654
  • 5
  • 56
  • 91
abn
  • 1,353
  • 4
  • 28
  • 58

1 Answers1

2

You can table each row to count the unique values and then form a matrix using use rbind.fill. You can then plot using geom_tile adding in the counts as labels.

# your data
dat <- read.table(text="A   B   C   D
A1   a   v   a
A2   v   v   a
A3   a   a   a", header=TRUE)


library(plyr)
library(ggplot2)
library(reshape2)

# transform your data 
mat <- rbind.fill.matrix(apply(dat[-1], 1, function(i) t(as.matrix(table(i)))))
mat[is.na(mat)] <- 0
rownames(mat) <- dat$A

# plot   
ggplot(melt(mat), aes(Var2, Var1, fill=value)) + 
                 geom_tile() +
                 scale_fill_gradient(limits=c(0,3), low="white") +
                 geom_text( aes(label=value))

enter image description here

EDIT

About the code

mat <- rbind.fill.matrix(apply(dat[-1], 1, function(i) t(as.matrix(table(i)))))

Starting from the inside

apply(dat[-1], 1, function(i) t(as.matrix(table(i))))

apply with MARGIN = 1 applies the function across the rows of the data, excluding the first column. The aim of the function is to table the values for each row. as.matrix is used to change the format of the output and the t (transpose) changes the orientation of the matrix. (Most likely a more succinct way to do this)

As there may not be the same values in each row (row 3 has no v's) all values will not be represented in each table - so rbind will not work. rbind.fill from plyr package fills in with NA.

Below replaces the missing (NA) with zero

mat[is.na(mat)] <- 0
user20650
  • 24,654
  • 5
  • 56
  • 91
  • Thank you for the answer. Could you briefly say what 'mat <- rbind.fill.matrix(apply(dat[-1], 1, function(i) t(as.matrix(table(i)))))' this line does. Thank you! – abn Sep 30 '14 at 21:57