ggplot2::geom_point()
   get_help() docs


Description

The geom_point() function is part of the {ggplot2} package, which is part of the {tidyverse}.

The geom_point() function is used within {ggplot2} plots to create points in plots. Most commonly, but not always, we use geom_point() as part of the code to create a scatterplot.

Required aesthetics:

  • x: The x-axis coordinate(s) for the point(s)
  • y: The y-axis coordinate(s) for the point(s)

Commonly used optional aesthetics:

  • color (colour): The point color
  • fill: The point fill, when the shape is specified to allow a fill
  • size: The point size
  • stroke: The point outline size, when the shape is specified to allow a fill
  • shape: The point shape
  • alpha: Point transparency, ranging from 0-1 where 0 is fully transparent and 1 is fully opaque.

Options for point shapes in R

The default shape used for plotting is 20.

To use this function, you need to either first load the {ggplot2} library, or always use the function with ggplot2::geom_point() notation.

# Load the library
library(ggplot2)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
ggplot2::geom_point()

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Scatterplot of awake against sleep_rem ("y against x")
ggplot(msleep) + 
  aes(x = sleep_rem, y = awake) + 
  geom_point()


# Scatterplot of awake against sleep_rem, with points colored blue
ggplot(msleep) + 
  aes(x = sleep_rem, y = awake) + 
  geom_point(color = "blue")


# Scatterplot of awake against sleep_rem, with point shape changed to 21 to allow for color and fill
ggplot(msleep) + 
  aes(x = sleep_rem, y = awake) + 
  geom_point(shape = 21, 
             fill = "goldenrod", 
             color = "blue")


# Scatterplot of awake against sleep_rem, with point shape changed to 21 to allow for color and fill
# Additional point modifications are shown:
ggplot(msleep) + 
  aes(x = sleep_rem, y = awake) + 
  geom_point(shape = 21, 
             fill = "goldenrod", 
             color = "blue", 
             size = 5, # size of point
             stroke = 3,# size of point outline
             alpha = 0.6) # some transparency


# Scatterplot of awake against sleep_rem, with points colored by vore
# Note to remove NAs, you must manipulate the data first
ggplot(msleep) + 
  aes(x = sleep_rem, y = awake, color = vore) + 
  geom_point()

# Advanced example showing use of geom_point to make a non-scatterplot.
# A lollipop plot of mean time spent awake across vores
# Wrangle:
msleep %>%
  tidyr::drop_na(vore) %>%
  dplyr::group_by(vore) %>%
  dplyr::summarize(mean_awake = mean(awake)) %>%
  # Pipe into ggplot:
  ggplot() + 
  aes(x = forcats::fct_reorder(vore, mean_awake),  # order x-axis by mean_awake value
      y = mean_awake) +
  geom_point(size = 5)