-1

I made a heatmap. The code I used is:

 heatmap(t(data.matrix(survey)))

enter image description here

I don't need anything on x-axis. In plots, the following command would delete the numbers in x-axis:

  xaxt='n'

Also, if I want to add a chart at the top (which tells about the representation of colors - like yellow means lower values and red means higher), how can I do that? I have no idea so I didn't even try. The only thing I can think of is 'scale' but that didn't work.

Lastly, I tried to change the color (green and red) and for that I used:

  mycol = c("green","red")
  heatmap(t(data.matrix(zscoreplus)),col=mycol)

enter image description here Unlike 1st pic, there are no colors in between. (1st one had a lot more variety.) What I was trying to get was red, light red, reddish-green, green, dark green, etc...

p.s. For some reason, gplots and heatmap.2 are not installed and R can not find those packages.

Ay_M
  • 193
  • 1
  • 2
  • 8
  • Wht do you mean by your p.s? Do you want a base solution? – agstudy Jul 16 '13 at 04:27
  • nvm...found gplots. Thnx tho – Ay_M Jul 16 '13 at 04:36
  • 1
    -1 for lack of effort. "I have no idea so I didn't even try" isn't very impressive. StackOverflow helps those who demonstrate that they have tried to help themselves. You need to do some of your own research and check the basic R documentation. Even googling 'how to install package in R' gives you plenty of advice to intall packages like `gplots`. Also look at [this post](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for advice on asking questions properly. You've asked four questions here on SO and none seem to show much thought. – SlowLearner Jul 16 '13 at 05:08
  • I respect what you said...and yes I made a mistake installing a package (which is a natural thing) but figured it out on my own (if u see my last comment). I asked 3 questions before and all of them were basic questions. Except 1st question (as it was my 1st question and I wasn't sure how SO works), I made effort in other 2 which you can see if you open those questions. The code I wrote work with basic plots but somehow they r not working with heatmap. Now, if you can't answer then don't criticize either! – Ay_M Jul 16 '13 at 05:52
  • 3
    I strongly recommend that you *do not* use red & green. About 10% of men are red-green colorblind. If you don't like red & yellow, red & blue can work nicely. – gung - Reinstate Monica Jul 16 '13 at 13:05

2 Answers2

2

Instead of using the basic heatmap() function, you could load the gplots package and use heatmap.2() - in your case same syntax - to get a color key. Let me know if you have any further questions about the heatmap.2() package.

EDIT:

Sorry, didn't read that you cannot install gplots. Is it because of limited admin rights?

Unfortunately, heatmap() is kind of limited regarding the color key.

But for the red -> green problem I have a solution for you. To create your own color palette, try

my_palette <- colorRampPalette(c("red", "green"))(n = 1000)

and then use it as color in your heat map: heatmap(..., col = my_palette, ...)

How important is clustering in your case? If you don't need clustering, you can use the levelplot() function (comes with R), which has a nice color key representation.

EDIT2: Regarding the color "scale" problem. I assumed that you mean something like legend according to the description in your first post. So is something like in the screenshot below that you want?

enter image description here

EDIT3 Regarding the x-labels: Unfortunately, there is no direct option in heatmap.2() to turn those off. THose x-labels are the colnames for your matrix that you read in. By xlabel you would just control the general description of the axis (it is turned off by default). Here is a Screenshot that shows what I mean when xlabels is used:

enter image description here

Maybe you could just give your matrix empty ( " " ) colnames. That should help. On the other hand, I am sorry to ask you this, but this doesn't make sense if you are using clustering. How would you know which is which? An alternative solution is to simply crop the region or code from the pdf, or svg once you saved the heat map. Shouldn't take more than 5 seconds.

  • Now that you were able to install `gplots` you can also make use of the `colorRampPalette` there. Similarly as described for `heatmap()` above. –  Jul 16 '13 at 04:56
  • ok. I used heatmap.2()...but still I dont know how to remove x-labels and how to add color scale. – Ay_M Jul 16 '13 at 05:11
  • For the color scale question: I added a screenshot in my answer section. –  Jul 16 '13 at 13:00
  • You can find my comments to the x axis query under EDIT3 in the same answer article above –  Jul 16 '13 at 13:08
  • 1
    @Edit 2: Yes. I thought putting key=TRUE but it didn't do anything. Clustering - To see how age groups correlate with each other. Otherwise levelplot() works perfectly. Edit 3: I tried xlab and it works the way you defined it. Running colnames="" gives an error. "colname" is not a graphical parameter – Ay_M Jul 16 '13 at 19:27
  • sorry, I forgot to mention that `colnames` is a general R function. Use it independently from the heat map functions. –  Jul 16 '13 at 19:41
  • For example, `colnames(your_datamatrix) <- c(2003:2012)` –  Jul 16 '13 at 19:42
  • You don't even have to use key=TRUE in order to display the legend, it should be the default character. I don't know what the issue can be here...Hm, here you find a ugly formatted tutorial (http://www.packtpub.com/article/creating-your-first-heat-map-in-r) maybe you could follow through it (skip everything up to the how to it section) and see if you get a legend for the `AirPassenger` sample dataset (a data set that is included in R). So we can rule out that there is a general problem with the `gplots` package. –  Jul 16 '13 at 19:47
  • I used the transpose. So dim names are y-axis in my example. The numbers in x-axis are the ID numbers assigned by R. Those numbers are not even in my data-set. – Ay_M Jul 16 '13 at 19:55
  • That's what I thought. I think it will just enumerate your columns automatically if you don't use any row or colnames. But I guess if you just use blank (whitespace) strings for colnames you should be fine –  Jul 16 '13 at 20:15
1

Regarding your problems to install gplots: You forgot the quotes.

require(gplots) Loading required package: gplots Warning message: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called ‘gplots’ > install.packages(gplots) Error in install.packages : object 'gplots' not found – ayesha malik 8 mins ago

Try

install.packages("gplots", dependencies = TRUE)

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
  • Yes, I figured that out. Thanks. Will try gplot now. Clustering is important in my case. – Ay_M Jul 16 '13 at 04:48
  • 1
    Good to hear. You will like `heatmap2()`, the syntax is almost identical to `heatmap()`, but has a lot of additional nice features. If you want to know something particular, let me know. –  Jul 16 '13 at 04:55