Getting a data frame name from d_ply into a ggplot title

121 views
Skip to first unread message

Bob

unread,
Nov 5, 2012, 11:44:24 AM11/5/12
to manip...@googlegroups.com
Hi All,

I'm doing a bunch of plots by a factor using d_ply and would like to put the factor level name onto each plot. These are going to a file but currently they're untitled, making them hard to use. The simplified example below should have "A" the title of the first plot and "B" as the title of the second. The code below doesn't work and, with df as the function variable, I've tried:

labs( title = eval( parse(text = df) ) )

as well as every other combination I could think of. I'm not quite sure what d_ply is passing for the data frame name. Any ideas?

Thanks,
Bob



x <- 1:4
y <- 1:4
group <- factor( c("A","A","B","B") )
mydata <- data.frame(group, x, y)
mydata

my_ggplot <- function(df) {
  ggplot(df, aes(x, y)) +
    geom_line() +
    labs(title = df )  # <---This is the line that doesn't work

# Test on whole data set
my_ggplot(mydata)

# Apply by group
pdf("plots_by_group.pdf")

d_ply(mydata, .(group), my_ggplot, .print = TRUE)

dev.off()

Ista Zahn

unread,
Nov 5, 2012, 12:04:49 PM11/5/12
to Bob, manip...@googlegroups.com
Hi Bob,

d_ply is passing the data.frame as df. Since you want to label using
df$group, you need

labs(title = unique(df$group))

Best,
Ista
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/manipulatr/-/11q1Nsnq-eIJ.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to
> manipulatr+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/manipulatr?hl=en.

Christiaan Pauw

unread,
Nov 5, 2012, 1:18:11 PM11/5/12
to manipulatr, Bob, Ista Zahn
Hi Bob and Ista and Everybody

Did you want a title or axis label?

for a title this worked for me:

opts(title = unique(df$group)

regards
Christiaan

Bob

unread,
Nov 6, 2012, 3:02:55 PM11/6/12
to manip...@googlegroups.com, Bob, Ista Zahn, christi...@nova.org.za
Hi Christian,

opts is what I initially used until I got the message that it was depracated:

Error in df$group : object of type 'closure' is not subsettable
In addition: Warning message:
'opts' is deprecated.
Use 'theme' instead.
See help("Deprecated")

Cheers,
Bob

Bob

unread,
Nov 6, 2012, 3:09:35 PM11/6/12
to manip...@googlegroups.com, Bob, Ista Zahn, christi...@nova.org.za
Ista, thanks very much for that solution! I tried an insane number of variations, but in hindsight unique is perfect. I'm still not quite clear as to how plyr passes the name of the group value so that when you write a function beginning with "function(df)..." within it you can say "data=df" and it prints out "A" and "B" for the result headings when doing ddply. I've used that successfully for years without really understanding how it gets the labels for the factor levels. Perhaps unique is the way it does it internally.

Thanks,
Bob

On Monday, November 5, 2012 1:18:42 PM UTC-5, Christiaan Pauw wrote:
Reply all
Reply to author
Forward
0 new messages