Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:29:12+00:00 2026-05-28T06:29:12+00:00

I would like to label each of the boxes in a barplot by their

  • 0

I would like to label each of the boxes in a barplot by their size(i.e number of observations in dataframe which are in the group).

e.g If the first variable has 3 levels and the second variable has 4 levels, I would like 12 labels.

(Also, is it possible to control the size or position of these labels)

Thank you for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:29:12+00:00Added an answer on May 28, 2026 at 6:29 am

    Here’s one way to do it, using the data VADeaths as an example (it will be in your R workspace by default, or if not, use library(datasets)).

    bar <- barplot(VADeaths)
    text(rep(bar,each=nrow(VADeaths)), as.vector(apply(VADeaths,2,cumsum)), 
         labels=as.vector(apply(VADeaths,2,cumsum)),pos=3)
    

    It looks like this:

    enter image description here

    To modify the size of the font you can use text(...,cex=2) to make things twice the size they were, e.g.

    Now, let’s explain this code so you know how to do it yourself!

    First, let’s look at VADeaths: it’s a tally of deaths in each age group by category:

    > VADeaths
          Rural Male Rural Female Urban Male Urban Female
    50-54       11.7          8.7       15.4          8.4
    55-59       18.1         11.7       24.3         13.6
    60-64       26.9         20.3       37.0         19.3
    65-69       41.0         30.9       54.6         35.1
    70-74       66.0         54.3       71.1         50.0
    

    Now, to do the text on the barplot, we basically draw the barplot, and then draw the text on top using R command text (see ?text).

    text requires x,y coordinates and corresponding pieces of text to draw on the bar plot. We will give it the coordinates of each line in the bar plot to draw the text on.

    To do this, see the “Value” section ?barplot. This function not only plots your bar plot, but returns the x coordinate of each bar. score!

    > bar <- barplot(VADeaths)
    > bar
    [1] 0.7 1.9 3.1 4.3
    

    Now all we need is y coordinates to go with our x coordinates.

    Well, a stacked bar plot just tallies up the frequencies in VADeaths as you go along.
    For example, in the ‘Rural Male’ group, the first line is drawn at 11.7, and the second is drawn at 11.7 + 18.1 = 29.8, the third at 11.7 + 18.1 + 26.9 = 56.7, and so on (see the values in VADeaths).

    So, our y coordinates need to be cumulative sums going down the columns.

    To calculate these for each column, we can use cumsum. For example

    > cumsum(c(1,2,3,4,5))
    [1]  1  3  6 10 15
    

    Since we want to do this for each column in VADeaths, we have to use the function apply.

    > apply(VADeaths,2,cumsum)
          Rural Male Rural Female Urban Male Urban Female
    50-54       11.7          8.7       15.4          8.4
    55-59       29.8         20.4       39.7         22.0
    60-64       56.7         40.7       76.7         41.3
    65-69       97.7         71.6      131.3         76.4
    70-74      163.7        125.9      202.4        126.4
    

    apply(VADeaths,2,cumsum) means: “For each column in VADeaths, calculate the cumsum of that”.
    This gives us the y values for each line of the bar plot.

    Let’s save these yvalues for further use:

    > yvals <- as.vector(apply(VADeaths,2,cumsum))
    

    The reason I use as.vector is just to flatten the matrix into a vector of values — it makes the plotting easier.

    One last thing — my x values (that I stored in bar) only have one value per bar, but I need to expand it out so there’s one x value per line on each bar. To do this:

    > xvals <- rep(bar,each=nrow(VADeaths))
    

    This turns my previous x1,x2,x3,x4 into x1,x1,x1,x1,x1, x2,x2,x2,x2,x2, ..., x4,x4,x4,x4,x4.
    Now my xvals match my yvals.

    After this it’s simply a case of using text.

    > text( xvals, yvals, labels=yvals, pos=3 )
    

    The labels arguments tells text what text to put at the x/y positions.
    The pos=3 means “draw each bit of text just above my specified x/y value”. Otherwise, the numbers would be drawn over the lines of the barplot which would be hard to read.

    Now, there are many options for customising the position and size of text, and I suggest you read ?text to see them.

    All this code condenses down to the two-liner I gave at the beginning of the answer, but this version might be a little more understandable:

    bar <- barplot(VADeaths)
    xvals <- rep(bar,each=nrow(VADeaths))
    yvals <- as.vector(apply(VADeaths,2,cumsum))
    text( xvals, yvals, labels=yvals, pos=3 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add a label for each bracket made. I am getting
I would like a button to change a label between being visible and not
I would like to create simple objects at runtime (textbox, label, etc) and add
I've got a multiline textBox that I would like to have a label on
I have 2 select boxes on a page with a variable number of options
I am dynamically adding labels to a form and I would like to set
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.