Search code examples
rggplot2scale

Issue with log_2 scaling using ggplot2 and log2_trans()


I am trying to plot data using ggplot2 in R. The datapoints occur for each 2^i-th x-value (4, 8, 16, 32,...). For that reason, I want to scale my x-Axis by log_2 so that my datapoints are spread out evenly. Currently most of the datapoints are clustered on the left side, making my plot hard to read (see first image). I used the following command to get this image:

ggplot(summary, aes(x=xData, y=yData, colour=groups)) +
geom_errorbar(aes(ymin=yData-se, ymax=yData+se), width=2000, position=pd) +
geom_line(position=pd) +
geom_point(size=3, position=pd)

Plot without scaling

However trying to scale my x-axis with log2_trans yields the second image, which is not what I expected and does not follow my data. Code used:

ggplot(summary, aes(x=settings.numPoints, y=benchmark.costs.average, colour=solver.name)) +
geom_errorbar(aes(ymin=benchmark.costs.average-se, ymax=benchmark.costs.average+se), width=2000, position=pd) +
geom_line(position=pd) +
geom_point(size=3, position=pd) +
scale_x_continuous(trans = log2_trans(),
                 breaks = trans_breaks("log2", function(x) 2^x),
                 labels = trans_format("log2", math_format(2^.x)))

wrong log2 scaling

Using scale_x_continuous(trans = log2_trans()) only doesn't help either.

EDIT:

Attached the data for reproducing the results: https://pastebin.com/N1W0z11x

EDIT 2: I have used the function pd <- position_dodge(1000) to avoid overlapping of my error bars, which caused the problem. Removing the position=pd statements solved the issue


Solution

  • I have used the function pd <- position_dodge(1000) to avoid overlapping of my error bars, which caused the problem. Adjusting the amount of position dodge and the with of the error bars according to the new scaling solved the problem.

    pd <- position_dodge(0.2) # move them .2 to the left and right
    
    ggplot(summary, aes(x=settings.numPoints, y=benchmark.costs.average, colour=algorithm)) +
    geom_errorbar(aes(ymin=benchmark.costs.average-se, ymax=benchmark.costs.average+se), width=0.4, position=pd) +
    geom_line(position=pd) +
    geom_point(size=3, position=pd) +
    scale_x_continuous(
      trans = "log2",
      labels = scales::math_format(2^.x, format = log2)
    )
    

    enter image description here

    Adding scale_y_continuous(trans="log2") yields the results I was looking for:

    enter image description here