Home > OS >  How to show all elements in the axis label of a heat map in R?
How to show all elements in the axis label of a heat map in R?

Time:11-29

How could I to show all years in a heat map label in R? Some years in "Y" label are missing. Is it possible to put all the years with an angle or with alternate displacements?

year = seq(from=1971,to=2020,by=1)
jan = runif(n = 50, min = -3, max = 3)
feb = runif(n = 50, min = -3, max = 3)
mar = runif(n = 50, min = -3, max = 3)
apr = runif(n = 50, min = -3, max = 3)
may = runif(n = 50, min = -3, max = 3)
jun= runif(n = 50, min = -3, max = 3)
jul= runif(n = 50, min = -3, max = 3)
aug= runif(n = 50, min = -3, max = 3)
sep= runif(n = 50, min = -3, max = 3)
oct= runif(n = 50, min = -3, max = 3)
nov= runif(n = 50, min = -3, max = 3)
dec= runif(n = 50, min = -3, max = 3)

df = data.frame(year,jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec)
head(df)
rownames(df) <- df$year
df=df[,-1]
df=as.matrix(df)
head(df)
my_palette <- colorRampPalette(c("red", "white", "blue"))(n = 9)

heatmap.2(df, scale = "none", col = my_palette, 
          trace = "none", density.info = "none", main = "Bohicon")

Thank you

CodePudding user response:

heatmap.2 has an option cexRow to change the size of the labels.

heatmap.2(df, scale = "none", col = my_palette, trace = "none", 
  density.info = "none", main = "Bohicon", cexRow=.5)

Its value is estimated with this 0.2 1/log10(nr) function, but if it gets too low it can be set manually to fit ones needs.

CodePudding user response:

By default in R, some axis labels are suppressed if they would clash with each other. The easiest way round this is simply to make your plotting window larger. For example, if my plotting window is small, many of the y axis labels are suppressed:

gplots::heatmap.2(df, scale = "none", col = my_palette, 
           trace = "none", density.info = "none", main = "Bohicon")

enter image description here

You can see there are only 17 labels on the y axis.

If I now drag the plotting window to make it taller, without even re-running my code, I get all 50 labels:

enter image description here

  • Related