当我在散点图中将轴的原点设置为零时,点被切断

时间:2018-09-17 01:16:40

标签: r ggplot2 scatter-plot

当我在散点图中将X轴和Y轴原点设置为0时,图表边缘上的点将被切除。

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

上面的代码是在RStudio查看器中产生的

enter image description here

顶部和底部的点被切断。如何显示每个点而不将其切断?

1 个答案:

答案 0 :(得分:3)

您需要在坐标规范上使用clip = "off"

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

reprex package(v0.2.0)于2018-09-16创建。