voiDnyx - 10 months ago 105
R Question

# Color points by their occurance count in ggplot2 geom_count

I want to color the points drawn by ggplot2's geom_count based on their count.

This is what i have so far:

``````ggplot(test3, aes(eleStart, eleLength)) +  geom_count(aes(alpha=0.25, color= ..prop..)) +
scale_y_continuous(breaks=seq(0,130,5)) +
scale_x_continuous(breaks=seq(0,114)) +
theme(panel.grid.minor = element_blank())
``````

Now i basically just want to exchange the
`color=..prop..`
with the actual count calculated by geom_count not their proportion.

test3 dataframe looks like:

``````# A tibble: 294 x 2
# Groups:   X1 [56]
eleStart eleLength
<int>     <int>
1        0         3
2        0         6
3        0         7
4        0         9
5        0        11
6        0        23
7        0        25
8        0        26
9        0        26
10       0        26
# ... with 284 more rows
``````

You can color points by their occurance with `color = ..n..` in `aes`. See the follow example:

``````ggplot(mtcars, aes(cyl, carb)) + geom_count(aes(color = ..n..))
``````

To know all the computed variables that can be accessed with `..x..` syntax, you can check the manual of a `geom_*` function for "Computed variables". For `geom_count`, it looks like:

Computed variables

n number of observations at position

prop percent of points in that panel at that position

If you want to "combine the 2 legends into one legend with colorized points", try the following:

``````ggplot(mtcars, aes(cyl, carb)) +
geom_count(aes(color = ..n.., size = ..n..)) +
guides(color = 'legend')
``````

Color was displayed as colorbar by default. Here, `guides(color = 'legend')` tells `ggplot` to dispaly it as `legend` instead instead of a seperate `colorbar`.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download