0

I have done very little interesting plotting in R so I'm looking for a little help. Thanks in advance.

I have two data.frames:

> dim(MyDates) [1] 371 1 > dim(SumData) [1] 371 30 

MyDates holds 371 Monday-Friday dates. SumData holds 30 different data streams aligned properly against MyDates.

The data is clustered in 6 groups defined as such:

groups=list(1:5,6:10,11:15,16:20,21:25,26:30) 

I would like to create one plot with all 30 columns in SumData plotted as (I think) solid lines. The X axis must be MyDates in ascending order. I would like to color each group differently.

group1 = red group2 = yellow group3 = green group4 = cyan group5 = blue group6 = magenta 

1 plot, 30 lines, 6 groups, 5 lines/group, each group a pre-defined color.

How can I accomplish this?

Thanks!

2 Answers 2

1

A bit of a hack, but this gets the job done...

require(zoo) # z <- zoo(SumData, MyDates) z <- zoo(matrix(rnorm(30*371),371,30),Sys.Date()-371:1) Groups <- list(1:5,6:10,11:15,16:20,21:25,26:30) Colors <- c("red","yellow","green","cyan","blue","magenta") Col <- vector("character",length(unlist(Groups))) for(i in 1:length(Groups)) { Col[Groups[[i]]] <- Colors[i] } plot(z, screens=1, col=Col) 
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a ggplot answer with the correct colors.

require(ggplot2) ngroups = 6 dummygroup <- function(g) (g * 50) + apply(matrix(rnorm(371 * 5), ncol = 5), 2, cumsum) SumData <- do.call("data.frame", lapply(ngroups:1, dummygroup)) MyDates <- as.Date(1:371, origin="2009-01-01") df <- data.frame(MyDates, SumData) dfmelt <- melt(df, id = "MyDates", variable_name = "series") dfmelt$Groups <- factor(rep(1:ngroups, each = 371 * 5)) Colors <- c("red","yellow","green","cyan","blue","magenta") p <- ggplot(dfmelt, aes(MyDates, value, color = Groups, group = series)) p <- p + geom_line() + scale_colour_manual(values = Colors) print (p) 

2 Comments

Looks interesting and as a newbie the code is helpful, but it didn't run for me here:
@LGTrader: Seems to work fine. I added a line to require the ggplot2 package in case that was the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.