Avoid using $ within ggplot, try this instead:
ggplot(df2, aes(Gene, Symbol, label = Count, colour = Count, size = Count)) +
geom_text()+
scale_colour_distiller(palette = "RdYlGn")+
theme_bw() +
scale_size_identity()
Dear Friends,
I have a matrix like this:
Gene BRCA THYM TGHJ
ACC 23 21 7
XTG 12 13 9
CFG 45 4 8
The numbers are the snp count from vcf file.
I want to plot this in the form of a matrix with numbers colored based on their count; for example the highest number is colored in "Red" and then gradually the intensity of the color decreases with the decrease in number, so in this 45 is colored "Red" and 4 is colored with a very light color. Please let me know if am clear.
I am looking to plot this matrix using ggplot2, but other ways in R are also very welcome. A matrix like this:

library(tidyr)
df2=gather(df1, Symbol,Count, -Gene)
library(ggplot2)
ggplot(df2, aes(Gene,Symbol, size = Count, label = Count)) +
geom_text(size = df2$Count, aes(colour=df2$Count))+
scale_colour_distiller(palette="RdYlGn")+
theme_bw()
I remember seeing a package for this exact task, but can't find it. So here is the starting steps, it will require more work to make it as pretty as you have in your example plot:
# example input
df1 <- read.table(text = "
Gene BRCA THYM TGHJ
ACC 23 21 7
XTG 12 13 9
CFG 45 4 8", header = TRUE)
library(ggplot2)
library(dplyr)
library(tidyr)
plotDat <- gather(df1, key = "Gene2", value = "value", -Gene)
ggplot(plotDat, aes(Gene, Gene2, col = value, fill = value, label = value)) +
geom_tile() +
geom_text(col = "black") +
theme_minimal() +
scale_fill_gradient2(low = "white", mid = "yellow", high = "red") +
scale_color_gradient2(low = "white", mid = "yellow", high = "red")

Thanks! it gives me error:
Mapping must be created by aes() or aes_()".Could you please let me know what could be the issue? I have 100 genes to plot. Thanks!
You're probably missing a parenthesis somewhere. Can you check if you formatted the code properly?
This is my code; can you please let me know what could be wrong here?

Posting screenshots of code is not useful for others to help debug the code. Please copy/paste actual text using the code formatting button.
You see how zx8754 used white space to write readable code? You can use a similar approach too - it is always better to write code in smaller, more logical chunks per line.
I want to write, or at least copy-paste, but the platform am working on does not allow me to copy-paste, otherwise I will have to write the whole code here from scratch. Can you see if there is any error that is causing problem? Thanks.
Thanks much! I have come to a point where I am getting the plot; however, I dont see the numbers in the matrix:

And, my code is:

Could you please let me know how can I make the numbers visible in the matrix? Thanks!
Thanks! it works :)
cpad, is there a go-to reference for the order to be used? How do you determine what goes first and what goes after?
layers (as in Adobe flash)
I usually think of them as OHP slide transparencies. Really old school, I know. The analogy doesn't hold up to how data is shared either.
I am not sure what you meant by data sharing. However, following is my primitive understanding about ggplot: I think data is shared both globally and locally within ggplot. If one passes the data to main function ggplot (ggplot function), that would stay in entire plot and all the geoms has access to that data. If it is used a geom, it is restricted to that geom only. If both main data and geom share different data/visualization for some reason, geom will have more preference, at least for aes, in rendering the plot. Ram
Log in to answer this question.
i think you'd have better luck searching Stack Overflow.
Thank you everyone for your informative suggestions and solutions which made this post a success :-).