Create a plot based on binary input of 1s and 0s.
1
1
Entering edit mode
2.5 years ago

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".

R script plot • 3.4k views
0
Entering edit mode

I am pressed for time, but you may consider a levelplot (from lattice package), as this example shows: Specifying colours for binary data using levelplot

0
Entering edit mode

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

0
Entering edit mode

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
0
Entering edit mode

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.

3
Entering edit mode
2.5 years ago
zx8754 10k

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

0
Entering edit mode