Quantcast

ggplot2's geom_errorbar legend

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ggplot2's geom_errorbar legend

bravegag
Hello,

I create a simple ggplot that only shows a straight line. I then add three datasets of CI using the geom_errorbar function. The problem is that I can't find any way to have the legend showing up ... I need to show what each  color of the CIs corresponds to i.e. which method.

Can anyone advice please?
TIA,
Best regards,
Giovanni

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2's geom_errorbar legend

Ista Zahn
Hi Giovanni,
A reproducible example would help. Also, since I think this will be
tricky, it might be a good idea to post it to the ggplot2 mailing list
 (you can register at http://had.co.nz/ggplot2/ ).

Best,
Ista

On Sat, May 1, 2010 at 1:34 PM, Giovanni Azua <[hidden email]> wrote:

> Hello,
>
> I create a simple ggplot that only shows a straight line. I then add three datasets of CI using the geom_errorbar function. The problem is that I can't find any way to have the legend showing up ... I need to show what each  color of the CIs corresponds to i.e. which method.
>
> Can anyone advice please?
> TIA,
> Best regards,
> Giovanni
>
> ______________________________________________
> [hidden email] 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.
>



--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2's geom_errorbar legend

bravegag
Hello Ista,

On May 1, 2010, at 8:37 PM, Ista Zahn wrote:
> Hi Giovanni,
> A reproducible example would help. Also, since I think this will be
> tricky, it might be a good idea to post it to the ggplot2 mailing list
> (you can register at http://had.co.nz/ggplot2/ ).
>
> Best,
> Ista

First, thank you so much for showing me the way ... the plots generated from ggplot2 are super nice!

Please find below a self contained reproducible example nearly exactly as the ones I need to produce.

Thanks in advance,
Best regards,
Giovanni

rm(list=ls())                                              # clear workspace
library(ggplot2)                                           # use ggplot2 library

methodlabels <- c("Classic", "Own Bootstrap",              # prepare plot labels
        "R Bootstrap")              
errortypelabels <- c("'Normal'", "'Student-t'",            # prepare plot labels
        "'Exponential'")
betalabels <- c("'Beta_1'", "'Beta_2'", "'Beta_3'")        # prepare plot labels
betas <- c(1, -2, 3)                                       # my real parameters
set.seed(11)
S <- 100
B <- 50
P <- length(betas)
classiccis <- owncis <- rbootcis <- array(data=0,dim=c(S,1,3,2))
for (p in 1:P) {
        classiccis[,1,p,1] <- rnorm(100,mean=betas[p]-1.0,sd=0.5)
        classiccis[,1,p,2] <- rnorm(100,mean=betas[p]+1.0,sd=0.5)
        owncis[,1,p,1] <- rnorm(100,mean=betas[p]-1.0,sd=0.5)
        owncis[,1,p,2] <- rnorm(100,mean=betas[p]+1.0,sd=0.5)
        rbootcis[,1,p,1] <- rnorm(100,mean=betas[p]-1.0,sd=0.5)
        rbootcis[,1,p,2] <- rnorm(100,mean=betas[p]+1.0,sd=0.5)
}

##
## Function that generates the ggplot of the CI for a given parameter
## and error type
##
ciplot <- function() {
        x <- 1:S
        y <- rep(betas[p],S)
        data <- data.frame(x,y)
        classiclimits <- aes(x=x[1:100],ymax = classiccis[1:100,e,p,1],
                ymin=classiccis[1:100,e,p,2])
        ownlimits <- aes(x=x[1:100]+0.4,ymax = owncis[1:100,e,p,1],
                ymin=owncis[1:100,e,p,2])
        rbootlimits <- aes(x=x[1:100]+0.8,ymax = rbootcis[1:100,e,p,1],
                ymin=rbootcis[1:100,e,p,2])
        g1 <- ggplot(data, aes(x=x, y=y))
        g1 + geom_errorbar(classiclimits, colour="red") + geom_errorbar(
             ownlimits, colour="green") + geom_errorbar(rbootlimits,
             colour="blue") + geom_hline(yintercept = betas[p]) + xlab(
             "Simulation") + ylab(betalabels[p]) + opts(title = paste("CI for",
             errortypelabels[e],"error, ",betalabels[p],",",S/5,
             "simulations and ",B,"bootstrapped samples"))
}

##
## Execute one by one ... note they have to be global
## otherwise seems that ggplot won't find them.
##
## p - means parameter index
## e - means error type index
##
p=1;e=1;ciplot()
p=1;e=2;ciplot()
p=1;e=3;ciplot()
p=2;e=1;ciplot()
p=2;e=2;ciplot()
p=2;e=3;ciplot()
p=3;e=1;ciplot()
p=3;e=2;ciplot()
p=3;e=3;ciplot()
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2's geom_errorbar legend

hadley wickham
Hi Giovanni,

The basic idea is:

classiclimits <- aes(x=x[1:100],ymax = classiccis[1:100,e,p,
 ymin=classiccis[1:100,e,p,2], colour = "classic")
ownlimits <- aes(x=x[1:100]+0.4,ymax = owncis[1:100,e,p,1],
  ymin=owncis[1:100,e,p,2], colour = "own")
rbootlimits <- aes(x=x[1:100]+0.8,ymax = rbootcis[1:100,e,p,1],
  ymin=rbootcis[1:100,e,p,2], colour = "rboot")

And then remove the explicit colours elsewhere in your code.

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
[hidden email] 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.
Loading...