This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Create a plot based on binary input of 1s and 0s.

Hello,

I have a tsv file with 1s and 0s. It has about 20 rows and 100 columns. I have created a dataframe. Now, I want to create a plot/graph which can show a particular colour/pattern for all 1s and another for the 0s with rownames in x-axis and column names along the y-axis. Is this possible in R ? I tried creating normal plots but this gives error saying "rows and columns are not equal".

Thanks in advance.

r script plot

Thanks for your reply. But this doesn't work for my pre-existing matrix

I do not know what 'doesn't work' means. Also, did you not consider that you may have to write some extra lines of code to adapt a pre-existing solution to your own data-set? In bioinformatics, there is never a 'one size fits all' solution'. You have to wrestle with and 'wrangle' your data. Punch it a few times. It neither helps that you have done neither of the following:

  • provided a sample of your dataset
  • provided the code that you have already tried

I have been trying different plot and methods with my data already. As mentioned, I have a binary file with 0s and 1s. I tried using this file as a dataframe and as a matrix.

When I used my file as a matrix, I used the below command w.r.t the link provided by you;

matrix1 <- matrix(Y, nrow = 20, ncol = 100) colour <- c("green", "red") levelplot(matrix1, col.regions=colour, margin = FALSE)

But it gave me an empty plot. Thanks once again for your reply.

1 answer

Simplest way, using image plot:

image(x, col = c("red", "blue"), main = "base image plot")

And for more flexibility, we can try ggplot:

library(ggplot2)

#convert wide to long format
plotDat <- reshape::melt(x)

#and plot
ggplot(plotDat, aes(X1, X2, fill = c("red", "blue")[ value + 1 ])) +
  geom_tile() +
  scale_fill_identity() +
  theme_minimal() +
  ggtitle("ggplot geom_tile")

Example data

set.seed(1); x <- matrix(sample(0:1, 25, replace = TRUE), nrow = 5)
colnames(x) <- paste0("col", 1:5)
rownames(x) <- paste0("row", 1:5)
x
#      col1 col2 col3 col4 col5
# row1    0    1    0    0    1
# row2    0    1    0    1    0
# row3    1    1    1    1    1
# row4    1    1    0    0    0
# row5    0    0    1    1    0

Thank you very much for your reply. The ggplot works perfectly.

Log in to answer this question.