0

I'm trying to create an R ggplot2 geom_point plot where the points are gradient filled according to a numeric value, sized according to a numeric value, and border colored according to a boolean value.

Here's an example dataset:

library(dplyr)
library(ggplot2)

set.seed(1)
levels.mat <- matrix(data = runif(7*6, 1, 3), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))
scores.mat <- matrix(data = runif(7*6, 0, 10), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))
sig.mat <- matrix(data = sample(c(F, T), 7*6, replace = T), nrow = 7, ncol = 6, dimnames = list(paste0("I", 1:7), paste0("P", 1:6)))

df <- dplyr::left_join(reshape2::melt(levels.mat) %>% dplyr::rename(x = Var1, y = Var2, level = value),
                       reshape2::melt(scores.mat) %>% dplyr::rename(x = Var1, y = Var2, score = value)) %>%
  dplyr::left_join(reshape2::melt(sig.mat) %>% dplyr::rename(x = Var1, y = Var2, sig = value))

Here's my ggplot2 function:

ggplot(df, aes(x = x, y = y))+
  geom_point(aes(fill = level, size = score, color = sig))+
  scale_fill_gradient(low = "lightgray", high = "darkred", aesthetics = "fill")+
  scale_color_manual(breaks = c(F, T), values = c("black", "green"), aesthetics = "color")+
  theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Which seems to ignore the fill part and gives: enter image description here

Any idea how to get the points filled with the "lightgray" - "darkred" gradient according to df$level and their borders colored with the "black"/"green" colors according to df$sig?

0

1 Answer 1

1

This does it:

ggplot(df, aes(x = x, y = y))+
  geom_point(aes(fill = level, size = score, color = sig),shape=21)+
  scale_fill_gradient(low = "lightgray", high = "darkred")+
  scale_color_manual(breaks = c(F, T), values = c("black", "green"))+
  theme_minimal() + theme(axis.title.x = element_blank(), axis.title.y = element_blank(), axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.