Hi, Can anyone please help me with how I could add labels with the value for each bar in a barchart? (similar to how data labels can be added in Excel) I have done a lot of searching but havent been lucky. Thanks, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Data-Labels-in-a-barchart-Lattice-or-otherwise-tp2278027p2278027.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Jul-05 14:20 UTC
[R] Data Labels in a barchart (Lattice or otherwise)
On Jul 4, 2010, at 11:43 PM, RaoulD wrote:> > Hi, > > Can anyone please help me with how I could add labels with the value > for > each bar in a barchart? (similar to how data labels can be added in > Excel) I > have done a lot of searching but havent been lucky.This is generally pretty easy with text() at least if you are using base graphics. If it is not clear after reading the help page then post an examply with whatever barchart function you have chosen to use. If it's the lattice barchart there is an ltext example immediately before the barchart example that quickly can be grafted into the barchart code. David Winsemius, MD West Hartford, CT
Thank You David. Yes, I am using the lattice barchart and have managed to add data labels, however, they tend to be on the tip of each bar and are difficult to read as they are partially on the bar. Any help would be greatly appreciated. This is the code I am using: levels(PR_SUMMARY$Bucket)=c("0-3 months","3-9 months","9-15 months","15-18 months") barchart(PrimaryReason ~ cInteractions| Bucket + Type, data = PR_SUMMARY, layout = c(4, 2),col="lightgreen",main="COMPARISON - PRIMARY REASON", sub="L & R",xlab="Number of Customers",ylab="Primary Reasons", auto.key = list(title = "COMPARISON - PRIMARY REASON",columns=2,points = FALSE, rectangles = TRUE,space= "right" ),scales = list(x = list(abbreviate=TRUE,minlength=5,rot=45)), panel = function(x,y,subscripts,groups,...){ panel.barchart(x,y,...) ltext(x,y,label=round(PR_SUMMARY$cInteractions,1), cex=.99,rot=45) border="transparent"}) I dont really understand the "ltext" part and found it with some other code, but it works. Thanks again, Raoul -- View this message in context: http://r.789695.n4.nabble.com/Data-Labels-in-a-barchart-Lattice-or-otherwise-tp2278027p2278646.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2010-Jul-05 22:37 UTC
[R] Data Labels in a barchart (Lattice or otherwise)
On Jul 5, 2010, at 1:14 PM, RaoulD wrote:> > Thank You David. Yes, I am using the lattice barchart and have > managed to add > data labels, however, they tend to be on the tip of each bar and are > difficult to read as they are partially on the bar. Any help would be > greatly appreciated. > > This is the code I am using: > levels(PR_SUMMARY$Bucket)=c("0-3 months","3-9 months","9-15 > months","15-18 > months") > barchart(PrimaryReason ~ cInteractions| Bucket + Type, data = > PR_SUMMARY, > layout = c(4, 2),col="lightgreen",main="COMPARISON - PRIMARY REASON", > sub="L & R",xlab="Number of Customers",ylab="Primary > Reasons", > auto.key = list(title = "COMPARISON - PRIMARY > REASON",columns=2,points = FALSE, rectangles = TRUE,space= > "right" > ),scales = list(x = list(abbreviate=TRUE,minlength=5,rot=45)), > panel = function(x,y,subscripts,groups,...){ > panel.barchart(x,y,...) > ltext(x,y,label=round(PR_SUMMARY$cInteractions,1), > cex=.99,rot=45)# if you add or subtract a small amount from "y" in the prior line it will move the labels up or down.> border="transparent"}) > > I dont really understand the "ltext" part and found it with some > other code, > but it works. > > Thanks again, > Raoul > --David Winsemius, MD West Hartford, CT
> fortune(197)If anything, there should be a Law: Thou Shalt Not Even Think Of Producing A Graph That Looks Like Anything From A Spreadsheet. -- Ted Harding (in a discussion about producing graphics) R-help (August 2007) Also read the discussion started with: http://tolstoy.newcastle.edu.au/R/e2/help/07/08/22858.html -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of RaoulD > Sent: Sunday, July 04, 2010 9:44 PM > To: r-help at r-project.org > Subject: [R] Data Labels in a barchart (Lattice or otherwise) > > > Hi, > > Can anyone please help me with how I could add labels with the value > for > each bar in a barchart? (similar to how data labels can be added in > Excel) I > have done a lot of searching but havent been lucky. > > Thanks, > Raoul > -- > View this message in context: http://r.789695.n4.nabble.com/Data- > Labels-in-a-barchart-Lattice-or-otherwise-tp2278027p2278027.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.
Seemingly Similar Threads
- panel function with barchart (lattice)
- Calculating Summaries for each level of a Categorical variable
- Barchart in lattice - wrong order of groups, data labels on top of each other, and a legend question
- Coding a new variable based on criteria in a dataset
- Help with panel.text in Lattice - Putting labels for co-oridnates in a plot