繁体   English   中英

ggplot2 没有 plot 所有点

[英]ggplot2 doesn't plot all the points

我正在尝试在 ggplot2 中绘制一个圆形 7 个点的图形,但尝试绘制它们只显示 6 个,我不知道为什么会这样。

代码如下:

# Function for the points
circleFun <- function(center = c(-1, 1), diameter = 1, npoints = 7) {
  r <- diameter / 2
  tt <- seq(0, 2 * pi, length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}

# example with 7 points
ej <- 
  circleFun(diameter = 50, center = c(50,50), npoints = 7)


# plot

ej |> 
  ggplot(aes(x = x, y = y)) +
  geom_point(alpha = 0.4) +
  theme_bw()

有谁知道为什么会这样?

第 1 行和第 7 行相同,因此它们的点重叠。 这个点有点暗(根据你的 alpha = 0.4)。 您可以通过添加 x = jitter(x) 来使这一点变得明显(为了演示,您不会在生产中这样做)。 鉴于相同的数据,我不确定您期望看到什么。

如果你想要 7 个不同的点,那么我建议你创建n+1并删除最后一个(或第一个)点。


circleFun <- function(center = c(-1, 1), diameter = 1, npoints = 7) {
  r <- diameter / 2
  tt <- seq(0, 2 * pi, length.out = npoints + 1)  # changed
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  data.frame(x = xx, y = yy)[-1,,drop = FALSE]    # changed
}

## unchanged from here on
ej <- 
  circleFun(diameter = 50, center = c(50,50), npoints = 7)
ej |> 
  ggplot(aes(x = x, y = y)) +
  geom_point(alpha = 0.4) +
  theme_bw()

在此处输入图像描述

(顺便说一句,不需要显式调用return(.) ,尤其是当它是 function 的唯一端点并且基于数据流“显而易见”时。当然没有什么坏处,但它增加了一步在没有增加任何价值的调用堆栈上。它可能是声明性/自我记录的,因此这是一个风格/主观点。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM