-->

R / d3heatmap - is there a way to rotate the axis

2019-08-08 15:50发布

问题:

In the R d3heatmap package, is there a way to rotate the x axis label to be 90 degrees (perpendicular to the axis) instead of 45?

回答1:

I couldn't find an easy option to pass a rotation value to the function, but you can go into the innards of the d3heatmap package and change the rotation in the javascript code.

Run ".libPaths()" in R to find where your R packages and their data are stored. In my case I found the d3heatmap package in '/home/nick/R/x86_64-suse-linux-gnu-library/3.1/d3heatmap/'.

The file you are looking for is something like '/home/nick/R/x86_64-suse-linux-gnu-library/3.1/d3heatmap/htmlwidgets/lib/d3heatmapcore/heatmapcore.js'

Open the file with a text editor. There are two places where there is a setting: "rotate(45),translate(6, 0)".

So search for "rotate(45)" and replace "45" with "90" in both places. You'll also have to change "translate(6, 0)" to something that places the text better. I found that "translate(12, -20)" (shifting the text down by 12 pixels and right by 20 pixels) worked well using the "mtcars" dataset as a test.

d3heatmap with vertical row labels

When using datasets with more or less rows, you'll have to play around with the right translation, changing it a bit from -20 so that things align correctly. There is probably a way to do this automatically, but I mostly just know R, not javascript, so I don't know how to do it.