Plot Weighted Frequency Matrix

Plot weighted frequency matrix

You can produce this plot...

Sample Image

... by using this code:

boring <- function(x, occ) occ/x

boring_seq <- function(occ, length.out){
x <- seq(occ, length.out=length.out)
data.frame(x = x, y = boring(x, occ))
}

numbet <- 31
odds <- 6
plot(1, 0, type="n",
xlim=c(1, numbet + odds), ylim=c(0, 1),
yaxp=c(0,1,2),
main="Frequency matrix",
xlab="Successive occasions",
ylab="Relative frequency"
)

axis(2, at=c(0, 0.5, 1))

for(i in 1:odds){
xy <- boring_seq(i, numbet+1)
lines(xy$x, xy$y, type="o", cex=0.5)
}

for(i in 1:numbet){
xy <- boring_seq(i, odds+1)
lines(xy$x, 1-xy$y, type="o", cex=0.5)
}

Visualizing a two-way weighted frequency table

My first idea would be to go with the geom_count (former stat_sum) function from the ggplot2 package. Using it, you could plot your table with e.g. gender on the x-axis and vol on the y-axis. Than the frequency would be displayed as the size of the circle

ggplot(two_way, aes(x=Var1, y=Var2)) +
stat_sum(aes(size=Freq))

Sample Image

Reproduce frequency matrix plot

here is an example:

library(plyr)
ps <- ldply(1:36, function(i)data.frame(s=0:i, n=i))
plot.new()
plot.window(c(1,36), c(0,1))
apply(ps, 1, function(x){
s<-x[1]; n<-x[2];
lines(c(n, n+1, n, n+1), c(s/n, s/(n+1), s/n, (s+1)/(n+1)), type="o")})
axis(1)
axis(2)

ps represents all points. Each point has two children.
So draw lines from each point to the children.

Sample Image

igraph creating a weighted adjacency matrix

The weights are there, weight (e/n) means that there is an edge attribute called weight, and it is numeric. See ?print.igraph. But they are not plotted by default, you need to add them as edge.label.

plot(ig, edge.label=round(E(ig)$weight, 3))

graph plot screenshot

For plotting, make sure you read ?igraph.plotting.

What is the simplest way to create a weight matrix bases on how frequent each element appear in the matrix?

Here's a three-line solution. First prepare the input:

X = [7 9 6;8 7 9;7 6 7];

Now do:

[a m n] = unique(X);

b = hist(X(:),a);

c = reshape(b(n),size(X));

Which gives this value for c:

4     2     2
1 4 2
4 2 4

If you also wanted the frequency matrix, you can get it with this code:

[a b']


Related Topics



Leave a reply



Submit