How do I build more than one series in the same scatterplot R?

I often visualize one time series compared to another using scatter charts in Excel, but since the latest data is more relavant, I use different highlights for later time periods:

enter image description here

In this case, the graphics of the month, week, and today - it's just a different (more recent) fragments of the same time series, so in this diagram, in principle, there are four superimposed graphics. How can I do the same in R? I got so far:

enter image description here

But I would like to reproduce what I have in excel. How to add new graphs to the same graph in R?

Or, perhaps, I could go ahead and use the attribute col on the R chart, to obtain a continuous increase in color value to the present, while avoiding these cautious steps? How can I do it?

+4
source share
2 answers

Here is an example of the skeleton of how to do this using ggplot :

 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() 

enter image description here

And for smooth colors

 ggplot(dat, aes(x=x, y=y, colour=day)) + geom_point() + scale_colour_gradient(low="pink", high="red") x, y = y, colour = day)) + geom_point () + ggplot(dat, aes(x=x, y=y, colour=day)) + geom_point() + scale_colour_gradient(low="pink", high="red") 

enter image description here

+5
source

You can use the construction of low-level points() , to add a point to an existing plot. It works the same way as you create a scatter plot through plot() , except that it adds points to the currently in use graphics.

For instance:

 plot(1:10) points(10:1,col="red") 

Edit:

One way to do color - use the rgb() , as suggested by Chi. I like to create a dummy variable with values ​​from 0 to 1, and use it as a scalar for flowers. For instance:

 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 reduces the number of points, because they have a higher value of z . Of course, you can change the min(z) and max(z) on the border of your scale.

enter image description here

+7
source

All Articles