Ahson
Ahson

Reputation: 23

Using factor levels as Labels in barplot

I asked this question in different format before and had answer which worked partially. I have a dataframe 'count_df2' which has two variables and one factor 'labels' with 12 levels.

TYPES_OF_COMPANIES                  COUNT_OF_COMPANIES                           LABELS
AIM-Listed                                 876                                     AIM
Charitable-organisation                     82                                   Charity
Industrial-Provident                        50                                     I-P
Limited-Partnership                          2                                     L-P
Limited by Guarantee                       277                                     L-G
Limited Liability Partnership              167                                     LLP
Listed-LSE                                1131                                   L-LSE
Not-Companies-Act                           75                                     NCA
Private Limited Company                   1163                                     PLC
Public-Unlisted                            418                                    P-UL
Royal-Charter                                5                                      RC
Unlimited                                  111                                      UL

I want to develop a bar plot with factor labels values as the labels for the bars. I can get the bar plot using

barplot(count_df2$COUNT_OF_COMPANIES,xlab='TYPE_OF_COMPANIES',
      ylab='COUNT_OF_COMPANIES',
      main='Number of Different Types of Companies in the database') 

but I am struggling to get the labels in without using the names.arg

Can anyone help please? Thanks in advance

Upvotes: 0

Views: 255

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174596

Are you looking for

barplot(setNames(count_df2[[2]], count_df2[[3]]),
        xlab = 'TYPE_OF_COMPANIES',
        ylab = 'COUNT_OF_COMPANIES',
        main = 'Number of Different Types of Companies in the database') 

enter image description here


Data

count_df2 <- structure(list(TYPES_OF_COMPANIES = c("AIM-Listed", "Charitable-organisation", 
"Industrial-Provident", "Limited-Partnership", "Limited by Guarantee", 
"Limited Liability Partnership", "Listed-LSE", "Not-Companies-Act", 
"Private Limited Company ", "Public-Unlisted", "Royal-Charter", 
"Unlimited"), COUNT_OF_COMPANIES = c(876L, 82L, 50L, 2L, 277L, 
167L, 1131L, 75L, 1163L, 418L, 5L, 111L), LABELS = c("AIM", "Charity", 
"I-P", "L-P", "L-G", "LLP", "L-LSE", "NCA", "PLC", "P-UL", "RC", 
"UL")), class = "data.frame", row.names = c(NA, -12L))

count_df2
#>               TYPES_OF_COMPANIES COUNT_OF_COMPANIES  LABELS
#> 1                     AIM-Listed                876     AIM
#> 2        Charitable-organisation                 82 Charity
#> 3           Industrial-Provident                 50     I-P
#> 4            Limited-Partnership                  2     L-P
#> 5           Limited by Guarantee                277     L-G
#> 6  Limited Liability Partnership                167     LLP
#> 7                     Listed-LSE               1131   L-LSE
#> 8              Not-Companies-Act                 75     NCA
#> 9       Private Limited Company                1163     PLC
#> 10               Public-Unlisted                418    P-UL
#> 11                 Royal-Charter                  5      RC
#> 12                     Unlimited                111      UL

Upvotes: 1

Related Questions