[R] ggplot2 lost label

David Winsemius dwinsemius at comcast.net
Sun Sep 18 21:27:09 CEST 2016


> On Sep 18, 2016, at 11:05 AM, Troels Ring <tring at gvdnet.dk> wrote:
> 
> dear friends - I have a problem in ggplot2 which I hope you can help me understand and solve.
> 
> Running windows 7, R 3.2.1
> 
> grp <- gl(3,4,12)
> trt <- gl(4,1,12)
> num <- c(11,1,0,0,6,2,0,0,1,9,10,10)
> MM <- data.frame(grp=grp,num=num,trt=trt)
> levels(MM$grp)<- c("anest1","anest2","anest3")
> levels(MM$trt)<- c("drug1","drug2","drug3","drug4")
> MM
> 
> ggplot(data=MM)+ geom_bar(mapping=aes(x=grp,y=num),fill=trt,stat="identity") +
> theme(legend.position="top") + ylab("Number")+xlab("")
> 
> How do Imake ggplot make a label? - I tried to annotate("text",1,5,"drug1") returning
> 
> mapping: x = x, xmin = xmin, y = y
> geom_text: na.rm = FALSE
> stat_identity: na.rm = FALSE
> position_identity

I wasn't sure what that "return" was signifying. Trying to add that annotate call to a ggplot object threw an error:

Error: geom_text requires the following missing aesthetics: label

Fixing that produced n apparently unlabelled plot except I thin realized you were trying to annotate in the same color ( "black") as that section of bar. So this succeeds:

 ggplot(data=MM)+ geom_bar(mapping=aes(x=grp,y=num),fill=trt,stat="identity") +
         theme(legend.position="top") + ylab("Number")+ xlab("")+  
         annotate("text",1,5,label="drug1", col="orange")

> 
> so that was not as I thought. How do I get the label directly

I'm not a very accomplished ggplotter so there may be "direct" methods. I remember a "directlabels" package that does some cool stuff but I'm not well-versed in it eiehter..


> - or annotate if that is the only possibility?
> 
> Best wishes
> 
> Troels Ring
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list