Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color points by their occurrence count in ggplot2 geom_count

Tags:

r

colors

ggplot2

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
like image 614
voiDnyx Avatar asked Aug 25 '17 13:08

voiDnyx


People also ask

What is Geom_count?

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.

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot.

What does geom_point mean?

geom_point.Rd. The point geom is used to create scatterplots. The scatterplot is most useful for displaying the relationship between two continuous variables.

How do I change the color of a geom point?

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.


2 Answers

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..))

enter image description here

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')

enter image description here

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

like image 62
mt1022 Avatar answered Sep 21 '22 16:09

mt1022


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.

like image 24
Eumenedies Avatar answered Sep 20 '22 16:09

Eumenedies