0

I have a data frame that looks roughly like this:

aa <- c(1:7)
bb <- c(11:15)
df1 <- expand.grid(aa, bb)
val1 <- rnorm(nrow(df1))
val2 <- runif(nrow(df1))

df <- data.frame(df1, val1, val2)
names(df) <- c("aa", "bb", "val1", "val2")

What I want to do: For a fixed aa (say, 1), there is a time series of val1 and val2 for all values of bb. Now, I would like to plot these (for aa = 1 these are 5 for each val1 and val2) time series. (so in total 7*5*2 time series)

How can I do this with ggplot2?

I tried the following:

require(ggplot2)
require(reshape2)

df_pl <- melt(df,  id.vars = c("aa", "bb"), variable.name = 'val')

ggplot(df_pl, aes(aa, value)) + geom_point(aes(colour = val))
ggplot(df_pl, aes(bb, value)) + geom_point(aes(colour = val))

But this only produces plots of val1 and val2 as functions of aa and bb, not of a val1 / val2 series for each value of bb. I am probably using the melt function incorrectly

user3825755
  • 883
  • 2
  • 10
  • 29
  • Something like this? `ggplot(df_pl, aes(x=(interaction(bb, aa)), y=value, colour = val)) + geom_point()` – Roman Jul 12 '16 at 08:35
  • @Jimbou Hmm, this plots all `val1` and `val2` for all combinations of aa and bb. What I would like to do is for say aa = 1, plot a time series of `val1` and `val2` as functions of bb, then do the same for aa = 2 and so on. So there should be 7*5 time series of val1 and 7*5 time series of val2 plotted – user3825755 Jul 12 '16 at 08:40

1 Answers1

2

I'm unsure if I understood you correctly and this is what you want to achieve, but maybe try:

ggplot(df_pl, aes(aa, value)) + geom_point(aes(colour = val)) + facet_wrap(~bb)
ggplot(df_pl, aes(bb, value)) + geom_point(aes(colour = val)) + facet_wrap(~aa)
bendae
  • 779
  • 3
  • 13
  • Thanks, this goes into the right direction, it is somewhat hard to explain what I want to do. So this plots several panels, what I would prefer is to have everything in a single panel, but using different colors for the different bb values (in the first case), or aa values (in the second case) – user3825755 Jul 12 '16 at 08:58
  • 1
    So `ggplot(df_pl, aes(bb, value)) + geom_point(aes(colour = factor(aa)))`? – Axeman Jul 12 '16 at 09:07