Wow. This looks awesome. Will try for my data and will let you know. Thanks
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
1 answer
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))

Log in to answer this question.
It'd be better to make a plot for each and put them side by side.
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.
I mean plot group and type separately, then assigning different colors is trivial. If they get different colors, they below in different subplots.
No, it didn't work. it throws an error.
I also tried with "ggplots2" with some setup data. But it didn't work for my original data.
What did you try? Can you post some code?
I had posted it below. Please check it
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.
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?
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.
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.
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?
Then you did it wrong.
No, that is different data.
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.
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.
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.