问题描述
在观察 2、3、5 上,在 LOG10 标度上绘图时,我得到了不完整的变量误差条,我收到以下消息:
NaN 产生的转换在连续中引入了无限值 y轴
library(tidyverse)
df <- tibble::tibble(
Observation = rep(1:5,2),Type = rep(c(rep("A",5),rep("B",5))),value = c(33046,970250,1870125,259625,3838750,196,578,323,509,215),sd = c(8538,319023,1538959,27754,1602186,161,687,380,474,282))
ggplot(df,aes(x=Observation,y=value,fill=Type)) +
geom_bar(stat="identity",color="black",position=position_dodge()) +
geom_errorbar(aes(ymin=value-sd,ymax=value+sd),width=.2,position=position_dodge(.9)) + theme_classic() +
# scale_fill_manual(values=c('#999999','#E69F00'))+
scale_y_continuous(trans='log10')
请问我该如何解决?
解决方法
这是因为 df$value - df$sd
产生负值,无法进行 log10 转换。我建议将这些值剪裁为某个正值。下面的示例使用 pmax()
将最小值设置为 1。
library(tidyverse)
df <- tibble::tibble(
Observation = rep(1:5,2),Type = rep(c(rep("A",5),rep("B",5))),value = c(33046,970250,1870125,259625,3838750,196,578,323,509,215),sd = c(8538,319023,1538959,27754,1602186,161,687,380,474,282))
ggplot(df,aes(x=Observation,y=value,fill=Type)) +
geom_bar(stat="identity",color="black",position=position_dodge()) +
geom_errorbar(aes(ymin=pmax(value-sd,1),ymax=value+sd),width=.2,position=position_dodge(.9)) + theme_classic() +
scale_y_continuous(trans='log10',oob = scales::oob_squish_any)
由 reprex package (v0.3.0) 于 2021 年 1 月 8 日创建