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

Use*_*ame 2 r scatter-plot ggplot2

当我在散点图上将 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)
  )
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

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

Eli*_*lli 5

您需要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")
Run Code Online (Sandbox Code Playgroud)

reprex 包(v0.2.0)于 2018 年 9 月 16 日创建。