由于在 y 对数刻度中转换无限值,奇数 geom_bar 误差条
Odd geom_bar error bars due to transforming infinite values in y-log scale
在观察 2、3、5 上,在 LOG10 刻度上绘图时,我得到了不完整的变量误差线,我收到以下消息:
NaNs produced Transformation introduced infinite values in continuous
y-axis
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-01-08 创建
在观察 2、3、5 上,在 LOG10 刻度上绘图时,我得到了不完整的变量误差线,我收到以下消息:
NaNs produced Transformation introduced infinite values in continuous y-axis
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-01-08 创建