[R] ggplot2-Issue placing error bars behind data points

Dennis Murphy djmuser at gmail.com
Thu Sep 8 00:33:01 CEST 2011


Hi:

For your test data, try this:

# Result of dput(NerveSurv)
NerveSurv <- structure(list(Time = c(0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 0,
0.25, 0.5, 0.75, 1, 1.25, 1.5, 0, 0.25, 0.5, 0.75, 1, 1.25, 1.5
), SAP = c(1, 1.04, 1.04, 1.06, 1.04, 1.22, 1.01, 1, 1.01, 1.01,
1.06, 1.01, 0.977, 0.959, 1, 1.01, 1.06, 0.921, 0.951, 0.904,
0.911), Temp = c(25L, 25L, 25L, 25L, 25L, 25L, 25L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), SAPSE = c(0,
0.0412, 0.0935, 0.0818, 0.131, 0.144, 0.0712, 0, 0.0156, 0.0337,
0.0481, 0.0168, 0.00486, 0.0155, 0, 0.00462, 0.0491, 0.0329,
0.0304, 0.0471, 0.0722)), .Names = c("Time", "SAP", "Temp", "SAPSE"
), class = "data.frame", row.names = c(NA, -21L))

limits<-aes(ymax = NerveSurv$SAP + NerveSurv$SAPSE,
                 ymin = NerveSurv$SAP - NerveSurv$SAPSE)

# library('ggplot2')
p <- ggplot(data=NerveSurv,aes(x=Time,y=SAP))
p + geom_errorbar(limits,width=0.2) +
    geom_point(aes(fill=factor(Temp)), colour = colors()[173],
                 shape = 21, size=4) +
    xlab("Time (min)") +
    xlim(c(0, 2)) +
    ylab("Normalized Spontaneous Action Potential Rate") +
    ylim(c(0,1.6)) +
    scale_fill_manual("Acclimation\nTemperature", breaks=c(8,18,25),
                      labels=c("25ºC", "18 ºC", "8 ºC"),
                      values=c(colours()[c(173,253,218)]))

Notice the following in the above code:

(1) As Jeff Newmiller pointed out, put geom_errorbar() before geom_point().
(2) If you are going to be using the same value of a plot aesthetic, you
     *set* it outside aes() rather than *map* it inside aes().
     See the revised code for geom_point(). The idea is that if a
variable is assigned
     to a plot aesthetic (e.g., color, fill, shape), then it needs to
be mapped inside
     aes(); if an aesthetic is set to a specific value, it is set outside aes().
(3) Your test data had an effective x-range of 0 - 1.5 rather than 0 - 50, so
     I shrunk xlim() for readability.
(4) You can use \n inside of a character string as a carriage return. See
     the legend title for scale_fill_manual().
(5) Style note: you want the layer addition operator + to be at the end of a
     line, not at the beginning. Copy and paste this code verbatim to
see what I mean:
p + geom_errorbar(limits,width=0.2)
+  geom_point(aes(fill=factor(Temp)), colour = colors()[173],
                 shape = 21, size=4)

This happens because the first line is syntactically complete.

HTH,
Dennis

On Wed, Sep 7, 2011 at 2:48 PM, Nathan Miller <natemiller77 at gmail.com> wrote:
> Hi all,
>
> This seems like a basic problem, but no amount of playing with the code has
> solved it. I have a time-series data set like that shown below (only longer)
> and am seeking to plot the data with filled, circular points and error bars.
> I would like the error bars to be behind the points otherwise they tend to
> obscure the points (especially when I have a lot of points in the actual
> data set). Despite providing a fill colour for the points and using shapes
> that utilize fill colours, the error bars are always placed on top of the
> points. Can anyone see the error I am making? I simply want to move the
> error bars so they are behind the data points.
>
> Thanks for your help. I assume its simple...or else its a bug.
>
> Nate
>
>
>  Time      SAP Temp    SAPSE
>  0.00 1.000000   25     0.000000
>  0.25 1.040000   25     0.041200
> 0.50 1.040000   25      0.093500
> 0.75 1.060000   25     0.081800
> 1.00 1.040000   25     0.131000
> 1.25 1.220000   25     0.144000
> 1.50 1.010000   25     0.071200
> 0.00 1.000000   15     0.000000
> 0.25 1.010000   15     0.015600
>  0.50 1.010000   15    0.033700
> 0.75 1.060000   15    0.048100
> 1.00 1.010000   15    0.016800
> 1.25 0.977000   15    0.004860
> 1.50 0.959000   15    0.015500
> 0.00 1.000000    8     0.000000
> 0.25 1.010000    8     0.004620
> 0.50 1.060000    8     0.049100
> 0.75 0.921000    8     0.032900
> 1.00 0.951000    8     0.030400
> 1.25 0.904000 8     0.047100
> 1.50 0.911000 8     0.072200
>
> limits<-aes(ymax=NerveSurv$SAP+NerveSurv$SAPSE,ymin=NerveSurv$SAP-NerveSurv$SAPSE)
>
> p<-ggplot(data=NerveSurv,aes(x=Time,y=SAP))
>
> p+geom_point(aes(colour=factor(Temp), shape=factor(Temp),
> fill=factor(Temp)), size=4)
> +geom_errorbar(limits,width=0.2)
> +xlab("Time (min)")
> +xlim(c(0,50))
> +ylab("Normalized Spontaneous Action Potential Rate")
> +ylim(c(0,1.6))
> +scale_shape_manual("Acclimation Temperature",breaks=c(8,18,25),
> labels=c("25 ºC", "18 ºC", "8 ºC"),values=c(21,21,21))
> +scale_fill_manual("Acclimation Temperature",breaks=c(8,18,25), labels=c("25
> ºC", "18 ºC", "8 ºC"),values=c(colours()[c(173,253,218)]))
> +scale_colour_manual("Acclimation Temperature",breaks=c(8,18,25),
> labels=c("25 ºC", "18 ºC", "8 ºC"), values=c(colours()[c(173,173,173)]))
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list