I am trying to display error bars using geom_errorbar()
after using coord_trans()
to plot the x-data in semilog scale. Somehow geom_errorbar()
does not work but geom_linerange()
does.
My code is as follows:
data <- data.frame(x=c(1, 10, 25, 50, 100, 500, 1000),
y=c(0.3990093, 0.6062332, 0.7663158, 0.9779979, 1.6546543, 8.2825181, 18.2218369),
std=c(0.09528301, 0.03620494, 0.01246995, 0.06584974, 0.15254007, 0.29668775, 0.86524986))
data %>%
ggplot()
geom_point(aes(x, y), colour="blue")
coord_trans(x = "log10")
geom_smooth(aes(x, y), method = "lm", formula = y ~ x, lwd=0.75, se = F, colour="lightblue")
#geom_errorbar(aes(x=x, ymin=y-std, ymax=y std), colour="black")
geom_linerange(aes(x=x, ymin=y-std, ymax=y std), colour="black")
However, geom_linerange()
only displays a vertical bar and I would like an horizontal bar on top of the vertical bar, as geom_errorbar()
usually allows to display.
Does anyone know how to do that? Thank you in advance!
Estelle
CodePudding user response:
Seems to work ok if we switch the transformation-plotting order by putting the transformation into scale_x_continuous
instead of using coord_trans
.
data %>%
ggplot()
geom_point(aes(x, y), colour="blue")
scale_x_continuous(trans = scales::log10_trans())
geom_smooth(aes(x, y), method = "lm", formula = y ~ x, lwd=0.75, se = F, colour="lightblue")
geom_errorbar(aes(x=x, ymin=y-std, ymax=y std), colour="black", width = 0.05)
geom_linerange(aes(x=x, ymin=y-std, ymax=y std), colour="black")
CodePudding user response:
A collegue and I found a solution.
It works if a width parameter is specified. As the x-axis is in log-scale, the width parameter needs to be a function of the x-variable, otherwise you won't be able to see it for the values with a higher x-component.
geom_errorbar(aes(x=x, ymin=y-std, ymax=y std), colour="black", width=0.1*data$x)