0
votes

I am using geom_tile to create a heatmap. I want to customize the heatmap by highlighting cells that meet a certain criteria. Specifically, I want any cell with a value less than 10 to be set to a lower transparency. Everything above 10 should be alpha=1. I've tried to apply this condition with alpha=ifelse(spinrate >= 10, 1, 0.5), but it seems alpha is not interpreted as I would expect it to be.

Reproducible Example:

spinrates <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/spinrates.csv", stringsAsFactors = FALSE)

ggplot(aes(x=velocity, y=spinrate, fill=swing_miss, label=swing_miss, alpha=ifelse(swing_miss >= 10, 1, .3)), data=spinrates) +
 geom_tile(show.legend = F) +
  geom_text(size=2.25, show.legend = F) +
  scale_fill_distiller(palette = "RdYlGn", direction = 1)

geom_tile

1

1 Answers

0
votes
ggplot(aes(x=velocity, y=spinrate, fill=swing_miss, label=swing_miss, alpha = swing_miss), 
       data=spinrates) +
  geom_tile(show.legend = F) +
  geom_text(size=2.25, show.legend = F) + 
  scale_fill_distiller(palette = "RdYlGn", direction = 1)+
  scale_alpha_continuous(ifelse(spinrates$swing_miss>=10, 1,0.5))