如何在点之间添加线而不“接触”点,如“type=”b“”?

发布于 2024-11-09 05:47:26 字数 84 浏览 6 评论 0原文

我想在 R 绘图中的点之间添加线条。 但不是他们所有人之间。

所以我用“线”。 但我想保留“type='b'”样式,该行就在该点之前停止。

I would like to add lines between points in a plot in R.
But not between all of them.

So I use "lines".
But I would like to keep the "type='b'" style, with the line stopping just before the point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-11-16 05:47:26

如果您喜欢 ggplot,请尝试一下。 ggplot 本身并不支持基本图形中的 type = "b"。我们可以通过一些过度绘制和子集来解决这个问题:

library(ggplot2)
x <- seq(1, pi, pi/36)
y <- sin(x)
z <- data.frame(x,y)



ggplot(z, aes(x,y)) + 
    geom_line(data = subset(z, x > 1.5 & x < 2.5)) + 
    geom_point(size = 6, colour = "white") +
    geom_point(size = 3, colour = "black") +
    theme_bw()

在此处输入图像描述

If ggplot is your thing, give this a whirl. ggplot doesn't natively support the type = "b" as in base graphics. We can get around that though with some overplotting and subsetting:

library(ggplot2)
x <- seq(1, pi, pi/36)
y <- sin(x)
z <- data.frame(x,y)



ggplot(z, aes(x,y)) + 
    geom_line(data = subset(z, x > 1.5 & x < 2.5)) + 
    geom_point(size = 6, colour = "white") +
    geom_point(size = 3, colour = "black") +
    theme_bw()

enter image description here

离线来电— 2024-11-16 05:47:26

设置一些数据

x <- seq(1, pi, pi/36)
y <- sin(x)

使用所有点创建绘图

plot(x, y)

为某些点添加 type="b" 行:

lines(x[10:20], y[10:20], type="b")

在此处输入图像描述

Set up some data

x <- seq(1, pi, pi/36)
y <- sin(x)

Create plot with all points

plot(x, y)

Add lines of type="b" for some of the points:

lines(x[10:20], y[10:20], type="b")

enter image description here

梦萦几度 2024-11-16 05:47:26

您可以使用带有 type='c' 的lines 函数来仅添加点周围带有空格的线。只需将您有兴趣为其执行线条的子集提供给线条函数即可。

You can use the lines function with type='c' to just add the lines with the spaces around the points. Just give the lines function the subset that you are interested in doing the lines for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文