1
library(ggpointdensity)
library(viridis)
ggplot(data = diamonds, mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw() + 
  facet_grid(~ cut, scales = 'free')

Notice from the result below, all the facets are colored based on one common global density scale. How can I force such that each facet has their own scale, therefore would be colored based on only their number of data points, not relative to the other data points from other facet. enter image description here

My current not so optimize solution is to use patchwork

library(patchwork)
r1 <- ggplot(data = diamonds %>% filter(cut == 'Fair'), mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw()

r2 <- ggplot(data = diamonds %>% filter(cut == 'Good'), mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw()

r3 <- ggplot(data = diamonds %>% filter(cut == 'Very Good'), mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw()

r4 <- ggplot(data = diamonds %>% filter(cut == 'Premium'), mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw()

r5 <- ggplot(data = diamonds %>% filter(cut == 'Ideal'), mapping = aes(x = jitter(carat), y = jitter(price))) +
  geom_pointdensity(method = 'default') +
  scale_color_viridis() + theme_bw()

r1 + r2 + r3 + r4 + r5 

enter image description here

Afiq Johari
  • 1,372
  • 1
  • 15
  • 28
  • The solution listed https://stackoverflow.com/questions/54009145/free-colour-scales-in-facet-grid is a bit more extensible for multiple groupings. But I don't believe there's a simple way of achieving this result. – ravic_ May 08 '20 at 23:35
  • @ravic_ the other solution I found is here https://rud.is/b/2016/02/14/making-faceted-heatmaps-with-ggplot2/ . Although I just find that there're not easily lisible somehow – Afiq Johari May 09 '20 at 13:22

0 Answers0