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
geom_count is a variant of geom_point that counts the number of observations at each location, then maps the count to point area. It is useful when you have discrete data with overplotting.
The function geom_point() adds a layer of points to your plot, which creates a scatterplot.
geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.
To color the points in a scatterplot using ggplot2, we can use colour argument inside geom_point with aes. The color can be passed in multiple ways, one such way is to name the particular color and the other way is to giving a range or using a variable.
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 of a seperate colorbar
.
If you examine the help file for the geom_count
function: help(geom_count)
, you will see a list of its Computed variables.
Computed variables
n
number of observations at position
prop
percent of points in that panel at that position
So you can use geom_count(aes(alpha=0.25, color= ..n..))
to color by the number of observations at a position and geom_count(aes(alpha=0.25, color= ..prop..))
to color by the percent of points at that position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With