rjuliaaxisgadfly

Adjusting tick spacing with log scale in Gadfly


I am trying to create the following plot in Julia using Gadfly:

Example plot
(source: missouri.edu)

I have some existing R code that creates a blank base for this as part of a larger plotting function using matplot with the parm log='xy' which results in correctly spaced ticks.

...matplot(tpa, qmd, ylab = "Quadratic Mean Diameter", 
xlab = "Trees per acre", type = "n",
log = "xy", xlim = c(1,1200),
ylim = c(1, 40),main ="")...

The Julia code I currently have is

Using Gadfly
plot(x=[1],y=[1],
Scale.x_log, Scale.y_log,
Scale.x_continuous(minvalue=1,maxvalue=1200),
Scale.y_continuous(minvalue=1,maxvalue=40),
Guide.xticks(ticks=[1,5,10,50,100,500,1000]),
Guide.YTicks(ticks=[1,2,5,10,20]),
Guide.XLabel("Trees per acre"), Guide.YLabel("Quadratic Mean Diameter"),
Theme(grid_color=colorant"black"),
Geom.point)

which creates the ticks at the proper values, but the spacing is off resulting in the lower values being condensed onto each other. I have tried modifying the Scale.x_log and moving the argument to after the continuous argument, but I cannot recreate the desired axes from the example plot.

Any advice on how I can adjust the log scale axis parameters to create the plot above?

Would this be easier using a different graphics library for this? I choose Gadfly as I am more comfortable with the ggplot syntax, but I kept coming across plots() and the Pyplot() packages in my googling.

EDIT/WORKING CODE:

I got it working, the sticking point was to pass the log of the desired breakpoints. Much thanks to Felipe for the lambda function, I was not familiar with how those worked.

myxticks=log([1,5,10,50,100,500,1000])
myyticks=log([1,2,5,10,20])

plot(x=[1], y=[1],
Geom.point,
Scale.x_log(labels=d-> @sprintf("%d",e^d)),
Scale.y_log(labels=d-> @sprintf("%d",e^d)),
Guide.xticks(ticks=myxticks),
Guide.yticks(ticks=myyticks)
) 

Solution

  • You should use Scale.x_log instead of Scale.x_continuous. If you see the source, you can see the only difference between these two is the transformation function.*

    *: I would call it translation function, but hey that's just me.

    EDIT:

    So the above doesn't quite do it for spacing, which requires a little more back-and-forth between log and exp. The following should help you as a skeleton, though you'll have to play with parameters to get what you want:

    using Gadfly
    pts=collect(1:20)
    theticks=collect(log(linspace(1,20,10)))
    plot(x=pts, y=pts,
         Geom.point, Scale.x_log(labels=d-> @sprintf("%d",e^d)),
         Guide.xticks(ticks=theticks))
    

    enter image description here