Help with ggplot polygon and facet

125 views
Skip to first unread message

Navin

unread,
Jan 2, 2013, 7:42:27 PM1/2/13
to ggp...@googlegroups.com
Hi,
I am trying to use the ggplot2 library to create polygon plots with lines overlaid.
I tried two options and have failed at either one to get the complete plots. Could someone please help ?


p = data.frame(x=rep(c(1:3,3:1),4), y=rep(c(4:2,3:5),4), g=rep(c("a","b","c","d"), each=6))
p

n= data.frame(x=c(1,3,1,2,2,3,1,3), y=c(5,2,3,5,4,2,5,2), g=rep(c("a","b","c","d"), each=2))
n

Option one :

pp = ggplot() +
geom_polygon(data=p, mapping=aes(x=x, y=y),
      fill="light blue", alpha = 0.5)+ 
facet_grid(.~g)
pp

But then how do I add those lines from data=n on these polygon plots ?




Option two :
pp=qplot(data=p,
      x=x,
      y=y,
      geom="polygon",
      facets=~g,  alpha=0.1)+
        geom_polygon(data=p,fill="light blue")
pp
pp+geom_line(data=n, colour="red", size=1, linetype=4)

Here I am unable to get all the panels on the same row (currently it is a 2X2 layout) and the alpha (transparency) option doesn't work properly.

Many Thanks





Zhonghua Zhang

unread,
Jan 2, 2013, 9:27:49 PM1/2/13
to Navin, ggp...@googlegroups.com

Try the following codes:

 

qq<-ggplot(data=p,aes(x,y))+geom_polygon(fill="light blue",alpha=0.5)

qq+geom_line(data=n,aes(x,y),colour="red",size=1,linetyp=4)+

      facet_grid(.~g)

--
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

Navin

unread,
Jan 2, 2013, 10:14:09 PM1/2/13
to ggp...@googlegroups.com, Navin, zhonghu...@unimelb.edu.au
Works perfect !
Thanks a lot

Navin

unread,
Jan 2, 2013, 10:47:11 PM1/2/13
to ggp...@googlegroups.com, Navin, zhonghu...@unimelb.edu.au
Hi,
I had a followup question, how would I add two sets of y variables ?

Is there a way to add another set of lines on the plots ? For example, in the following after adding lines from dataframe=n, I want to add another set of lines (example data.frame=m) to the plots as well . Basically two sets of y values, y1 and y2

Many thanks again


p = data.frame(x=rep(c(1:3,3:1),4), y=rep(c(4:2,3:5),4), g=rep(c("a","b","c","d"), each=6))
p

n= data.frame(x=c(1,3,1,2,2,3,1,3), y=c(5,2,3,5,4,2,5,2), g=rep(c("a","b","c","d"), each=2))
n
m= data.frame(x=c(1,3,1,2,2,3,1,3), y=c(4,1,3,4,4,1,5,1), g=rep(c("a","b","c","d"), each=2))
m

or say a dataframe with two variable columns y1 and y2

r= data.frame(x=c(1,3,1,2,2,3,1,3), y1=c(5,2,3,5,4,2,5,2), y2=c(4,1,3,4,4,1,5,1),
                         g=rep(c("a","b","c","d"), each=2))
r

Zhonghua Zhang

unread,
Jan 2, 2013, 10:51:16 PM1/2/13
to Navin, ggp...@googlegroups.com

Just need to add another layer as follows:

 

qq<-ggplot(data=p,aes(x,y))+geom_polygon(fill="light blue",alpha=0.5)

qq+geom_line(data=n,aes(x,y),colour="red",size=1,linetyp=4)+

   geom_line(data=r,aes(y1,y2),colour="blue",size=1,linetype=2)+

   facet_grid(.~g)

Reply all
Reply to author
Forward
0 new messages