A75awh
A75awh

Reputation: 37

Change axis labeling in R heatmap

I have a heatmap drawing a dataframe that has 3 columns and 100 rows. X and Y axes are representing X and Y coordinates. When I create the map, it shows every location and becomes unreadable.

Both axis range from 0-100. I would like both axis to just go 0,10,20,30,40,50,60,70,80,90,100. Can anyone help me clean this up? Thanks.

ggplot( data = CombinedDF, mapping = aes( x = factor(allPoints.xLocs), y = factor(allPoints.yLocs) ) ) + 
  geom_tile( aes( fill = sum_patch ),  colour = "white") + labs( x = "X-Coordinate", y = "Y-Coordinate") +
  theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))


Here is the sample of the input dataframe "CombinedDF"

  allPoints.xLocs allPoints.yLocs sum_patch
1   74.106128071    62.2365805  13
2   70.786698116    58.8928561  13
3   65.543694422    33.8426416  3
4   8.647094783     50.1071865  2
5   95.822909172    11.3294181  4
6   91.324434988    42.4157078  5
7   96.444815141    68.6108005  13
8   13.105758978    83.1488258  7
9   92.958515161    74.3948395  13
10  76.149455458    98.8090307  4

b

When I remove "factor" I get this (axis are correct but no data?): enter image description here

Upvotes: 0

Views: 5261

Answers (2)

dc37
dc37

Reputation: 16178

Your data are not fully complete to make a heatmap. You don't have a single value for each combinations of X and Y.

Here, I reproduce your example by doing:

DF <- data.frame(X = runif(100,0,100),
                 Y = runif(100,0,100),
                 sum = sample(0:30,100, replace =TRUE))

You can plot them as point:

ggplot(DF,aes(x = X, y = Y, color= sum))+
  geom_point()

enter image description here

An another possibility if you want to make an heatmap is to create some group intervals (0-10 / 10-20 / ...). You can do that by using cut functions:

library(dplyr)

DF <- DF %>% mutate(CutX = cut(X,seq(0,100, by = 10)),
              CutY = cut(Y,seq(0,100, by = 10)))

         X          Y sum     CutX    CutY
1 19.48048 79.1970915  17  (10,20] (70,80]
2 42.47574 34.1226793  10  (40,50] (30,40]
3 43.99754 25.7454872   7  (40,50] (20,30]
4 90.88465  0.3961523  18 (90,100]  (0,10]
5 46.26645 38.0338865  25  (40,50] (30,40]
6 93.15978 59.9426569  15 (90,100] (50,60]

Then, you need to expand this dataframe for each combinations of X and Y by doing:

Expand_DF <- expand.grid(CutX = unique(DF$CutX), CutY = unique(DF$CutY))
Expand_DF$Sum <-NA

      CutX    CutY Sum
1  (10,20] (70,80]  NA
2  (40,50] (70,80]  NA
3 (90,100] (70,80]  NA
4  (30,40] (70,80]  NA
5  (80,90] (70,80]  NA
6  (70,80] (70,80]  NA

Finally, you can bind them together and if several values are in the same interval, you can calculate the mean and finally plot them in ggplot by doing:

library(dplyr)
library(ggplot2)

DF %>% bind_rows(.,Expand_DF) %>% 
  group_by(CutX, CutY) %>%
  summarise(Sum = mean(sum,na.rm = TRUE)) %>%
  ggplot(aes(x = CutX, y = CutY, fill = Sum))+
  geom_tile(color = "black")+
  scale_fill_gradient(na.value = "white")

enter image description here

Does it answer your question ?

Upvotes: 1

Leander
Leander

Reputation: 859

Add

scale_x_continuous(n.breaks=10, limits=c(0,100))+

scale_y_continuous(n.breaks=10, limits=c(0,100))

to your ggplot.

More info here

Add labes=seq(1,100,10) to both scale functions to modify the label text to be 0-100 with steps of 10.

Upvotes: 1

Related Questions