plotting matrix binomial

Hi,

I am new to modelling in R. I have a combined matrix with binomial values (0s and 1s) that has been filled in based on different prevalences like so:

prev1 <- 0.10
a <- sapply(1:5 ,function(p) rbinom(500,1,prev1))

prev2 <- 0.80
b <- sapply(1:5 ,function(p) rbinom(500,1,prev2))

prev3 <- 0.10
c <- sapply(1:5 ,function(p) rbinom(500,1,prev3))

matrixA<- cbind(a,b,c)

I would like to plot a histogram of matrix A showing the distribution of the 1s: y axis showing frequency of 1s and x axis showing matrix column numbers. How do I do this? I have tried hist(), plot() and colsums() function but they dont make sense.
Thank you for your help!

prev1 <- 0.10
a <- sapply(1:5 ,function(p) rbinom(500,1,prev1))

prev2 <- 0.80
b <- sapply(1:5 ,function(p) rbinom(500,1,prev2))

prev3 <- 0.10
c <- sapply(1:5 ,function(p) rbinom(500,1,prev3))

matrixA<- cbind(a,b,c)
table(matrixA)
#> matrixA
#>    0    1 
#> 4969 2531
ones <- colSums(matrixA)/dim(matrixA)[1]
zeros <- (dim(matrixA)[1] - colSums(matrixA))/dim(matrixA)[1]
hist(ones)

hist(zeros)

Created on 2023-07-04 with reprex v2.0.2

Something like this?

matrixA |>
  as_tibble() |>
  set_names(str_c("c", str_pad(1:15, side = "left", pad = "0", width = 2))) |>
  pivot_longer(everything(), names_to = "column", values_to = "value") |>
  group_by(column) |>
  summarise(f_one = mean(value)) |>
  ggplot(aes(x = column, y = f_one)) +
    geom_col() +
    scale_y_continuous(limits = c(0, 1)) +
    theme_bw()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.