Mir Henglin
Mir Henglin

Reputation: 669

Controlling tile radius with ggplot2 geom_tile

I am trying to create a heatmap using ggplot2 that controls for two different continuous variables. One only has values greater than 0. For that variable, I would like to control the 'radius' of the tile such that the maximum value is a tile that takes up all available space, and 0 means that the tile will have an area of 0. So far, using aes(size = ...) has not given any results, and I feel lost as to what to do.

Example:

library(dplyr)
library(ggplot2)

dat <-
    data_frame(
        factor_1 = as.factor(rep(c(1, 2, 3, 4, 5), 5)),
        factor_2 = as.factor(rep(c(1, 2, 3, 4, 5), each = 5)),
        value_1  = rnorm(25),
        value_2  = runif(25)
    )

ggplot(dat) +
    geom_tile(aes(x = factor_1, y = factor_2, fill = value_1, size = value_2))

produces

heatmap

As we can see, there is a size legend that scales with value_2, but the tiles are all uniformly sized.

The result I would like to obtain is similar to the first two examples at this link for the corrplot function where the circles or squares can vary in size, leaving a white border around them.

Upvotes: 0

Views: 949

Answers (1)

Marius
Marius

Reputation: 60160

You can use the width and height aesthetics:

ggplot(dat) +
    geom_tile(aes(x = factor_1, y = factor_2, fill = value_1, 
                  width = value_2, height = value_2))

For some reason I get warnings about these being "unknown aesthetics", but they are obviously working to set the width and height of the tiles.

Upvotes: 3

Related Questions