1

maybe a easy question but I cannot figure out. That's my original df:

                Date.time      P    ID
    1   2013-07-03 11:15:00 1207.7  K0904
    2   2013-07-03 11:20:00 1207.7  K0904
    3   2013-07-03 11:25:00 1207.9  K0904
    4   2013-07-03 11:30:00 1208.0  K0904
    5   2013-07-03 11:35:00 1208.0  K0904
    ....
    70  2013-07-03 17:00:00 1208.6  K0955
    71  2013-07-03 17:05:00 1208.4  K0955
    72  2013-07-03 17:10:00 1208.4  K0955
    73  2013-07-03 17:15:00 1208.6  K0955
    74  2013-07-03 17:20:00 1208.8  K0955
    ...

And with this code

    ggplot(df1, aes(x=Date.time, y=P, group=ID, color=ID)) + 
      geom_line() +
      facet_grid(~ID) +
      theme(legend.position="none")

I create this plot

enter image description here

Simply I want to overlay the last 3 plots to the first 3 ones (so the plot is smaller and more readable. I tried to split the df into 2 ones, each one made by 3 ID and then combine them adding 2 geom_line() one for every df but nothing changed. Any idea? Code to extract the new df is:

df1<-subset(df, ID %in% c("K1142", "K0904", "K1136"))
df2<-subset(df, ID %in% c("K0955", "K1148", "K8651"))

and then the new attempt

ggplot(df1, aes(x=Date.time, y=P, group=ID, color=ID)) + 
  geom_line() +
  geom_line(data=df2, aes(x=Date.time, y=P, color=ID)) +
  facet_grid(~ID) +
  theme(legend.position="none")
matteo
  • 4,683
  • 9
  • 41
  • 77
  • 1
    Can you show the code you used? – Señor O Aug 15 '13 at 20:01
  • Also, on SO, it is important to make post your question as a fully reproducible example - for instance by recreating your problem from one of hte built-in data sets, making up fake `data` and posting the code, or posting your actual data in a public repository. See link: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Drew Steen Aug 15 '13 at 20:20
  • 1
    Yeah - it's difficult to answer precisely without the data, but you essentially need to split ID into two variables: one that you'll use to facet the plot, and one that you'll map to the `group` and `color` aesthetics. So if you want to overlay K1142 and K0955, create a second ID variable in which they have the same value and use that to facet the plots. Then you could continue to use your current `ID` to map `group` and `color`. – Matt Parker Aug 15 '13 at 20:25
  • In my experience, it's pretty damn unusual to pass two data.frames to the same ggplot - I've only ever used that to add arbitrary text annotations to a plot. – Matt Parker Aug 15 '13 at 20:28
  • 1
    @Drew and Matt sorry guys. I edit now my question with a piece of my df – matteo Aug 15 '13 at 20:32
  • @matteo An even better way to do it is to copy the output of `dput(df)` - that generates code that anyone can paste into their R console to create an exact duplicate of your data. – Matt Parker Aug 15 '13 at 20:42
  • @Matt, no problem with different data frames in different `geom`s in one `ggplot` call. See e.g. http://stackoverflow.com/questions/9109156/ggplot-combining-two-plots-from-different-data-frames – Henrik Aug 15 '13 at 20:43
  • I find I use multiple data frames in a single `ggplot` fairly regularly - it is useful when I want to compare real data with a theoretical prediction, or compare my data with someone else's range, that kind of thing. – Drew Steen Aug 15 '13 at 20:48
  • @Henrik and Drew - that's interesting. I knew that multiple datasets *could* be used, but I almost always end up combining what I need into a single dataset (e.g., empirical and theoretical data). Can't remember why... I feel like it makes tweaking the aesthetics easier, but I'll have to experiment with multiple dfs a little more. – Matt Parker Aug 15 '13 at 20:54
  • I found it rather convenient with three df:s in my answer to @martens previous question today, but I would be happy to see other alternatives! – Henrik Aug 15 '13 at 20:56
  • Should have written @matteo. Sorry. – Henrik Aug 15 '13 at 21:08

1 Answers1

5

This will do the job (basically what @MattParker suggested)

# Recreate your data (fake)
Date.time <- rep(1:100, 6)
P <- runif(600) + rep(0:5, each=100)
ID <- gl(6, 100, labels=c("K1142", "K0904", "K1136", "K0955", "K1148", "K8651"))
d <- data.frame(Date.time=Date.time, P=P, ID=ID)


#Create a new dummy variable to facet by
#    (There's a cleaner way to do this, but this way is easy to understand)
d$newID <- NA
d$newID[d$ID %in% levels(d$ID)[1:2]] <- "groupA"
d$newID[d$ID %in% levels(d$ID)[3:4]] <- "groupB"
d$newID[d$ID %in% levels(d$ID)[5:6]] <- "groupC"

# Make the plot, faceting on the dummy variable
ggplot(d, aes(x=Date.time, y=P, colour=ID)) + geom_line() +
  facet_wrap(~newID)
Drew Steen
  • 16,045
  • 12
  • 62
  • 90