使用 ggplot2 和 log2_trans() 进行 log_2 缩放的问题

Issue with log_2 scaling using ggplot2 and log2_trans()

我正在尝试使用 R 中的 ggplot2 绘制数据。 数据点出现在每个第 2^i 个 x 值(4、8、16、32,...)。出于这个原因,我想按 log_2 缩放我的 x 轴,以便我的数据点均匀分布。目前大多数数据点都集中在左侧,使我的图难以阅读(见第一张图片)。 我使用以下命令获取此图像:

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)

然而,尝试使用 log2_trans 缩放我的 x 轴会产生第二张图像,这不是我所期望的并且不符合我的数据。 使用的代码:

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)))

仅使用 scale_x_continuous(trans = log2_trans()) 也无济于事。

编辑:

附上重现结果的数据: https://pastebin.com/N1W0z11x

编辑 2: 我使用函数 pd <- position_dodge(1000) 来避免我的错误栏重叠,这导致了问题。 删除 position=pd 语句解决了问题

这是一种格式化 x 轴的方法:

# Generate dummy data
x <- 2^seq(1, 10)
df <- data.frame(
  x = c(x, x, x),
  y = c(0.5*x, x, 1.5*x),
  z = rep(letters[seq_len(3)], each = length(x))
)

剧情大概是这样的:

ggplot(df, aes(x, y, colour = z)) +
  geom_point() +
  geom_line()

调整 x 轴的方式如下:

ggplot(df, aes(x, y, colour = z)) +
  geom_point() +
  geom_line() +
  scale_x_continuous(
    trans = "log2",
    labels = scales::math_format(2^.x, format = log2)
  )

labels 参数只是为了让您拥有 2^x 格式的标签,您可以将其更改为任何您喜欢的格式。

我使用函数 pd <- position_dodge(1000) 来避免我的错误栏重叠,这导致了这个问题。 根据新的缩放比例调整位置闪避量和误差条的宽度解决了问题。

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)
)

添加 scale_y_continuous(trans="log2") 得到我正在寻找的结果: