如何在R中绘制一条虚线? [英] How to plot a broken line in R?

查看:250
本文介绍了如何在R中绘制一条虚线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下 MWE,一条水平线表示特定一天的平均值,点是情绪的度量.

So I have the following MWE, a horizontal line expresses the mean of a particular day and the points are measurements of emotion.

我想在点之间的一天内画一条线而不是点,但这条线在几天之间必须有中断.我似乎无法弄清楚如何做到这一点.
我在这个页面上尝试了这个例子,但这似乎不适用于我的数据.
我的一个朋友设法对水平线执行此操作(它们在几天之间有空格),但我似乎无法更改我的代码以使其在几天内用于我的测量.

I'd like to draw a line instead of points within a day between the points, but the line must have breaks between days. I can't seem to figure out how to do this.
I tried the example on this page, but that does not seem to work for my data.
A friend of mine managed to do this for the horizontal lines (they have spaces between days), but I can't seem to change my code to let it work for my measurements within days.

MWE:

beeps.MWE <- c(91.188697, 87.846194, 93.166418, 96.249094, 95.495146, 99.362597, 94.373646, 
81.995712, 87.626009, 91.880172, 93.112647, 99.349234, 87.073372, 85.161982, 88.119728, 
89.738318, 68.891181, 62.504569, 75.131526, 56.035989, 66.035109, 56.012537)

day.MWE <- rep(c(91.35869, 63.17620), each = 11)

loc.MWE <- c(8, 15)

plot(day.MWE, type = "n", pch = 15, cex = 1.5, ylim = c(40, 110), bty = "n", 
ylab = "score on PA/NA", xlab = "days of person i", axes = FALSE)
dayUn <- unique(day.MWE)
for (i in seq_along(dayUn))
{
  points(which(day.MWE==dayUn[i]),day.MWE[day.MWE==dayUn[i]], type = 'l', lwd = "2")
}
points(1:length(beeps.MWE), beeps.MWE, type = "p")
lines(1:length(beeps.MWE), rep(mean(day.MWE), 22), lwd = "2", lty = 2)
axis(1, at = c(1, 20), labels = c("day 1", "day 2"))
axis(2, las = 1)

这是上面代码的输出:

推荐答案

使用所提供的代码,您就大功告成了.只需在循环中添加一条线即可绘制点之间的线:

You're nearly there with the code provided. Just add a line to the loop to draw the lines between the points:

for (i in seq_along(dayUn)){

  # draw horizontal lines to show the mean per day
  points(which(day.MWE==dayUn[i]),day.MWE[day.MWE==dayUn[i]], type = 'l', lwd = "2")

  # draw a line that connects points within a day
  points(which(day.MWE==dayUn[i]),beeps.MWE[day.MWE==dayUn[i]], lwd = "2", type='l')
}

另请注意,points(x,y,type='l')lines(x,y) 相同.更有意义;)

Also note that points(x,y,type='l') is the same as lines(x,y). Makes more sense ;)

这篇关于如何在R中绘制一条虚线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆