Transformation of scales within a function

21 views
Skip to first unread message

Xavier Fernández i Marín

unread,
Nov 24, 2012, 6:16:29 AM11/24/12
to ggp...@googlegroups.com
Dear all,

I would like to transform the x axis, so that instead of getting, say:
0, 50, 100, 150, 200

the labels appear with a transformation that depends on 'x', and on another
attribute of the dataframe (say, attribute(df)$myattribute), such that the
final result is x * myattribute

I can create a function to change the labels:
-----8<---------------
myformat <- function(x) return(x * attributes(df)$myattribute
-----8<---------------

But my problem is that this can't be done inside a function that, as an
argument, has the dataframe:
-----8<---------------
f <- function(df) {
# Some calculations
# ...
ggplot(df, ....) + scale_x_continuous(labels=myformat)
}
-----8<---------------


So the question is how can I pass a secondary argument to
'scale_x_continuous' that refers to an attribute of the original dataframe?



I have tried several approaches with no luck, including a double function:
-----8<---------------
myformat <- function() {
function(x) return(x * attributes(df)$myattribute
}
# And then inside the f function:
ggplot(df, ....) + scale_x_continuous(labels=myformat())
-----8<---------------

And passing all arguments, but then 'x' is not found:
-----8<---------------
myformat <- function(x, a) return(x * a)
ggplot(df, ....) + scale_x_continuous(labels=myformat(x, a=attributes(df)$myattribute))
-----8<---------------


Thank you,
--
- Xavier -
Reply all
Reply to author
Forward
0 new messages