Googme
Googme

Reputation: 914

How to combine facet_grid from different data sets with same features into one plot?

I have two identical data sets consisting of a variable indicating the date, two categorical variables and wage:

data("EmplUK", package="plm")
df<- head(EmplUK, 50)


ggplot(df, aes(x=year, y=wage, colour=sector)) + 
  stat_summary(fun.y=mean, geom="line", size=2) +
  stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar") +
  facet_grid(sector~firm)+theme_bw()

enter image description here

The second data set is only different in regard to wage:

df1<- head(EmplUK, 50)
df1$wage<- df1$wage +10

ggplot(df1, aes(x=year, y=wage, colour=sector)) + 
  stat_summary(fun.y=mean, geom="line", size=2) +
  stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar") +
  facet_grid(sector~firm)+theme_bw()

enter image description here

I would like to combine them both into one graph to have two time series for each grid instead of one, where instead of "sector" on the right hand side with the blue scale ranging from 8 to 3 I have an indicator of these two data sets like "dataset" with df1 and df.

Upvotes: 2

Views: 940

Answers (1)

Valter Beaković
Valter Beaković

Reputation: 3250

Something like this?

library(ggplot2)
    data("EmplUK", package="plm")
    df<- head(EmplUK, 50)
    df1<- head(EmplUK, 50)
    df1$wage<- df1$wage +10
    df$source <- rep("df", 50) 
    df1$source <- rep("df1", 50) 
    dfAggregate <- rbind(df, df1)
    dfAggregate$source <- as.factor(dfAggregate$source)

    ggplot(dfAggregate, aes(x=year, y=wage, colour=source)) + 
            stat_summary(fun.y=mean, geom="line", size=2) +
            stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar") +
            facet_grid(sector~firm)+theme_bw()

Upvotes: 1

Related Questions