Tobias
Tobias

Reputation: 574

Change tile size and color in dependence of two different columns

I try to create a geom_tile plot where I want to set the color and the size of the tiles in dependence of two different variables/columns. I found, that I can use geom_tile and geom_point together but my code is not generating what I expect (see example code and image). I also know, this is because I use different columns so maybe I need to go a totally different way but I couldn't figure out how to do it.

Example code

Generate data:

var_1 = rep(c("a", "b", "c"), 3)
a = rep("a", 3)
b = rep("b", 3)
c = rep("c", 3)
var_2 = c(a, b, c)

var_3 = c(1, 0, 0.5, 1, 1, 0, 1, 1, 0.5)
var_4 = c(0.5, 1, 1, 1, 0, 0.5, 0, 0, 1)

data_source <- data.frame(matrix(nrow = 9, ncol = 4))
data_source$X1 = var_1
data_source$X2 = var_2
data_source$X3 = var_3
data_source$X4 = var_4

Create the plot:

library(ggplot2)
connectivity_plot <- data_source %>% ggplot(
  aes(
    X1,
    X2,
    fill = X3
  )
) +
  geom_tile(
    color = "black",
    lwd = 1.5,
    linetype = 1
  ) +
  geom_point(
    aes(size = X4),
    shape = 19
  ) +
  labs(
    x = "x",
    y = "y"
  ) +
  scale_fill_gradientn(
    guide = "legend",
    breaks = c(1.0, 0.75, 0.5, 0),
    labels = c("TRUE", "TRUE WITH WARNINGS", "NOT TESTED", "FALSE"),
    colours = c(
      "red",
      "white",
      "yellow",
      "green"
    ),
    values = c(
      0,
      0.5,
      0.75,
      1
    )
  ) +
  theme(
    axis.text = element_text(size = 18),
    axis.title = element_text(size = 22, face = "bold"),
    legend.text = element_text(size = 18),
    legend.title = element_text(size = 20, face = "bold"),
    legend.key.size = unit(1, "cm"),
  ) +
  guides(
    fill = guide_legend(title="WORKS")
  )

This is the result of the code:

enter image description here

So is it possible to connect the geom_point part with the geom_tile as those are currently plotted independently?

Or in general:

What I would like to receive is that the tiles change in size in dependence of column X4 (does not work) and change their color in dependence of column X3 (already works).

P.S.: I tried to add the size to the general aes or to the geom_tile part but nothing happened.

Upvotes: 0

Views: 374

Answers (2)

Allan Cameron
Allan Cameron

Reputation: 174303

The problem is that what you are describing isn't really what geom_tile, which by definition is a fixed grid. You can use a filled square point shape instead, using scale_size_identity, then add a new size scale for the points using ggnewscale:

data_source %>% 
  ggplot(aes(X1, X2, fill = X3)) +
  geom_point(shape = 22, aes(size = X4 * 75)) +
  scale_size_identity() +
  scale_fill_gradientn(guide = "legend",
    breaks  = c(1.0, 0.75, 0.5, 0),
    labels  = c("TRUE", "TRUE WITH WARNINGS", "NOT TESTED", "FALSE"),
    colours = c("red", "white", "yellow", "green"),
    values  = c(0, 0.5, 0.75, 1)) +
  ggnewscale::new_scale("size") +
  geom_point(aes(size = X4), shape = 19) +
  labs(x = "x", y = "y", fill = "WORKS") +
  theme(axis.text       = element_text(size = 18),
        axis.title      = element_text(size = 22, face = "bold"),
        legend.text     = element_text(size = 18),
        legend.title    = element_text(size = 20, face = "bold"),
        legend.key.size = unit(1, "cm")) +
  guides(fill = guide_legend(override.aes = list(shape = 22, size = 10)))

enter image description here

Upvotes: 1

stefan
stefan

Reputation: 125208

With the size aes you set the line width of the outline. To change the size of the tile itself you could map on the width and height aes:

library(ggplot2)

ggplot(data_source,
  aes(
    X1,
    X2,
    fill = X3
  )
) +
  geom_tile(
    aes(width = X4, height = X4),
    color = "black",
    lwd = 1.5,
    linetype = 1
  ) +
  geom_point(
    aes(size = X4),
    shape = 19
  ) +
  labs(
    x = "x",
    y = "y"
  ) +
  scale_fill_gradientn(
    guide = "legend",
    breaks = c(1.0, 0.75, 0.5, 0),
    labels = c("TRUE", "TRUE WITH WARNINGS", "NOT TESTED", "FALSE"),
    colours = c(
      "red",
      "white",
      "yellow",
      "green"
    ),
    values = c(
      0,
      0.5,
      0.75,
      1
    )
  ) +
  theme(
    axis.text = element_text(size = 18),
    axis.title = element_text(size = 22, face = "bold"),
    legend.text = element_text(size = 18),
    legend.title = element_text(size = 20, face = "bold"),
    legend.key.size = unit(1, "cm"),
  ) +
  guides(
    fill = guide_legend(title="WORKS")
  )

Upvotes: 1

Related Questions