Re: Plots of backtransformed data

474 views
Skip to first unread message

Dennis Murphy

unread,
Sep 5, 2012, 6:17:04 AM9/5/12
to kirsty, ggp...@googlegroups.com
Hi:

It's a little difficult to help since you didn't provide a reproducible example, but your question appears to have something to do with the distinction between scale transformations and coordinate transformations. Perhaps section 7.3 of
http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf
might be of some assistance; if not, please provide a reproducible example (small data set + code) to make it easier for others to help. We don't have mydata2, nor do we know what is options1. If you provide a small data set, please use dput form to send it (see ?dput if this is news to you).

Stab in the dark:
Coordinate transformation is done after statistical transformation, so you should either not use transformed data in the initial ggplot() call or you should be using inverse transforms in coord_trans(). The example using the diamonds data in the cited section of the aforementioned document demonstrates what I mean.

Dennis

On Tue, Sep 4, 2012 at 11:06 PM, kirsty <nashk...@gmail.com> wrote:
I have produced a ggplot of log-log data fitted with a linear model (plotb below).  I then wanted to produce the same plot backtransformed (plotg below).  This has worked great using the script below.  But when the data is presented in back-transformed state, the axes labels are still in the log scale rather than in the arithmetic scale.  Is there anyway to modify the script below so I have the back transformed plot but can change the scale labeling to be arithmetic:


plota=ggplot(mydata2, aes(log10(Size),  log10(IntraFD),group=Site))+geom_point(aes(shape=Site), size = 4, alpha = I(0.5))+options1
plotb=a+geom_smooth(aes(log10(Size),  log10(IntraFD),group=1), method=lm)

plotg=b+coord_trans(x="pow10",y="pow10")+xlab("Log body length (cm)")+ylab("Log intraforay distance (cm)")+opts(title=NULL)


Thank you for all suggestions.

Cheers
Kirsty

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

kirsty

unread,
Sep 5, 2012, 9:48:50 PM9/5/12
to ggp...@googlegroups.com, kirsty
Thank you Dennis,

Great tip about dput.

So here is hopefully a better example so people can understand what I am trying to do:


dataset1=structure(list(x = c(27.6, 31.1, 23.2, 22.5, 23.5, 22.3, 24.6,
22.5, 24.6, 15, 6.6, 7.1, 7.4, 21, 21.2, 21.3, 28.3, 28.4, 28.6,
26.9), y = c(260.4, 648.5, 254.2, 250.6, 244.1, 265.4, 507.8,
407.6, 896.1, 54.2, 26.9, 39, 45.8, 131.1, 135.2, 134, 526.5,
580.8, 481.4, 476.9), site = structure(c(1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("A",
"B"))), .Names = c("x", "y", "site"), row.names = c(NA, -20L), class = "data.frame")

library(ggplot2) #load library

#create plot of log y against log x with the shape of the points specified by variable shape.
#Make shapes of size 4 and partially transparent
a=ggplot(dataset1, aes(log10(x),  log10(y),group=site))+geom_point(aes(shape=site), size = 4, alpha = I(0.5))    

#to a add a line for the linear model of log y against log x ignoring site.
b=a+geom_smooth(aes(log10(x),  log10(y),group=1), method=lm)

#backtransform b so now on arithmetic scales
g=b+coord_trans(x="pow10",y="pow10")

#print b and g next to each other
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
vplayout=function(x,y)
viewport(layout.pos.row=x,layout.pos.col=y)
print(b,vp=vplayout(1,1))
print(g,vp=vplayout(1,2))

This produces two plots that look exactly as I would like, except I would like the scale labels on the backtransformed plot (g) to be an arithmetic scale not a backtransformed log scale.  I would like the tick marks to be equally spaced, and the tick labels to go from 5 to 35 rather than be in scientific format.  I had a look at the link you posted Denis, and I didnt get much further.

Cheers
Kirsty

Dennis Murphy

unread,
Sep 6, 2012, 3:19:27 AM9/6/12
to kirsty, ggp...@googlegroups.com
Here's one way to get the back-transformed fitted model in the original units of measurement.

library(ggplot2)
library(scales)

# Base plot
p <- ggplot(dataset1, aes(x, y)) +
          geom_point(aes(shape = site), size = 4, alpha = 0.5)

# Rescale to log coordinates and fit the model
p1 <- p + scale_x_continuous(trans = "log") + scale_y_continuous(trans = "log") +
           geom_smooth(aes(group = 1), method = "lm")

# Now back-transform using coord_trans():
p1 + coord_trans(x = "exp", y = "exp")

Dennis

kirsty

unread,
Sep 7, 2012, 12:28:20 AM9/7/12
to ggp...@googlegroups.com, kirsty
Thank you Dennis,

Very happy with the plots!

Cheers
Kirsty

Sergei Petrov

unread,
May 2, 2016, 7:25:31 AM5/2/16
to ggplot2, nashk...@gmail.com
My dirty hack for boxcox

> my.rev.boxcox <- function (p)
+ {
+     if (abs(p) < 1e-07)
+         return(log_trans())
+     trans <- function(x) (abs(x) * p + 1 * sign(x))^(1/p)
+     inv <- function(x) (x^p - 1)/p * sign(x - 1)
+     trans_new(paste0("pow-", format(p)), trans, inv)
+ }
> ggplot(data.pr, aes(x=Age, y=ALT, color=Gender, fill=Gender)) + scale_y_continuous(trans=boxcox_trans(-0.3069934)) + geom_smooth(size=1, method="gam", formula=y~s(x)) + theme_bw()+theme(legend.position="bottom")   + coord_trans(y = my.rev.boxcox(-0.3069934))


coord_trans() does not have any options for replace methods "trans" & "inv"?


Sergei

четверг, 6 сентября 2012 г., 10:19:27 UTC+3 пользователь Dennis Murphy написал:
Reply all
Reply to author
Forward
0 new messages