Putting titles on facets

4,411 views
Skip to first unread message

Marcelo Stabile

unread,
May 16, 2010, 8:58:09 PM5/16/10
to ggp...@googlegroups.com

Hello Folks,

 

Last week I asked a question to Hadley and he suggested I asked it here.

Since then, I’ve solved the problem, but he solution is quite fiddly.

Any suggestion is appreciated.

 

Here is the problem:

 Looking at facet_grid:

p <- ggplot(diamonds, aes(carat, ..density..)) +   geom_histogram(binwidth = 0.2)

p + facet_grid(clarity ~ cut)

If I want to put a title on “clarity”, I can do:

p + facet_grid(clarity ~ cut) +opts(title="Clarity")

But is there a simpler way of Putting a Title on the Facets?

I’d like to put “Cut” as the title of the cut facet…

 

So what I thought was:

# Setting the Theme

theme_set(theme_bw())

# Creating Viewports

VP<- viewport(width=1,height=1, x=0.5,y=0.5)

VP2<- viewport(width=.8,height=.8, x=0.5,y=0.5)

# Creating a Blank plot, witht he title and Facet Y label

OutPlot<- ggplot(diamonds, aes(carat, price))+geom_blank()+opts(title="Plot title",

panel.grid.major=theme_blank(), panel.grid.minor=theme_blank(), panel.border =theme_blank(),

axis.text.x=theme_text(colour= "white"),axis.text.y=theme_text(colour= "white"),

axis.title.x=theme_text(colour= "white"), axis.title.y=theme_text(colour= "white"),

axis.line=theme_segment(colour= "white"),axis.ticks=theme_segment(colour= "white"))+geom_text(aes(x=5, y=10000),angle=90,label="Facet Y label")

# Defining the inner plot as in The help example

p <- ggplot(diamonds, aes(carat, ..density..)) +   geom_histogram(binwidth = 0.2)

# Defining the Ineer plot

InPlot <- p + facet_grid(clarity ~ cut)+opts(title="Facet X label", strip.background = theme_blank())

windows()

print(OutPlot, vp=VP)

print(InPlot, vp=VP2)

 

I just want to make this simpler….

 

Thanks

Marcelo

 

 

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

baptiste auguie

unread,
May 17, 2010, 8:08:41 AM5/17/10
to Marcelo Stabile, ggp...@googlegroups.com
Hi,

Try this,

p = qplot(hp, wt, data=mtcars,facets=vs ~ am ) + opts(title = "Facet X label")
source("http://gridextra.googlecode.com/svn/trunk/R/arrange.r")
arrange(p, main = textGrob("Plot title", gp=gpar(fontface
="bold",fontsize=12)),
legend = textGrob("Facet Y label",rot=90,gp=gpar(fontsize=14)))

HTH,

baptiste

Marcelo Stabile

unread,
May 17, 2010, 7:52:07 PM5/17/10
to baptiste auguie, ggp...@googlegroups.com
Dear baptiste,
Thanks for that....

Yesterday I had a few ideas and came up with a nice solution.
Hope it is useful for everyone...

p <- ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2)
VP <- viewport(width=0.95,height=0.95, x=0.0,y=0.0, just=c(0,0))
InPlot <- p + facet_grid(clarity ~ cut)+opts(title="X facet label", strip.background = theme_blank())
print(InPlot, vp=VP)
grid.text("Y facet Label" ,x=0.95,y=0.5,rot = 270)
grid.text("Graph Title", x=0.5, y=0.95, gp=gpar(fontsize=14))

Cheers
Marcelo
> I'd like to put "Cut" as the title of the cut facet...
> I just want to make this simpler....
Reply all
Reply to author
Forward
0 new messages