[R] Y error bars, dates on the X-axis

Dennis Murphy djmuser at gmail.com
Fri Jun 3 18:59:56 CEST 2011


Hi:

Here's one way to do it with ggplot2; it contains the basic elements
you need, including a 'geom' for error bars. Two data frames are used:
one that contains the raw data, another that produces the daily means
and standard deviations. A couple of bells and whistles are added
(e.g., point jittering and a line to connect the daily means), but
don't let it intimidate you if this is all new.

library(ggplot2)      # also loads the plyr package to be used below

# Generate the data:
df <- data.frame(Date = rep(seq(as.Date('2011-05-01'), by = 'days',
length = 10), each = 50),
                 y = rnorm(500), stringsAsFactors = FALSE)

# Summarize it, outputting the mean and standard deviation by Date:
dfsumm <- ddply(df, .(Date), summarise, yavg = mean(y), sdy = sd(y))

# This plot uses two data frames. Each 'geom' is a type of graphical object
# which adds a 'layer' to the plot. The first is a scatterplot of the data where
# x = Date, y = y and the points are jittered. aes() represents the 'aesthetics'
# of the geom, which in this case are the x and y variables of the scatterplot.
# The second layer is the error bar plot, which needs x, ymin and ymax as
# aesthetics; width refers to the width of the horizontal bars, size
to its thickness.
# Point and line plots complete the display. Notice that after the first layer,
# the input data frame is dfsumm rather than df. The common date in each
# data frame is key.

ggplot() +
   geom_point(data = df, aes(x = Date, y = y),
              position = position_jitter(width = 0.2)) +
   geom_errorbar(data = dfsumm, aes(x = Date, ymin = yavg - sdy,
                 ymax = yavg + sdy), colour = 'red', width = 0.4, size = 1.5) +
   geom_point(data = dfsumm, aes(x = Date, y = yavg), colour = 'blue',
size = 2.5) +
   geom_line(data = dfsumm, aes(x = Date, y = yavg), colour = 'blue', size = 1)

If you find this interesting, see http://had.co.nz/ggplot2 for more
information about the package, including a set of on-line help pages.

HTH,
Dennis

On Fri, Jun 3, 2011 at 8:41 AM, Terhukka <terhi at live.co.uk> wrote:
> Dear forum,
>
>
> Does anyone know any functions for plotting vertical error bars, if the
> x-axis variable is a date?
>
> I can produce a pretty plot with dates on the x-axis, if I don't want to
> include error bars. I can also produce a nice plot with error bars, if I
> just use Julian days for the x-axis, but I'd much rather have the actual
> dates.
>
> I have tried writing my own function, plotCI {gplots} and errbar {Hmisc}.
>
>
> This is my own version:
>
> y.error.bars<-function(x,y,ybar){plot(x,y,pch=16,ylim=c(min(y-ybar),max(y+ybar)))
> arrows(x,y-ybar,x,y+ybar,code=3,angle=90,length=0.1)}
> y.error.bars(x,y,yb)
>
> It works if x is numeric, but once change the x variable to be a date, I get
> the following error message:
> "Error in arrows(x, y - ybar, x, y + ybar, code = 3, angle = 90, length =
> 0.1) :
>  invalid first argument"
>
>
> Thanks!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Y-error-bars-dates-on-the-X-axis-tp3571219p3571219.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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