Aligning labels to bars in barplot

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

Aligning labels to bars in barplot

David Arnold
Hi,

Consider:

x=c(2,4,5,10,13,73)
names(x)=c("American\nIndian",
           "No\ncategory\nlisted",
           "Hispanic",
           "African-\nAmerican",
           "Asian",
           "White")
barplot(x,main="Undergraduate Enrollment by Race",
        xlab="Race",
        ylab="Percent",
        col=rainbow(6))

The labels at the bottom of the bars are aligned at the bottom of the last word in each bar label. How can I code it so that the labels are aligned at the top of the first word in each bar label?

Thanks,

David.
Reply | Threaded
Open this post in threaded view
|

Re: Aligning labels to bars in barplot

Jim Lemon
On 01/16/2013 07:48 PM, David Arnold wrote:

> Hi,
>
> Consider:
>
> x=c(2,4,5,10,13,73)
> names(x)=c("American\nIndian",
>             "No\ncategory\nlisted",
>             "Hispanic",
>             "African-\nAmerican",
>             "Asian",
>             "White")
> barplot(x,main="Undergraduate Enrollment by Race",
>          xlab="Race",
>          ylab="Percent",
>          col=rainbow(6))
>
> The labels at the bottom of the bars are aligned at the bottom of the last
> word in each bar label. How can I code it so that the labels are aligned at
> the top of the first word in each bar label?
>
Hi David,
Try this:

barpos<-barplot(x,
        main="Undergraduate Enrollment by Race",
        names.arg=rep("",6),
         xlab="Race",
         ylab="Percent",
         col=rainbow(6))
axis(1,at=barpos,labels=names(x),padj=1)

Jim

______________________________________________
[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
|

Re: Aligning labels to bars in barplot

David Arnold
Nice, worked very well. But because of the realignment, I now need to lower by xlab a bit. Any suggestions?

David.
Reply | Threaded
Open this post in threaded view
|

Re: Aligning labels to bars in barplot

Jim Lemon
On 01/17/2013 04:51 AM, David Arnold wrote:
> Nice, worked very well. But because of the realignment, I now need to lower
> by xlab a bit. Any suggestions?
>
Hi David,
This should give you an idea of how to do it:

par(mar=c(6,4,4,2))
plot(1:10,xlab="")
mtext("Index",side=1,line=4)

Jim

______________________________________________
[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.