Independent alpha for each plot within facet_grid based on density

本秂侑毒 提交于 2021-02-08 04:36:38

问题


I am constructing a facet_grid using stat_hexbin however I would like the alpha value to be independent for each of the facet plots.

I am currently using the following code:

ggplot (data, aes (x, y , fill = z)) +
  stat_binhex(bins=20, aes(alpha = ..count..)) +
  facet_grid(. ~ z) +
  guides(alpha = F) +
  coord_equal() + 
  theme_bw()

which produces the following plot:

However, the alpha value, which is defined by ..count.. doesn't work when applied outside of the aes within stat_binhex.

I would like to show that there is some clustering in the 90 grouping on the right, around the (100,0) region, but the hexes are very pale, since there is such heavy clustering around (0,0) in the 10 grouping (leftmost plot) which skews the alpha.

Main question: How can I make the alpha independent for each facet, but still connected to count/density to better show the clustering in '70' and '90' groups?

Many thanks!

Data:

# rounded x and y, from 2 days of 365
structure(list(x = c(-24, 41, 43, 14, 9, 30, 8, -14, -45, 42, 
65, 39, 43, 49, 39, 61, -53, -16, 29, 27, 9, 6, -61, 20, 5, -30, 
-10, 75, 94, 28, 70, 44, -11, 26, 29, 33, 26, -35, 20, 40, 7, 
4, 14, 4, -41, -7, -21, 95, 20, 50, 63, 31, 47, 19, 20, 19, 23, 
-25, 29, -8, -73, 13, -82, 4, -29, 3, 9, 3, 35, 45, 64, -14, 
-4, 34, 13, 12, 20, 13, 15, -17, 12, 19, -55, -49, 95, -19, 45, 
94, 23, 29, 22, -91, -39, -35, -3, 63, 2, 5, 30, 62, 1, 4, -61, 
-6, -2, 5, -26, -23, 5, 6, 8, 45, 104, -7, 8, 44, -43, -8, 9, 
12, 29, 30, 69, 90, 12, -28, -10, -9, 49, 60, 32, 43, -11, 12, 
28, 91, 11, 13, 43, 61, 11, 12, 28, 31, 47, 12, 13, 30, 46, 66, 
98, 11, 12, 29, 31, 44, 64, -11, 14, 48, 62, 96, 10, 11, 12, 
29, 67, 30, 93, -10, -9, 44, 101, -28, 34, 46, 10, 27, 30, 61, 
8, 24, -7, -2, 52, 65, 5, -43, 41, 45, 91, -24, -23, 37, 73, 
97, -61, 63, 57, 52, -37, -35, 19, 24, 110, -91, -5, -17, 95, 
13, 85, -52, -50, 78, 30, 37, -8, -27, 19, -78, -75, 52, 42, 
-11, -37, 27, 62, 78, -16, -56, 41), y = c(-100, -95, -95, -92, 
-88, -86, -84, -82, -81, -78, -73, -72, -71, -70, -69, -68, -67, 
-67, -64, -63, -62, -59, -58, -57, -56, -54, -54, -54, -54, -52, 
-52, -49, -48, -48, -48, -47, -46, -45, -45, -45, -44, -42, -41, 
-40, -39, -39, -38, -38, -37, -36, -36, -35, -35, -34, -34, -33, 
-33, -32, -32, -31, -30, -30, -29, -29, -28, -27, -27, -26, -26, 
-26, -26, -25, -25, -25, -24, -23, -23, -22, -22, -21, -21, -21, 
-20, -20, -19, -18, -18, -18, -17, -17, -16, -14, -14, -14, -13, 
-13, -12, -12, -12, -12, -11, -11, -10, -10, -10, -10, -9, -9, 
-9, -9, -9, -9, -9, -8, -8, -8, -7, -7, -7, -7, -6, -6, -6, -6, 
-5, -4, -4, -4, -4, -4, -3, -3, -2, -2, -2, -2, -1, -1, -1, -1, 
0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 10, 
10, 11, 11, 11, 11, 12, 13, 14, 14, 14, 15, 15, 15, 16, 16, 18, 
19, 20, 21, 23, 23, 24, 24, 24, 26, 27, 28, 28, 29, 30, 32, 32, 
32, 36, 36, 41, 42, 44, 48, 48, 50, 51, 57, 60, 62, 76, 76, 85, 
89, 93), z = c(90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 
90, 90, 90, 90, 90, 70, 70, 70, 70, 70, 90, 70, 70, 70, 70, 90, 
90, 70, 90, 70, 50, 70, 70, 70, 70, 70, 50, 70, 50, 50, 50, 50, 
70, 50, 50, 90, 50, 70, 70, 50, 70, 50, 50, 50, 50, 50, 50, 30, 
90, 30, 90, 30, 50, 30, 30, 30, 50, 50, 70, 30, 30, 50, 30, 30, 
30, 30, 30, 30, 30, 30, 70, 70, 90, 30, 50, 90, 30, 30, 30, 90, 
50, 50, 10, 70, 10, 10, 30, 70, 10, 10, 70, 10, 10, 10, 30, 30, 
10, 10, 10, 50, 90, 10, 10, 50, 50, 10, 10, 10, 30, 30, 70, 90, 
10, 30, 10, 10, 50, 70, 30, 50, 10, 10, 30, 90, 10, 10, 50, 70, 
10, 10, 30, 30, 50, 10, 10, 30, 50, 70, 90, 10, 10, 30, 30, 50, 
70, 10, 10, 50, 70, 90, 10, 10, 10, 30, 70, 30, 90, 10, 10, 50, 
90, 30, 30, 50, 10, 30, 30, 70, 10, 30, 10, 10, 50, 70, 10, 50, 
50, 50, 90, 30, 30, 50, 70, 90, 70, 70, 70, 70, 50, 50, 30, 30, 
90, 90, 30, 30, 90, 30, 90, 70, 70, 90, 50, 50, 50, 50, 50, 90, 
90, 70, 70, 70, 70, 70, 90, 90, 90, 90, 90)), .Names = c("x", 
"y", "z"), row.names = c(NA, -231L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x0000000000330788>)

来源:https://stackoverflow.com/questions/42433116/independent-alpha-for-each-plot-within-facet-grid-based-on-density

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!