0

I have created a table from my dataframe(no.out) using only 1 variable(DX_2_CD).

> counts <- table(no.out$DX_2_CD)
> counts

             Blood CirculatorySystems         Congenital          Digestive      Genitourinary 
                 7                133                  0                  7                 35 
        Illdefined           Immunity         Infectious             Injury             Mental 
               126                 98                  0                 84                  7 
          Muscular          Neoplasms            Nervous          Perinatal          Pregnancy 
               119                  7                  0                  0                  7 
       Respiratory              Sense               Skin 
                63                 35                 63 

Now, I want to re-arrange counts in ascending order, such that the lowest value is first and the highest value is the last. If there are two values which are the same, it does not matter which comes first.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Madhumita
  • 489
  • 1
  • 7
  • 15

2 Answers2

3

I think the simplest solution is using order():

ordered_counts <- counts[order(counts)]

or, even better, sort():

ordered_counts <- sort(counts)
  • I was trying to draw a barplot with counts. I did try : barplot(sort(counts)). This is working . But, the actual problem is, 1. some of the labels of the bars are not appearing, for this I tried text(cex=.6, x=x-.50, y=-1.25, lbls, xpd=TRUE, srt=45) this is making the labels in actual order of counts, not the sorted order ; 2. I want the bars to be of different colours. Have been searching for the solution but did not find one. – Madhumita Nov 30 '14 at 15:42
  • I guess you should start a new question – Francisco Rodríguez Algarra Nov 30 '14 at 20:19
0

Ok, Now I have managed to get the bars with different colours, using :

    x<-barplot(sort(counts),col=rainbow(length(sort(counts))))#, main="DX_2_CD Distribution", 
           lablist <- as.vector(names(sort(counts)))      
text(cex=1, x=x-.50, y=-1.25, lablist, xpd=TRUE, srt=45)

This solves both the problem.

BUT, the original x-axis labels also remains in addition to the 45 degree slanted labels. How do I get rid of them?

I am not being able to attach the screen-shot since I do not have enough reputation

Madhumita
  • 489
  • 1
  • 7
  • 15
  • You never mentioned anything about a barplot in your question – Rich Scriven Nov 30 '14 at 16:17
  • My apologies, I have corrected the Tag and the Question description. Need solution for the overlapping of the sorted x-label(horizontal) and the sorted x-label(45 degree angled). Urgent help required. – Madhumita Nov 30 '14 at 17:13