Dots get cut off when I set axes' origins to zero in scatterplots

1656 views r
1

When I set the X and Y axes origins to 0 on a scatterplot, dots on the edges of the chart get cut off.

ggplot(
    data = cars,
    aes(
      x = speed,
      y = dist
    )
  ) +
  geom_point(
    size = 3
  ) +
  scale_x_continuous(
    breaks = seq(
      from = 0,
      to = max(cars$speed),
      by = 5
    )
  ) + 
  scale_y_continuous(
    breaks = seq(
      from = 0,
      to = max(cars$dist),
      by = 5
    ),
    expand = c(0,0)
  )

The code above produces this in RStudio viewer

enter image description here

Dots on the very top and bottom get cut off. How do I show every dot without them getting cut off?

answered question

1 Answer

2

You need to use clip = "off" on the coordinate specification.

library(ggplot2)

ggplot(
  data = cars,
  aes(
    x = speed,
    y = dist
  )
) +
  geom_point(
    size = 3
  ) +
  scale_x_continuous(
    breaks = seq(
      from = 0,
      to = max(cars$speed),
      by = 5
    )
  ) + 
  scale_y_continuous(
    breaks = seq(
      from = 0,
      to = max(cars$dist),
      by = 5
    ),
    expand = c(0,0)
  ) +
  coord_cartesian(clip = "off")

Created on 2018-09-16 by the reprex package (v0.2.0).

posted this

Have an answer?

JD

Please login first before posting an answer.