How to make a color plot out of three columns from a dataframe in R?
1
0
Entering edit mode
4.7 years ago
Biologist ▴ 230

I have a dataframe "df" like following (example):

ID    type  group
A3EP    1    M
A3MA    2    M
A459    3    M
A3I1    1    N
A9D2    2    N
A3M9    3    N
A7XP    1    O
A4ZP    2    O
A4ZP    3    O

Original table is with 1000 ID's. For this I have used heatmap.3 function. Ofcourse this is not a heatmap but I tried getting the color plot using this function.

clab=cbind(df$type,df$group)
colnames(clab)=c("Type","Group")
heatmap.3(clab, margins=c(6,17), Rowv=FALSE, Colv=FALSE)

And this gave me a plot like following:

![enter image description here][1]

The upper part in the image is group "M", "N", "O". And below that it shows "type". But I see both "groups" and "types" in the same colors. What I need to do to get a different colors for "groups" and "types". Along with that I want to see "ID" names below "types".

Can anyone help me how to do this? Thank you

r heatmap plot • 2.0k views
ADD COMMENT
0
Entering edit mode

It'd be better to make a plot for each and put them side by side.

ADD REPLY
0
Entering edit mode

Yes, I can do that. Even then the colors would be same I guess. Instead if I assign different colors for group and type it would work right. And I don't have any idea how to do that and also I need the ID names below types.

ADD REPLY
0
Entering edit mode

I mean plot group and type separately, then assigning different colors is trivial. If they get different colors, they below in different subplots.

ADD REPLY
0
Entering edit mode

No, it didn't work. it throws an error.

ADD REPLY
0
Entering edit mode

I also tried with "ggplots2" with some setup data. But it didn't work for my original data.

ADD REPLY
0
Entering edit mode

What did you try? Can you post some code?

ADD REPLY
0
Entering edit mode

I had posted it below. Please check it

ADD REPLY
0
Entering edit mode

I just want to know how to assign different colors for the columns "group" and "type" and also want to see the ID names below the type.

ADD REPLY
0
Entering edit mode

I don't understand what you are trying to plot. Your example data frame contains three columns. What do you want on the X axis? On Y? Which column do you want to map as color?

ADD REPLY
0
Entering edit mode

I want to check the "type" comes under which "group". And also need ID names below type.

If you see the plot in the upper part RED color is group "M" Saffron is group "N" and white color is "O".

Down part are the "types". Red -1, saffron - 2, white - 3.

What I need is different colors for Groups and types. Also need the ID names below the types.

ADD REPLY
0
Entering edit mode

but if you have 1000s of IDs as you say, then the plot will become very confused. You will not be able to read the ID names, unless you have a very long plot.

ADD REPLY
0
Entering edit mode

Yes, Ofcourse I will be having a long plot. small or long is not the problem now. I just gave a random number of ID's. I totally have 350 ID's. Any idea how to do this?

ADD REPLY
0
Entering edit mode

it didn't work

Then you did it wrong.

ADD REPLY
0
Entering edit mode

No, that is different data.

df <- structure(list(ID = structure(c(1L, 4L, 5L, 2L, 8L, 3L, 7L, 6L), .Label = c("A3EP", "A3I1", "A3M9", "A3MA", "A459", "A4ZP", "A7XP", "A9D2"), class = "factor"), type = 1:8, group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), class = "factor", .Label = "M")), .Names = c("ID", "type", "group"), row.names = c(NA, -8L), class = "data.frame")

mypalette <- rainbow(8)
barplot(rep(0.5,8), width=1, space=0, col=mypalette, axes=F)
text(df$type-.5, .2, df$ID,  srt=90)
rect(0, .4, 8, .5, col="red")
text(4, .45, "M")

This plot looks different from what I want for my original data. So, if you can help me how to set up this to my data please let me know.

ADD REPLY
0
Entering edit mode
  1. Plot the first plot.
  2. Plot the second plot.
  3. Make them next to each other.

Those are the steps for you to follow. If you need further help I would suggest asking in an R forum, since this isn't really in the scope of this site.

ADD REPLY
0
Entering edit mode

As you said I tried doing first plot and then second plot. In the first plot group "M" is with red colour and types 1 is red and 2,3 are yellow. The color key need to be changed. that is what I just want. Anyways thanks for the reply. Will ask in R forum.

ADD REPLY
5
Entering edit mode
4.7 years ago

I haven't understood very well what you are asking, but I think this code will help you.

I switched the X and Y axis because if the plot contains 1000s of values, putting them all on the X axis will be unreadable, unless you have a very wide plot.

Note that your input data contains some duplicated values (in this example: A4ZP has both an "M" and a "O" entry"). You didn't specify what should happen in these cases, so I've just plotted them twice.

Explanation of the code: you need first to convert your dataframe to a "long" format, which is a standard practice for ggplot2. Then you just use the geom_tile plot.

> library(tidyverse)
> df = data.frame(ID=as.factor(1:9), label=c("A3EP", "A3I1", "A3M9", "A3MA", "A459", "A4ZP", "A7XP", "A9D2", "A4ZP"), type=rep(1:3, 3), group=c(rep("M", 6), rep("O", 3)))
> df
  ID label type group
1  1  A3EP    1     M
2  2  A3I1    2     M
3  3  A3M9    3     M
4  4  A3MA    1     M
5  5  A459    2     M
6  6  A4ZP    3     M
7  7  A7XP    1     O
8  8  A9D2    2     O
9  9  A4ZP    3     O
> df.long = df %>% gather(variable, value, -label, -ID)
Warning message:
attributes are not identical across measure variables; they will be dropped
> df.long
   ID label variable value
1   1  A3EP     type     1
2   2  A3I1     type     2
3   3  A3M9     type     3
4   4  A3MA     type     1
5   5  A459     type     2
6   6  A4ZP     type     3
7   7  A7XP     type     1
8   8  A9D2     type     2
9   9  A4ZP     type     3
10  1  A3EP    group     M
11  2  A3I1    group     M
12  3  A3M9    group     M
13  4  A3MA    group     M
14  5  A459    group     M
15  6  A4ZP    group     M
16  7  A7XP    group     O
17  8  A9D2    group     O
18  9  A4ZP    group     O
> df.long %>% ggplot(aes(y=ID, x=variable, fill=value, label=label)) + geom_tile() + geom_text(data=subset(df.long, variable=="type")) +  geom_text(data=subset(df.long, variable=="group"), aes(label=value))

enter image description here

ADD COMMENT
0
Entering edit mode

Wow. This looks awesome. Will try for my data and will let you know. Thanks

ADD REPLY
0
Entering edit mode

Thank you very much Giovanni. This worked. But it gave me labels of IDs in types and also groups. Now, I don't want them to be seen on the plot. How to remove the labels?

ADD REPLY
1
Entering edit mode

Just remove the geom_text() part that you don't need.

ADD REPLY
0
Entering edit mode

Thank you Very much !!

ADD REPLY

Login before adding your answer.

Traffic: 2131 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6