[R] Barplot not showing all labels

William Dunlap wdunlap at tibco.com
Tue Jan 14 00:01:38 CET 2014


If the problem is that not all y-axis labels fit on the horizontal barplot
with the default settings, you can rotate then to horizontal with las=1
and reduce their size with cex.names=0.5 to avoid overlap, as in
   barplot(structure(1:50, names=state.name), horiz=TRUE,las=1, cex.names=0.5)
or, using plotrix::staxlab, as Jim Lemon suggested, to spread them out
in alternating columns
   staxlab(2,at=barpos,labels=URLcodes[noUS],nlines=2,las=1,cex=0.5)

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Jim Lemon
> Sent: Monday, January 13, 2014 1:43 PM
> To: Jeff Johnson
> Cc: r-help at r-project.org
> Subject: Re: [R] Barplot not showing all labels
> 
> On 01/14/2014 06:15 AM, Jeff Johnson wrote:
> > I have a table that consists of the following country codes and frequencies:
> >     AE AN AR AT AU BB BD BE BH BM BN BO BR BS CA CH CM CN CO CR CY DE DK DO
> > EC ES
> >   0  3  0  2  1 31  4  1  1  1 45  1  1  4  5 86  3  1  8  1  2  1  8  2  1
> >   2  4
> > FI FR GB GR GU HK ID IE IL IN IO IT JM JP KH KR KY LU LV MO MX MY NG NL NO
> > NZ PA
> >   2  4 35  3  3 14  3  5  2  5  1  2  1 15  1 11  2  2  1  1 23  7  1  6  1
> >   3  1
> > PE PG PH PR PT RO RU SA SE SG TC TH TT TW TZ US ZA
> >   2  1  1  8  1  1  1  1  1 18  1  1  2 11  1  0  3
> >
> > I am executing:
> > non_us<- table(subset(mydf, (COUNTRY %in% validcountries)&  COUNTRY !=
> > "US", select = COUNTRY))
> >
> > barplot(non_us,horiz=TRUE,xlab = "Count", ylab = "Country",main= "Count of
> > Non-US Records by Country",col="red")
> >
> > It creates the attached image (I hope images come through on email). Notice
> > that it is not displaying all of the country codes. It shows bars for each
> > country, but only 6 are appearing.
> >
> > Does anyone have a suggestion? I'm open to using qplot, ggplot or ggplot2
> > (and have tried that), but I want a bar (horizontal) chart not a column
> > chart.
> >
> Hi Jeff,
> We didn't get the image, and I don't know what "validcountries"
> contains, but this may be close to what you want:
> 
> URLcodes<-c("AE","AN","AR","AT","AU","BB","BD","BE","BH","BM",
>   "BN","BO","BR",
>   "BS","CA","CH","CM","CN","CO","CR","CY","DE","DK","DO","EC",
>   "ES,FI","FR",
>   "GB","GR","GU","HK","ID","IE","IL","IN","IO","IT","JM","JP",
>   "KH","KR","KY",
>   "LU","LV","MO","MX","MY","NG","NL","NO","NZ","PA","PE","PG",
>   "PH","PR","PT",
>   "RO","RU","SA","SE","SG","TC","TH","TT","TW","TZ","US","ZA")
> values<-c(0,3,0,2,1,31,4,1,1,1,45,1,1,4,5,86,3,1,8,1,2,1,8,2,12,4,
>   2,4,35,3,3,14,3,5,2,5,1,2,1,15,1,11,2,2,1,1,23,7,1,6,1,3,12,
>   1,1,8,1,1,1,1,1,18,1,1,2,11,1,0,3)
> noUS<-URLcodes!="US"
> barpos<-barplot(values[noUS],horiz=TRUE)
> require(plotrix)
> staxlab(2,at=barpos,labels=URLcodes[noUS],nlines=3)
> 
> Jim
> 
> ______________________________________________
> 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.




More information about the R-help mailing list