geom_errorbar issue displaying geom_linerange() instead

I am trying to plot using geom_errorbar() but the ggplot display geom_linerange() looking plot instead. I tried to reproduce an exact example dataset structure as my dataset but the example code below was able to correctly plot geom_errorbar(). The only difference is that my dataset's variability (sd, standard deviation) is quite small at many timepoints. However, even when I tweak and decrease the example dataset variability by 100 fold DV_sd = sd(conc)/100, it still displays correctly geom_errorbar(). I am not sure why I am unable to plot geom_errorbar() on my actual dataset as it continues to look like geom_linerange().

library(tidyverse)
data(Indometh)

Indometh1 <- Indometh %>%
  mutate(conc = conc * 1) %>%
  mutate(Subject = as_numeric(Subject) + 0) %>%
  mutate(Group = "Group 1")

Indometh2 <- Indometh %>%
  mutate(conc = conc * 10) %>%
  mutate(Subject = as_numeric(Subject) + 6) %>%
  mutate(Group = "Group 2")

Indometh3 <- Indometh %>%
  mutate(conc = conc * 100) %>%
  mutate(Subject = as_numeric(Subject) + 6*2) %>%
  mutate(Group = "Group 3")

Indometh_combine <- bind_rows(Indometh1, Indometh2, Indometh3)


Indometh_summary <- Indometh_combine %>%
  group_by(Group, time) %>%
  summarize(DV_mean = mean(conc),
            DV_sd = sd(conc) ) %>%
  ungroup()

Indometh_summary %>%
  ggplot(aes(x = time, y = DV_mean, ymin = DV_mean - DV_sd, ymax = DV_mean + DV_sd, color = Group)) +
  geom_point() +
  geom_line() +
  geom_errorbar(width = 0.25) +
  scale_y_continuous(
    trans = "log10",
    limits = c(0.01, 500),
    breaks = c(0.01, 0.1, 1, 10, 100, 500),
    labels = c(0.01, 0.1, 1, 10, 100, 500)
  ) +
  annotation_logticks(sides = "l") +
  theme_bw() 

I was able to solve this issue by modifying width argument in geom_errorbar() to a much higher number (ie, geom_errorbar(width = 15) )

You can replicate the issue you were having, by modifying your example by changing aes(x = time, ... to aes(x = time * 1000, .... When the x-axis is continuous, and you set the width of the errorbar manually, it will be on the scale of the x-axis. Using geom_errorbar() instead of geom_errorbar(width = 0.25) will try to choose a reasonable default, which also fixes the issue with your example.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.