【问题标题】:Bar chart with error bar using log10 trans使用 log10 trans 带误差线的条形图
【发布时间】:2018-07-22 20:18:35
【问题描述】:

我是使用 R 和 ggplot 库的新手,我正在尝试构建带有错误栏的条形图,如下所示:

pd <- position_dodge(0.80)
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1) 
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)") 

唯一的事情是我想对 y 轴进行对数缩放,所以我尝试使用 scale_y_log10 函数:

ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd) 
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1) 
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)") 
+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x)))

但我有一个奇怪的结果,误差条有另一个比例,与条形图不同。

我该如何解决这个问题?我尝试使用+ ylim(10^-2, 10^1),但它不起作用。

【问题讨论】:

  • 这个有帮助吗:stackoverflow.com/a/9507037/8583393你可以试试scale_y_sqrt()
  • 谢谢,但我也有同样的问题,错误栏重叠了,而且有自己的比例。
  • 条形图在对数刻度上效果不佳,因为条形图的基线设置为 1 (10^0) 而不是零。您最好将值绘制为点,而不是条形图。此外,要设置限制,+ ylim(10^-2, 10^1) 将覆盖之前的 scale_y_log10()。相反,在scale_y_log10 中添加参数limits=c(10^-2, 10^1)。但是请注意,如果您将限制设置为小于数据范围,则超出限制的数据将被排除在外。为避免这种情况,请勿在scale_y_log10 中使用limits 参数。相反,添加+ coord_cartesian(ylim=c(10^-2, 10^1))
  • 请在aqp1 中包含部分或全部数据,以帮助人们回答问题。

标签: r ggplot2 bar-chart


【解决方案1】:

我只想引用@eipi10

条形图不适用于对数刻度,因为条形图的基线设置为 1 (10^0) 而不是零

这解释了为什么图像会出错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多