2

I have this plot that I need to replicate. The problem with this is that there seems to be some kind of grouping here with specific colors. Here are some R code to generate a similar plot.

set.seed(10)
x1 <- rnorm(10^5,0,1) 

x2 <- runif(10^5,0,1)

x2 <- 0.4*x1 + sqrt(1-0.4^2)*x2 + rpois(10^5, 2)

data.frame(x1, x2) %>% ggplot() + geom_hex(aes(x1, x2))

I cant seem to replicate the color shown below screenshor

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
Tea
  • 21
  • 2
  • I believe it is not only the color palette that I am changing but also some subtleties in the color of the plot itself. I am not sure how to describe that with `ggplot2` language, so my title is a bit confusing. Thank you for your input. – Tea Aug 30 '19 at 05:27
  • try `scale_fill_gradient` if you are looking to manually change the colors. – heck1 Aug 30 '19 at 05:45

1 Answers1

1

In this example, it looks like the ones place is mapped to one color channel (maxing out at 10), ditto for tens and hundreds. Each channel is mapped to a geom_hex layer, with the hexagons scaled by size.

I don't think this is currently possible "out of the box" with ggplot, since the geom_hex geom does not allow mapping to size:

Github issue closed in 2018, deciding to add mapping to size for geom_hex, but only for line thickness, not hex size itself: https://github.com/tidyverse/ggplot2/issues/2488

Workaround outside ggplot: https://stackoverflow.com/a/16960960/6851825

That said, we can do something similar with geom_point. It would take some more fiddling to create a custom legend like the example, but should be possible.

# Count within each hex area:
example.hex <- hexbin::hexbin(x1, x2,
                             xbins = 30, IDs = TRUE)
value_by_hexbin <- data.frame(hexbin::hcell2xy(example.hex),
                              cell = example.hex@cell,
                              count = example.hex@count)

# Plot those counts to three layers of point, for each color channel
ggplot(value_by_hexbin, 
       aes(x, y)) +
  geom_point(aes(size = pmin(10, floor(count / 1))), color = "purple") +
  geom_point(aes(size = pmin(10, floor(count / 10))), color = "green") +
  geom_point(aes(size = pmin(10, floor(count / 100))), color = "red") +
  scale_size_area(max_size = 3) +
  coord_equal()

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Hi Jon, thank you for your recommendations and examples. However, I think the problem i have here is that fill colors in each hexagon and not the size itself. This makes me wonder if the plot i have was created somewhere else and not with `ggplot2`. I will develop on what you provided to get as close as possible, but I suspect the coloring has something to do with `fill` – Tea Aug 30 '19 at 23:17
  • Yes, geom_hex can let you control fill color, but I was trying to replicate the specific effect in your example, with varying sizes of colored hexagon inside each bin. – Jon Spring Aug 31 '19 at 02:24