can we use several time geom_errorbar in the same plot?

70 views
Skip to first unread message

Vincent Margerin

unread,
Nov 15, 2011, 8:19:47 PM11/15/11
to ggplot2
Hi,

Not sure the subject is clear, I would like to plot on the same graph
2 sets of data points, each one is of different length and has its own
errors associated with it.

Cheers

Vincent Margerin


x4 <- c(330, 835, 317, 396, 486, 391)
y4 <- c(4098.11, 4099.14, 4098.16, 4098.35, 4099.30, 4096.73)
y4b <- c(0.47, 0.29, 0.46, 0.83, 0.72, 0.87)
t4 <- sum(y4/y4b)/(sum(1/y4b))

x5 <- c(721, 257, 204, 271)
y5 <- c(4096.33, 4095.51, 4095.29, 4095.19)
y5b <- c(0.10, 0.15, 0.18, 0.22)
t5 <- sum(y5/y5b)/(sum(1/y5b))

dat.tgg817a <- data.frame(x4, y4, y4b)
dat.tgg817b <- data.frame(x5, y5, y5b)

xplot4 <- seq(100,1000)
yplot4 <- t4
xplot5 <- seq(100,1000)
yplot5 <- t5

x45 <- c(x4, x5)
y45 <- c(y4, y5)
y45b <- c(y4b, y5b)


### I tried to do the following but does not work
# qplot(x4, y4, ylim=c(4094,4101)) + geom_errorbar(aes(ymin= y4 - y4b,
ymax= y4 + y4b), width = 3, col='NavyBlue') +
geom_line(aes(xplot4,yplot4), col='NavyBlue', cex=2) +
geom_point(aes(x5, y5), col='DarkRed') + geom_line(aes(xplot5,yplot5),
col='DarkRed', cex=2) + geom_errorbar(aes(ymin= y5 - y5b, ymax= y5 +
y5b), width = 3, col='DarkRed')

### Used this very poor method but it's not nice
qplot(x45, y45, ylim=c(4094,4101)) + geom_errorbar(aes(ymin= y45 -
y45b, ymax= y45 + y45b), width = 3) + geom_point(aes(x5, y5),
col='Red') + geom_point(aes(x4, y4), col='RoyalBlue') +
geom_line(aes(xplot4,yplot4), col='NavyBlue', cex=1.5) +
geom_line(aes(xplot5,yplot5), col='DarkRed', cex=1.5)

Brandon Hurr

unread,
Nov 16, 2011, 4:10:54 AM11/16/11
to Vincent Margerin, ggplot2
You're doing something quite complicated, perhaps it's time you use
ggplot() instead of qplot(). Try putting all of your vectors into 1 or
2 data.frame(s) and redoing the plot (like so)...

x4 <- c(330, 835, 317, 396, 486, 391)y4 <- c(4098.11, 4099.14,
4098.16, 4098.35, 4099.30, 4096.73)y4b <- c(0.47, 0.29, 0.46, 0.83,
0.72, 0.87)y4min<-y4-y4by4max<-y4+y4b
dat.tgg817a <- data.frame(x4, y4, y4b,y4max, y4min)


t4 <- sum(y4/y4b)/(sum(1/y4b))
x5 <- c(721, 257, 204, 271)y5 <- c(4096.33, 4095.51, 4095.29,

4095.19)y5b <- c(0.10, 0.15, 0.18, 0.22)y5min<-y5-y5by5max<-y5+y5b
dat.tgg817b <- data.frame(x5, y5, y5b, y5max, y5min)
t5 <- sum(y5/y5b)/(sum(1/y5b))

ggplot()+geom_point(data=dat.tgg817a, aes(x=x4,
y=y4))+geom_errorbar(data=dat.tgg817a, aes(x=x4, y=y4, ymin= y4min,
ymax= y4max), width = 3,
colour='NavyBlue')+geom_hline(aes(yintercept=t4), colour ='NavyBlue',
size=2)+geom_point(data=dat.tgg817b, aes(x=x5, y=y5),
colour='DarkRed') + geom_hline(aes(yintercept=t5), colour ='DarkRed',
size=2) + geom_errorbar(data=dat.tgg817b, aes(x=x5, y=y5, ymin= y5min,
ymax= y5max), width = 3,
colour='DarkRed')+coord_cartesian(ylim=c(4094,4101), wise=TRUE)

Vincent Margerin

unread,
Nov 16, 2011, 8:31:27 AM11/16/11
to ggplot2
Thanks a lot for your answer, i tried using ggplot() and what you
suggested to me but for some reason I can't explain it does not
recognise the input of the second geom_errorbar() or geom_hline(). I
tried to do it by putting all of my vectors into one data.frame by
putting dummy values into x5, y5 and y5b to check if it would work but
I am no more successful. I cannot see any typo so I really dont know
why there is an unexpected input in the sequence.

Brandon Hurr

unread,
Nov 16, 2011, 8:39:48 AM11/16/11
to Vincent Margerin, ggplot2

x4 <- c(330, 835, 317, 396, 486, 391)
y4 <- c(4098.11, 4099.14, 4098.16, 4098.35, 4099.30, 4096.73)
y4b <- c(0.47, 0.29, 0.46, 0.83, 0.72, 0.87)
y4min<-y4-y4b
y4max<-y4+y4b
dat.tgg817a <- data.frame(x4, y4, y4b,y4max, y4min)

t4 <- sum(y4/y4b)/(sum(1/y4b))

x5 <- c(721, 257, 204, 271)
y5 <- c(4096.33, 4095.51, 4095.29, 4095.19)
y5b <- c(0.10, 0.15, 0.18, 0.22)
y5min<-y5-y5b
y5max<-y5+y5b
dat.tgg817b <- data.frame(x5, y5, y5b, y5max, y5min)

t5 <- sum(y5/y5b)/(sum(1/y5b))

ggplot()+
geom_point(data=dat.tgg817a, aes(x=x4, y=y4))+
geom_errorbar(data=dat.tgg817a, aes(x=x4, y=y4, ymin= y4min, ymax= y4max), width = 3, colour='NavyBlue')+
geom_hline(aes(yintercept=t4), colour ='NavyBlue', size=2)+
geom_point(data=dat.tgg817b, aes(x=x5, y=y5), colour='DarkRed')+
geom_hline(aes(yintercept=t5), colour ='DarkRed', size=2)+
geom_errorbar(data=dat.tgg817b, aes(x=x5, y=y5, ymin= y5min, ymax= y5max), width = 3, colour='DarkRed')+
coord_cartesian(ylim=c(4094,4101), wise=TRUE)



If you copy and past the above it should work.  Did it not work the first time due to line breaks being mucked in gmail? 

If the above does not work, could you please paste the error into your reply?

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Vincent Margerin

unread,
Nov 16, 2011, 9:02:54 AM11/16/11
to ggplot2
ok so i copy/past-ed it and it does work, the first time I had

ggplot() + geom_point(data=dat.tgg817a, aes(x=x4,y=y4)) +
geom_errorbar(data=dat.tgg817a, aes(x=x4, y=y4, ymin= y4min,ymax=
y4max), width=5, colour='NavyBlue') + geom_hline(aes(yintercept=t4),
colour='NavyBlue', size=1) + geom_point(data=dat.tgg817b, aes(x=x5,
y=y5), colour='DarkRed') +geom_errorbar(data=dat.tgg817b, aes(x=x5,
y=y5, ymin=y5min, ymax=y5max), width=3, colour='DarkRed')
+ geom_hline(aes(yintercept=t5), colour ='DarkRed',size=1) +
coord_cartesian(ylim= c(4094,4101), wise=TRUE)

without line breaking. After seeing that copy/past-ing from your last
answer worked I broke the line and found the problem:

Error: unexpected input in:
"geom_point(data=dat.tgg817b, aes(x=x5, y=y5), colour='DarkRed') +
geom_errorbar(data=dat.tgg817b, aes(x=x5, y=y5, ymin=y5min,
ymax=y5max), width=3, colour='DarkRed') +¬"

The actual weird sign at the end was "encrypted" in the line, could
not see it, even erasing the space at after the + sign did not work.

Thanks so much for that, it's terribly helpful !!
> On Wed, Nov 16, 2011 at 13:31, Vincent Margerin <vincenzo.marge...@gmail.com
Reply all
Reply to author
Forward
0 new messages