rggplot2graphicswidtherrorbar

R ggplot2 - issue with errorbar width


I've been trying to plot the following data using ggplot2, but I ran into an issue with the errorbars:

data1<-c(0.04, 0.5, 1.0, 1.5 ,2.0, 2.6, 3.1, 3.6,  0.9,0.56,0.4,0.33,0.27,0.2,0.15,0.1, 0.12, 0.17, 0.22 ,0.33, 0.45,  0.57, 0.74, 0.85)

sym<-as.data.frame(matrix(data = data1, ncol = 3, byrow = FALSE))

ggplot(data=sym, mapping = aes(x=sym[,1], y=sym[,2]))+
  theme(plot.background = element_rect(fill = "white"), 
        panel.background = element_rect(fill = "white", color="black",linetype = 1),
        panel.grid = element_blank(),
        plot.title = element_text(hjust = 0.5, size =30),
        axis.title = element_blank(),
        axis.ticks = element_line(color="black"),
        axis.ticks.length=unit(-0.25, "cm"),
        axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 
        axis.text.y = element_text(size=25, margin=unit(c(0.5,0.5,0.5,0.5), "cm")))+
  ggtitle("variable, symmetric error")+
  ylim(-1.0, 1.5)+
  xlim(0.0,4.5)+
  geom_line(color="blue",size=1.2)+
  geom_point(color="blue", size=4)+
  geom_errorbar(aes(ymin = sym[,2]-sym[,3], ymax = sym[,2]+sym[,3], x= sym[,1]),
                width=0.1, color="blue", size =1.2)

The result is (mainly) as expected, but I cannot figure out why the first errorbar is not responding to the "width" setting. Plot showing the issue with the first errorbar, which is not responding to the set width parameter

My first guess was that changing the margin may have caused some sort of overlapping, which led R to not plot width-wise for the very first point. However, shifting the data inwards on the x-axis made no difference. Therefore I'm assuming I must have missed something else?

Any input is very much appreciated!


Solution

  • Pointing to the great suggestions of @stefan and @RuiBarradas about the variable names and limits, you can also try scale_x_continuous() and scale_y_continuous() for limits as it is used in next code:

    ggplot(data=sym, mapping = aes(x=V1, y=V2))+
      theme(plot.background = element_rect(fill = "white"), 
            panel.background = element_rect(fill = "white", color="black",linetype = 1),
            panel.grid = element_blank(),
            plot.title = element_text(hjust = 0.5, size =30),
            axis.title = element_blank(),
            axis.ticks = element_line(color="black"),
            axis.ticks.length=unit(-0.25, "cm"),
            axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")), 
            axis.text.y = element_text(size=25, margin=unit(c(0.5,0.5,0.5,0.5), "cm")))+
      ggtitle("variable, symmetric error")+
      scale_y_continuous(limits = c(NA,1.5))+
      scale_x_continuous(limits = c(NA,4.5))+
      geom_line(color="blue",size=1.2)+
      geom_point(color="blue", size=4)+
      geom_errorbar(aes(ymin = V2-V3, ymax = V2+V3, x= V1),
                    width=0.1, color="blue", size =1.2)
    

    enter image description here