There are two other ways I can think to do it using facet_wrap()
that are a little more bare-bones:
- using
annotate()
in ggplot2
(simple approach)
- doubling your data frames for each company (still relatively simple, just more prone to errors)
Either way, let's recreate your two data frames so that we can reproduce your example:
First create the "total company revenue" data frame:
Quarter <- seq(2011, 2012, by = .25)
CompA <- as.integer(runif(5, 5, 15))
CompB <- as.integer(runif(5, 6, 16))
CompC <- as.integer(runif(5, 7, 17))
df1 <- data.frame(Quarter, CompA, CompB, CompC)
Next, the "segment revenue" data frame of Company A:
CompA_Footwear <- as.integer(runif(5, 0, 5))
CompA_Apparel <- as.integer(runif(5,1 , 6))
CompA_Wholesale <- as.integer(runif(5, 2, 7))
df2 <- data.frame(Quarter, CompA_Footwear, CompA_Apparel, CompA_Wholesale)
Now we will re-arrage your data to be something more recognizable for ggplot2
using melt()
from reshape2
require(reshape2)
melt.df1 <- melt(df1, id = "Quarter")
melt.df2 <- melt(df2, id = "Quarter")
df <- rbind(melt.df1, melt.df2)
We are mostly ready to graph now. For sake of example, I'll only focus on "Company A"
Using annotate()
Subset the data so that it only contains "segment revenue" for Company A
CompA.df2 <- df[grep("CompA_", df$variable),]
This assumes all your segment revenue is coded starting with "CompA_*". You will have to subset according to your data.
Now plot:
require(ggplot2)
ggplot(data = CompA.df2, aes(x = Quarter, y = value,
group = variable, colour = variable)) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_wrap(~variable) + # Facets by segment
# Next, adds the total revenue data as an annotation
annotate(geom = "line", x = Quarter, y = df1$CompA) +
annotate(geom = "point", x = Quarter, y = df1$CompA)
Basically, we are just annotating the graph with a line and points from our original "total company revenue" data frame for Company A. The major downside to this is the lack of a legend.

The second approach will produce a legend for all values
Duplicating your data
The way facet_wrap() works, we need to define the same facet variables for each of the intended plotted lines on each facet. So we are going to replicate our total revenue for each "segment revenue" level, and group each of these pairs together.
Using the same data frames as above, we are going to separate out the Total Company A Revenue and the Segment Revenue of Company A
CompA.df1 <- df[which(df$variable == "CompA"),] # Total Company A Revenue
CompA.df2 <- droplevels(df[grep("CompA_", df$variable),]) # Segment Revenue of Company A
Now repeat the total revenue data frame for Company A based on how many levels we have for the "Segment Revenue"
rep.CompA.df1 <- CompA.df1[rep(seq_len(nrow(CompA.df1)), nlevels(CompA.df2$variable)), ]
This might be prone to errors if you have NA's
or NaN's
Now merge the repeated data frame, and add a facet variable (facet.var here) to pair these together.
CompA.df3 <- rbind(rep.CompA.df1, CompA.df2)
CompA.df3$facet.var <- rep(CompA.df2$variable,2)
Now you are ready to graph. You can still define group = variable
, but this time we will set facet_wrap()
to our newly created facet.var
require(ggplot2)
ggplot(data = CompA.df3, aes(x = Quarter, y = value,
group = variable, colour = variable)) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
facet_wrap(~facet.var)
As you can see, we now have our "Total Revenue" added to the legend:

That plot's a real beaut