[R] how to use 'points' function to plot two curves with errbar
David Winsemius
dwinsemius at comcast.net
Tue Jun 15 15:48:06 CEST 2010
In context below:
On Jun 15, 2010, at 4:20 AM, Yogesh Tiwari wrote:
> Dear John,
> Great thanks,
> I would appreciate if any expert can throw more light on it.
>
> Thanks in advance,
>
> Best Regards,
> Yogesh
>
> On 6/14/10, John Kane <jrkrideau at yahoo.ca> wrote:
>> Hi Yogesh,
>>
>> I think you accidentelly replied only to me and not to the mailing
>> list.
>> I'd suggest that you copy your post to me to the list to get input
>> from some
>> of the
>> real experts.
>>
>> Currently I don't see any provision for differently coloured error
>> bars in
>> the the help page but I easilly could be missing something.
>>
>> Also you have a fairly busy plot there, you might want to consider
>> having
>> the cuves in separate panels. Perhaps something like this: you can
>> probably
>> do this much better in lattice or ggplot but can give you an idea.
>> #=========================================================
>> x=1:7
>> y=6:12
>>
>> uperror=y+2
>> downerror=y-2
>>
>> opar <- par(mfrow=c(2,2))
>> errbar(x,y, uperror, downerror,type="l")
>> errbar(x,y, uperror, downerror, type="l")
>> errbar(x,y, uperror, downerror, type="l")
>> errbar(x,y, uperror, downerror, type="l")
>> par(opar)
>> #==========================================================
>> Also we really should have some sample data., It have to be the
>> entire data
>> set or even the real data as
>> long as it has the same format and layout.
>>
>> Have a look at ?dput for way to include some easily useable data.
>>
>> Sorrry I'm not more help.
>>
>> --- On Sun, 6/13/10, Yogesh Tiwari <yogesh.mpi at googlemail.com> wrote:
>>
>> From: Yogesh Tiwari <yogesh.mpi at googlemail.com>
>> Subject: Re: [R] how to use 'points' function to plot two curves
>> with errbar
>> To: "John Kane" <jrkrideau at yahoo.ca>
>> Received: Sunday, June 13, 2010, 11:21 PM
>>
>> Dear John,
>> Thanks,
>> I am using errbar in the package Hmisc,
>>
>> par(new=TRUE), worked between two errbar ploting commands but the
>> new
>> problem is now 'color of arrow (bars)', its always black for all
>> the four
>> curves whereas I have specified separate color for each curve. I
>> want to
>> have similar color of arrow as curve. My command which plots four
>> curves
>> and respective errbar subsequently the generated plot is attached
>> herewith.
>> Kindly help.
>>
>>
>>> x.val <- as.integer(names(co2mean))
>>> errbar(x.val, co2mean, co2mean + co2sd, co2mean - co2sd, xaxt='n',
>>> col="black", xlab=NA,ylab=NA)# obs error bar
>>> lines(x.val, co2mean, col="black", lwd=2)
>>
>>
>>> par(new=TRUE)
>>
>>> errbar(x.val, co2tm3.month.mean, co2tm3.month.mean + co2sd.tm3,
>>> co2tm3.month.mean - co2sd.tm3, xaxt='n', yaxt='n', col="red",
>>> xlab=NA,ylab=NA)# tm3 error bar
>>
>>> lines(x.val, co2tm3.month.mean, col="red", lwd=2)
>>
>>> par(new=TRUE)
>>
>>> errbar(x.val, co2lsce.month.mean, co2lsce.month.mean + co2sd.lsce,
>>> co2lsce.month.mean - co2sd.lsce, xaxt='n',yaxt='n', col="green",
>>> xlab=NA,ylab=NA)# lsce error bar
>>
>>> lines(x.val, co2lsce.month.mean, col="green", lwd=2)
>>
>>> par(new=TRUE)
>>
>>> errbar(x.val, co2patra1.month.mean, co2patra1.month.mean +
>>> co2sd.actm,
>>> co2patra1.month.mean - co2sd.actm, xaxt='n',yaxt='n', col="blue",
>>> xlab=NA,ylab=NA)# actm error bar
>>
>>> lines(x.val, co2patra1.month.mean, col="blue", lwd=2)
I am not sure I fully understand what is being done since I do not
believe you have included the "co2mean" data being used to drive the
plotting function. If all you want is for errbar to make the error
bars colored the same as the points, then it will be necessary to hack
the code, since looking at the code, the segments() functions within
errbar() are not set up to receive the triple dot (optional arguments)
arguments. It is a very simple matter to fix that, however. The
slightly hacked errbar2 function was tested with the second example
from the errbar:
require(rms) # has side-effect of also loading Hmisc
errbar2 <-
function (x, y, yplus, yminus, cap = 0.015, main = NULL, sub =
NULL,
xlab = as.character(substitute(x)), ylab = if (is.factor(x) ||
is.character(x)) "" else as.character(substitute(y)),
add = FALSE, lty = 1, type = "p", ylim = NULL, lwd = 1, pch =
16,
Type = rep(1, length(y)), ...)
{
if (is.null(ylim))
ylim <- range(y[Type == 1], yplus[Type == 1], yminus[Type ==
1], na.rm = TRUE)
if (is.factor(x) || is.character(x)) {
x <- as.character(x)
n <- length(x)
t1 <- Type == 1
t2 <- Type == 2
n1 <- sum(t1)
n2 <- sum(t2)
omai <- par("mai")
mai <- omai
mai[2] <- max(strwidth(x, "inches")) + 0.25 * .R.
par(mai = mai)
on.exit(par(mai = omai))
plot(NA, NA, xlab = ylab, ylab = "", xlim = ylim, ylim = c(1,
n + 1), axes = FALSE, ...)
axis(1)
w <- if (any(t2))
n1 + (1:n2) + 1
else numeric(0)
axis(2, at = c(seq.int(length.out = n1), w), labels = c(x[t1],
x[t2]), las = 1, adj = 1)
points(y[t1], seq.int(length.out = n1), pch = pch, type = type,
...)
segments(yplus[t1], seq.int(length.out = n1), yminus[t1],
seq.int(length.out = n1), lwd = lwd, lty = lty, ...)
if (any(Type == 2)) {
abline(h = n1 + 1, lty = 2, ...)
offset <- mean(y[t1]) - mean(y[t2])
if (min(yminus[t2]) < 0 & max(yplus[t2]) > 0)
lines(c(0, 0) + offset, c(n1 + 1, par("usr")[4]),
lty = 2, ...)
points(y[t2] + offset, w, pch = pch, type = type,
...)
segments(yminus[t2] + offset, w, yplus[t2] + offset,
w, lwd = lwd, lty = lty, ...)
at <- pretty(range(y[t2], yplus[t2], yminus[t2]))
axis(side = 3, at = at + offset, labels = format(round(at,
6)))
}
return(invisible())
}
if (add)
points(x, y, pch = pch, type = type, ...)
else plot(x, y, ylim = ylim, xlab = xlab, ylab = ylab, pch = pch,
type = type, ...)
xcoord <- par()$usr[1:2]
smidge <- cap * (xcoord[2] - xcoord[1])/2
segments(x, yminus, x, yplus, lty = lty, lwd = lwd, ...)
if (par()$xlog) {
xstart <- x * 10^(-smidge)
xend <- x * 10^(smidge)
}
else {
xstart <- x - smidge
xend <- x + smidge
}
segments(xstart, yminus, xend, yminus, lwd = lwd, lty = lty, ...)
segments(xstart, yplus, xend, yplus, lwd = lwd, lty = lty, ...)
return(invisible())
}
errbar2(c('a','b','d','a - b','a - d','b - d'),
c(cla[1],clb[1],cld[1],cla[1]-clb[1],cla[1]-cld[1],clb[1]-
cld[1]),
c(cla[3],clb[3],cld[3],a.b[2],a.d[2],b.d[2]),
c(cla[2],clb[2],cld[2],a.b[1],a.d[1],b.d[1]),
Type=c(1,1,1,2,2,2), xlab='', ylab='', col=1:3)
>>
>>
>>
>> Best regards,
>> Yogesh
>>
>>
>>
>> On Sat, Jun 5, 2010 at 11:36 PM, John Kane <jrkrideau at yahoo.ca>
>> wrote:
>>
>> The first thing we need to know is what "errbar" are you using?
>>
>> There are at least two, on in the Hmisc package and one in sfsmisc.
>>
>>
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html and provide commented,
>> minimal,
>> self-contained,reproducible code.
>>
>>
>> I'm not sure what you are trying to do here but perhaps looking at
>> "new" in
>> ?par might hrlp you.
>>
>> Example:
>> plot(1:10, col="red")
>> par(new=TRUE)
>> plot(10:1, col="blue")
>>
>>
>>
>>
>>
>> --- On Sat, 6/5/10, Yogesh Tiwari <yogesh.mpi at googlemail.com> wrote:
>>
>>> From: Yogesh Tiwari <yogesh.mpi at googlemail.com>
>>
>>> Subject: [R] how to use 'points' function to plot two curves with
>>> errbar
>>> To: r-help at stat.math.ethz.ch
>>> Received: Saturday, June 5, 2010, 10:06 AM
>>
>>
>>
>>
>>> Dear R Users,
>>> I am using R on windows.
>>>
>>> how to use 'points' function to plot two curves with
>>> errbar
>>>
>>> I am doing like:
>>>
>>> x.val <- as.integer(names(co2mean))
>>
>>> errbar(x.val, co2mean, co2mean + co2sd, co2mean - co2sd,
>>> xaxt='n', col=1,
>>> xlab=NA,ylab=NA)# obs error bar
>>> lines(x.val, co2mean, col=1, lwd=2)
>>>
>>> errbar(x.val, co2tm3.month.mean, co2tm3.month.mean +
>>
>>> co2sd.tm3,
>>> co2tm3.month.mean - co2sd.tm3, xaxt='n', col=2,
>>> xlab=NA,ylab=NA)# model
>>> error bar
>>> lines(x.val, co2tm3.month.mean, col=2, lwd=2)
>>>
>>> Above code replaces first error bar and its line with
>>
>>> second. Kindly help,
>>> how to use 'points' function here so both above can get
>>> over ploted
>>>
>>> Thanks,
>>> Regards,
>>> Yogesh
>>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>
>>> and provide commented, minimal, self-contained,
>>> reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list