[英]How do I plot more than one series in the same R scatterplot?
I often visualize one time series against another using scatterplots in Excel, but since recent data are more relavant, I use different highlights for more recent time periods:我经常使用 Excel 中的散点图将一个时间序列与另一个时间序列可视化,但由于最近的数据更相关,我对最近的时间段使用不同的亮点:
In this case the month, week and today plots are simply different (more recent) slices of the same time series, so basically there are four superimposed plots in this chart.在这种情况下,月、周和今天的图只是同一时间序列的不同(更新的)切片,因此该图表中基本上有四个叠加图。 How can I do the same in R?我怎样才能在 R 中做同样的事情? I have gotten so far:我到目前为止:
But i'd like to replicate what I have in excel.但我想复制我在 excel 中的内容。 How do I add new plots to the same chart in R?如何在 R 的同一图表中添加新图?
Or perhaps I could even go further and use the col attribute in the R plot to get a continuous increase in the colour up to the today value, thus avoiding these discreet steps?或者我什至可以进一步使用 go 并使用 R plot 中的 col 属性来连续增加颜色直到今天的值,从而避免这些离散步骤? How would I do that?我该怎么做?
You can use the lower level plotting function points()
to add points to an already existing plot.您可以使用较低级别的绘图 function points()
将点添加到已经存在的 plot。 It works in exactly the same way you create a scatter plot through plot()
except that it adds points to the currently used plot.它的工作方式与您通过plot()
创建散点图 plot 的方式完全相同,只是它向当前使用的 plot 添加点。
For example:例如:
plot(1:10)
points(10:1,col="red")
One way to do the colors is by using rgb()
as Chi suggested.执行 colors 的一种方法是按照 Chi 的建议使用rgb()
。 I like to create a dummy variable with values between 0 and 1 and use that as a scalar on the colors.我喜欢创建一个值介于 0 和 1 之间的虚拟变量,并将其用作 colors 上的标量。 For example:例如:
x <- rnorm(100)
y <- 0.5*x + rnorm(100)
z <- 0.5*y + rnorm(100)
dum <- (z - min(z)) / (max(z) - min(z))
plot(x,y,col=rgb(1-dum*0.4,1-dum*0.8,1-dum*0.8),pch=16)
This makes the points redder as they have a higher value of z
.这使得点更红,因为它们具有更高的z
值。 Of course you can change min(z)
and max(z)
into the bounds of the scale you are interested in.当然,您可以将min(z)
和max(z)
更改为您感兴趣的比例范围。
Here is a skeleton example of how to go about doing it using ggplot
:这是一个关于如何使用ggplot
进行 go 的骨架示例:
library(ggplot2)
day <- 1:100
dat <- data.frame(
day=day,
x = day+(1+rnorm(100, 0, 10)),
y = 5 + day+(1+rnorm(100, 0, 10)),
when = cut(day, 5)
)
ggplot(dat, aes(x=x, y=y, colour=when)) + geom_point()
And for smooth colours:对于平滑的颜色:
ggplot(dat, aes(x=x, y=y, colour=day)) + geom_point() +
scale_colour_gradient(low="pink", high="red")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.