Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot only 1s of a binomial matrix

Tags:

plot

r

matrix

I would like to plot all the 1s of a binomial matrix (code given below). I need to histogram y-axis to be the frequency and the x-axis to be all the matrix columns.

#code for matrix    
prevalence <- 0.01
Infected <- sapply(1:10, function(p) rbinom(500, 1, prevalence))

I tried using hist(Infected) but this just gave me the frequency of 0 and 1, with no column details. Thank you for your help.

like image 421
Katy Baker Avatar asked Oct 24 '25 01:10

Katy Baker


1 Answers

Use colSums to get the number of 1 and then barplot to get a bar per column.

set.seed(42)
prevalence <- 0.01
Infected <- sapply(1:10, function(p) rbinom(500, 1, prevalence))

barplot(colSums(Infected), names.arg = 1:10)

enter image description here

like image 163
GKi Avatar answered Oct 26 '25 15:10

GKi