Question: Create a plot based on binary input of 1s and 0s.
1
gravatar for Inquisitive8995
4 months ago by
Inquisitive8995130 wrote:

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.

plot script R • 221 views
ADD COMMENTlink modified 4 months ago by zx87547.9k • written 4 months ago by Inquisitive8995130

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

ADD REPLYlink modified 4 months ago • written 4 months ago by Kevin Blighe46k

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

ADD REPLYlink written 4 months ago by Inquisitive8995130

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
ADD REPLYlink modified 4 months ago • written 4 months ago by Kevin Blighe46k

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.

ADD REPLYlink written 4 months ago by Inquisitive8995130
3
gravatar for zx8754
4 months ago by
zx87547.9k
London
zx87547.9k wrote:

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
ADD COMMENTlink modified 4 months ago • written 4 months ago by zx87547.9k

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

ADD REPLYlink written 4 months ago by Inquisitive8995130
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1406 users visited in the last hour