I would like to plot three functions on a log-log plot using ggplot2 (Ver 0.9.3.1) in R (Ver 3.0.2).
y = x
y = 0.5*x
y = 1.5*x
I have tried a number of things but keep running into issues. This includes reading into the stackoverflow question here.
An example of the what I would like to have is here. I generated that plot in Matlab.
The following is a sample of code that I am working with but currently it does not dray anything. Ultimately, I want to to be a layer on top of other data (which needs the log-log to show structure).
library(ggplot2)
plot = ggplot()
plot = plot + coord_cartesian(xlim = c(0.02, 300), ylim = c(0.035, 20))
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {x}, aes(x,y), geom = "line", color = "blue")
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {0.5*x}, aes(x,y), geom = "line", color = "red")
plot = plot + stat_function(data = data.frame(x=c(0,1000), y=c(0,1)), fun=function(x) {1.5*x}, aes(x,y), geom = "line", color = "red")
plot = plot + scale_x_log10() + scale_y_log10() + annotation_logticks()
plot
You can use coord_trans
instead of scale_.._log10
and coord_cartesian
Something like (using the examples from annotation_logticks
to get the breaks labelled appropriately)
ggplot(data = data.frame(x=c(0.0001,1000), y=c(0.001,1)), aes(x=x,y=y)) +
stat_function(fun = function(x) x, geom='line',colour ='blue') +
stat_function(fun = function(x) 0.5*x, geom='line',colour = 'red') +
stat_function(fun = function(x) 1.5 * x , geom = 'line', colour = 'red') +
coord_trans(xtrans = 'log10',ytrans = 'log10', limx = c(0.02,300), limy =c(0.035,20)) +
annotation_logticks(scaled=FALSE) +
scale_x_continuous(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_y_continuous(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
Note ?annotation_logticks
provide a number of approaches to this issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With