creating a multi layered plot with different data sources

53 views
Skip to first unread message

Harish Krishnan

unread,
Aug 30, 2015, 7:23:54 AM8/30/15
to ggplot2
I am trying to create a multi layered plot with 2 different data sources

1) economics 2) presidential

The first plot is a geom_rect using presidential

p <- ggplot(presidential,aes(ymin = 2500,ymax = 12000,xmin = start,xmax = end,alpha = 1))+geom_rect(aes(fill = party))

The second one is a line plot using economics

q <- ggplot(economics,aes(date,unemploy)) + geom_line()

My aim is to add these two plots as layers of one single plot:

something similar to 

p <- ggplot(presidential,aes(ymin = yrange[1],ymax = yrange[2],xmin = start,xmax = end,alpha = 1))+geom_rect(aes(fill = party)) + geom_line(data = economics,aes(date,unemploy))

Unfortunately this is not working and gives me errors. 

How to do this?
Thanks in advance.



Dennis Murphy

unread,
Aug 30, 2015, 10:10:45 AM8/30/15
to Harish Krishnan, ggplot2
Hi:

Try

ggplot() +
geom_rect(data = presidential, aes(ymin = 2500, ymax = 12000, xmin
= start, xmax = end, fill = party)) +
geom_line(data = economics, aes(x = date, y = unemploy)) +
coord_cartesian(y = c(2500, 12000)) +
scale_x_date(expand = c(0, 0))

Pass the data frames to the individual geoms and make the aesthetic
mappings local to the geom. The last two lines exist to crop the
graphics region to the extent of the x/y ranges.

You might benefit by *setting* the alpha transparency of the
geom_rect() layer to something in the vicinity of 0.3 if you want to
see the grid line guides - moreover, ymin and ymax are also set, so a
more 'correct' way of specifying the plot with a smaller alpha might
be

ggplot() +
geom_rect(data = presidential, aes(xmin = start, xmax = end, fill = party),
ymin = 2500, ymax = 12000, alpha = 0.3) +
geom_line(data = economics, aes(x = date, y = unemploy)) +
coord_cartesian(y = c(2500, 12000)) +
scale_x_date(expand = c(0, 0))

I might also be inclined to color code the parties as blue and red, so
you could add

scale_fill_manual(values = c("blue", "red"))

to the end of the above call.

Dennis

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Harish Krishnan

unread,
Aug 30, 2015, 10:46:58 AM8/30/15
to Dennis Murphy, ggplot2

That worked perfectly. Thanks for the tip.

Hadley Wickham

unread,
Aug 30, 2015, 2:38:21 PM8/30/15
to Dennis Murphy, Harish Krishnan, ggplot2
> ggplot() +
> geom_rect(data = presidential, aes(ymin = 2500, ymax = 12000, xmin
> = start, xmax = end, fill = party)) +
> geom_line(data = economics, aes(x = date, y = unemploy)) +
> coord_cartesian(y = c(2500, 12000)) +
> scale_x_date(expand = c(0, 0))
>
> Pass the data frames to the individual geoms and make the aesthetic
> mappings local to the geom. The last two lines exist to crop the
> graphics region to the extent of the x/y ranges.

You're a little better off using -Inf and Inf as short hand for the
bottom and top of the panel:

geom_rect(data = presidential, aes(ymin = -Inf, ymax = Inf, xmin =
start, xmax = end, fill = party))

Hadley


Hadley
--
http://had.co.nz/

Harish Krishnan

unread,
Aug 30, 2015, 7:02:23 PM8/30/15
to Hadley Wickham, Dennis Murphy, ggplot2
Thanks for the tip Hadley!! 
Reply all
Reply to author
Forward
0 new messages