Ben Hunter
2011-Jul-08 20:39 UTC
[R] Referencing a vector of data labels in ggplot function
Hi,
I really feel I've looked everywhere, although I know this can't be a
hard
problem. I'd like to be able to call the graph below as a function, but I
can't get the function to recognize variables beyond 'dframe'.
I've read
through many papers on writing functions in R, but I can't get this to work.
data <- data.frame('date' = as.Date(rep(c(15101,
15108, 15115, 15122, 15129, 15136, 15143, 15150),4),
origin = '1899-12-30'),
'factor' = factor(rep(c('first','second'),
each = 8, 2)),
'value' = rep(c(429258, 430645, 431165, 431360, 452284,
467316,
467326, 467330,
375588, 411383, 427179, 364582, 351494, 359034, 374047,
339628),2),
'Facet' = rep(c('bottom','top'), each = 16))
pTitle <- 'Main Title'
plines <- c('Line 1', 'Line 2','Line 3', 'Line
4')
col1 <- c('#ec421e', '#f7bd2e','#ec421e',
'#f7bd2e')
#If I use the line below and explicitly place plines, pTitle and col1 in the
appropriate places
#it will work fine. I want to use the line as written without the hashmark.
#simple <- function(dframe){
withNames <- function(dframe, lineNames, plotName, colors){
p <- ggplot(dframe, aes(date, value, group = factor, color = factor))
p2 <- p + geom_line(size = 1)
# + opts(title = plotName)
p2 <- p2 + facet_grid(Facet~., scales = 'free') +
# p2 <- p2 + geom_text(data = dframe[dframe[,'date'] ==
'1941-06-16',],
# aes(date, value, label = lineNames, vjust = 1)) +
scale_colour_manual(values = colors)
}
finalP <- withNames(data, plines, pTitle, col1)
#finalP <- simple(data)
[[alternative HTML version deleted]]
Hadley Wickham
2011-Jul-09 14:38 UTC
[R] Referencing a vector of data labels in ggplot function
Maybe something like this?
withNames <- function(dframe, lineNames, plotName, colors){
one_day <- subset(dframe, data == '1941-06-16')
one_day$lineNames <- lineNames
ggplot(dframe, aes(date, value, group = factor, color = factor)) +
geom_line(size = 1) +
facet_grid(Facet~., scales = 'free') +
geom_text(aes(label = lineNames), data = one_day, vjust = 1) +
scale_colour_manual(values = colors) +
opts(title = plotName)
}
withNames(data, plines, pTitle, col1)
It's not at all clear how you expect the line names to be matched up
to the lines though.
Hadley
On Fri, Jul 8, 2011 at 3:39 PM, Ben Hunter <bjameshunter at gmail.com>
wrote:> Hi,
>
> I really feel I've looked everywhere, although I know this can't be
a hard
> problem. I'd like to be able to call the graph below as a function, but
I
> can't get the function to recognize variables beyond 'dframe'.
I've read
> through many papers on writing functions in R, but I can't get this to
work.
>
> data <- data.frame('date' = as.Date(rep(c(15101,
> ? ? ? ? ? ?15108, 15115, 15122, 15129, 15136, 15143, 15150),4),
> ? ? ? ? ? ?origin = '1899-12-30'),
> ? ? ? ? ? ?'factor' =
factor(rep(c('first','second'), each = 8, 2)),
> ? ? ? ? ? ?'value' = rep(c(429258, 430645, 431165, 431360, 452284,
467316,
> 467326, 467330,
> ? ? ? ? ? ?375588, 411383, 427179, 364582, 351494, 359034, 374047,
> ? ? ? ? ? ?339628),2),
> ? ? ? ? ? ?'Facet' = rep(c('bottom','top'), each =
16))
>
> pTitle <- 'Main Title'
> plines <- c('Line 1', 'Line 2','Line 3',
'Line 4')
> col1 <- c('#ec421e', '#f7bd2e','#ec421e',
'#f7bd2e')
>
> #If I use the line below and explicitly place plines, pTitle and col1 in
the
> appropriate places
> #it will work fine. I want to use the line as written without the hashmark.
> #simple <- function(dframe){
>
> withNames <- function(dframe, lineNames, plotName, colors){
> ? ?p <- ggplot(dframe, aes(date, value, group = factor, color = factor))
> ? ?p2 <- p + geom_line(size = 1)
> # ? + opts(title = plotName)
> ? ?p2 <- p2 + facet_grid(Facet~., scales = 'free') +
> # ? ?p2 <- p2 + geom_text(data = dframe[dframe[,'date'] ==
'1941-06-16',],
> # ? ? ? ? ?aes(date, value, label = lineNames, vjust = 1)) +
> ? ? ? ? ?scale_colour_manual(values = colors)
> }
>
> finalP <- withNames(data, plines, pTitle, col1)
>
> #finalP <- simple(data)
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/