Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 scale_x_log10() destroys/doesn't apply for function plotted via stat_function()

Tags:

r

ggplot2

I finally managed to plot my custom fitted function over my data in ggplot2 but when I log-transform the x axis the plotted function gets totally messed up. It looks like the scale_x_log10() applies only to the plotted data but not to the function.

How can I make the function to appear in the correct scale?

Here is an modified example from Hadley's stat_function() documentation:

x <- rnorm(100)
qplot(x, geom="density") + stat_function(fun = dnorm, colour="red")

and now with log10 x-axis:

qplot(x, geom="density") + stat_function(fun = dnorm, colour="red") + scale_x_log10()

update

Okay, I think my example was not very helpful so I try it differently:

essentially what I want is to reproduce a plot I did with curve(). I fitted a Hill function to my data and now want to plot it:

# the function
HillFunction <- function(ec50,hill,rmax,x) {rmax/(1+(ec50/x)^hill)} 
# fitted parameters
hill.args <- list(ec50=10^-2, hill=.7, rmax=1)                     

curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5,log="x")  

so curve() gives me a smooth sigmoidal curve as expected. Now I try to reproduce the same plot with ggplot:

I add some data from 10^-5 to 10^5 just to define the plotting range, not sure if there are better ways

p <- ggplot(data=data.frame(x=c(10^-5:10^5)), aes(x=x))  + stat_function(fun=HillFunction, args=hill.args, n=3000, color="red")        

now if I plot p everything looks fine, like the curve() plot without the logscale:

p
curve(HillFunction(ec50=hill.args$ec50,rmax=hill.args$rmax, hill=hill.args$hill,x),from=10^-5, to=10^5)    

If I transform the coordinate system I get a sigmoidal curve but not smooth at all and the curve looks way to steep, but maybe that comes from x-scaling:

p + coord_trans(x="log10")

And if I define the x scale to be a log-scale the plot looks smooth but stops at 10^0:

p + scale_x_log10()

and I get the following warning: Removed 1500 rows containing missing values (geom_path).

like image 228
Dahaniel Avatar asked Feb 21 '23 09:02

Dahaniel


1 Answers

Proposed Solution

The following code is one way to get ggplot2 to do what I think you are trying to accomplish.

library(ggplot2)

# Define function. Fitted parameters included as default values.
HillFunction = function(x, ec50=0.01, hill=0.7, rmax=1.0) {
    result = rmax / (1 + (ec50 / x)^hill)
    return(result)
} 

# Create x such that points are evenly spread in log space.
x = 10^seq(-5, 5, 0.2) 
y_fit = HillFunction(x)
y_raw = y_fit + rnorm(length(y_fit), sd=0.05)

dat = data.frame(x, y_fit, y_raw)

plot_1 = ggplot(data=dat, aes(x=x, y=y_raw)) +
         geom_point() +
         geom_line(data=dat, aes(x=x, y=y_fit), colour="red") +
         scale_x_log10() +
         opts(title="Figure 1. Proposed workaround.")

png("plot_1.png", height=450, width=450)
print(plot_1)
dev.off()

Figure 1

Problems With stat_function()

  1. stat_function() is trying to evaluate HillFunction() for negative values of x. This why you get the missing values error.

  2. stat_function() is not evaluating HillFunction() for any x values between 0 and 1. It is selecting x in linear space, ignoring that scale_x_log10() has been specified.

The following code illustrates the problem, but I still can't explain why stat_function() diverges so much from y_fit in Figure 2.

plot_2 = ggplot(dat, aes(x=x, y=y_fit)) +
         geom_point() +
         stat_function(fun=HillFunction, colour="red") +
         scale_x_log10() +
         opts(title="Figure 2. stat_function() misbehaving?")

png("plot_2.png", height=450, width=450)
print(plot_2)
dev.off()


png("plot_3.png", height=450, width=450)

plot(x, y_fit, pch=20, log="x")
curve(HillFunction, col="red", add=TRUE)
title("Figure 3. curve() behaving as expected.")

dev.off()

Figure 2

enter image description here

like image 163
bdemarest Avatar answered Feb 24 '23 13:02

bdemarest