facet_grid with only one level does not display the graph with the facet_grid level in title

30 views
Skip to first unread message

MPettis

unread,
Oct 28, 2010, 1:26:09 PM10/28/10
to ggplot2
** Cross-posted in R-help, but then I realized that this was the more
appropriate list -- my apologies **


Hi All,

Here is the code that I'll be referring to:

p <- ggplot(wastran.data, aes(PER_KEY, EVENTS))
(p <- p +
facet_grid( pool.short ~ .) +
stat_summary(aes(y=EVENTS), fun.y = sum, geom="line") +
opts(axis.text.x = theme_text(angle = 90, hjust=1), title="Events
(15min.) vs. Time: Facet pool", strip.text.y = theme_text())
)


Now, depending on preceding parameters, the 'pool.short' factor
variable in 'wastran.data' can have one distinct factor level or it
can have more than one. When 'pool.short' has more than one factor
level, the graph performs as I expect, with multiple rows of graphs
with the value of the 'pool.short' variable displayed on the right-
hand side of the graph. When 'pool.short' has only one factor level,
the value is NOT displayed on the right-hand side. However, I'd still
like it displayed, even though it has only one value.

Can someone tell me how to tweak this code to make it still display
when it has only 1 factor level? If this code is unclear, I will be
happy to take some time and generate an artificial but reproducible
self-contained example. I left in the stat_summary layer in this code
in case it is interfering with the desired output (but I suspect is is
superfluous, but I am not confident enough to say that with absolute
certainty).

Thanks,
Matt

MPettis

unread,
Oct 28, 2010, 2:54:12 PM10/28/10
to ggplot2
FYI - In lieu of request, here is a reproducible example you can run
with R and see that >1 factor levels produce a label on the right side
of the plots, but 1 factor level does not. I'd like the latter to
display the factor level on the right as well...

# Sample Data
df.multi <- data.frame(
a=rep(c("A","B"), 5),
x=rep(Sys.Date() - seq(1,5), each=2),
y=runif(10)
)

df.single <- data.frame(
a=rep(c("A"), 10),
x=Sys.Date() - seq(1,10),
y=runif(10)
)


library(ggplot2)

# This plot has labels on the far right that show the value of
'a'
(p.multi <- ggplot( df.multi, aes(x, y)) +
geom_line() +
facet_grid(a ~ .)
)


# This plot DOES NOT HAVE labels on the far right that show the
value of 'a'
(p.single <- ggplot( df.single, aes(x, y)) +
geom_line() +
facet_grid(a ~ .)
)


TIA,
Matt

Joshua Wiley

unread,
Nov 1, 2010, 1:43:11 PM11/1/10
to MPettis, ggplot2
Hi Matt,

Thanks for providing an example. After spending a couple hours going
back and forth between facet wrap and facet grid, I came up with two
things:

1) I think whatever happens happens at build_strip, panel_info, or
layout_grid, maybe with one of these two lines:
row_vars <- unique(.$panel_info[names(.$rows)])
labels[, i] <- labeller(names(label_df)[i], label_df[, i])
But, I do not understand the guts of ggplot2 (/proto?) to be able to
properly debug so I could watch what is happening.

2) It is not on the rows, but you can get a column label using:
ggplot(df.single, aes(x, y)) + geom_line() + facet_wrap(~ a)


HTH,

Josh

P. S. I was also re-impressed with the sheer amount of work and
careful attention to detail Hadley must have put forth to make all of
this. Wow, and thanks.

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


--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

takahashi kohske

unread,
Nov 1, 2010, 2:07:24 PM11/1/10
to MPettis, ggplot2
hi, here is a hack:

FacetGrid$labels_default <- function(., gm, theme) {
labeller <- match.fun(.$labeller[[1]])
add.names <- function(x) {
for(i in 1:ncol(x)) x[[i]] <- labeller(colnames(x)[i], x[,i])
x
}

row.labels <- add.names(rrownames(gm))
col.labels <- add.names(rcolnames(gm))

strip_h <- apply(col.labels, c(2,1), ggstrip, theme = theme)
if (nrow(strip_h) == 1 && ncol(strip_h) == 1) strip_h <-
matrix(list(zeroGrob()))
strip_v <- apply(row.labels, c(1,2), ggstrip, horizontal=FALSE, theme=theme)
# if (nrow(strip_v) == 1 && ncol(strip_v) == 1) strip_v <-
matrix(list(zeroGrob()))

list(
h = strip_h,
v = strip_v
)
}

(p.single <- ggplot( df.single, aes(x, y)) +
geom_line() +
facet_grid(a ~ .)
)

Matthew Pettis

unread,
Nov 1, 2010, 3:48:21 PM11/1/10
to takahashi kohske, ggplot2
Thank you Josh, Takahashi... I will try Takahshi's hack...

Thanks for your responses,
Matt

--
Seven Deadly Sins (Gandhi):
  - Wealth without work             - Politics without principle
  - Pleasure without conscience     - Commerce without morality
  - Science without humanity        - Worship without sacrifice
  - Knowledge without character

Reply all
Reply to author
Forward
0 new messages