How do I display frequency in a Barplot at the top


here is he first 20 sample of my dataset
ID = c(1,2,3,4,5,6,7,8,9,10,11,12,13,
Rescan = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)

2 = rescan. These are prostate cancer patients, 2 determines if needs rescan. 1 = no rescan.

I can plot a barchart using this code
barplot(table(prostate_cleaned$Rescan), col= c("green", "red"), ylab='Number of Patients')

However, what I want, is the frequency (i.e. the actual value of number of patients that are either in '1' or '2') diplayed at the top of each bar.

I have used the text() fucntion..but it doesnt work. Can someone please help. Thanks

why not share that code ?

Sure thing,

named bar plot as 'trex'

trex <- barplot(table(prostate_cleaned$Rescan), col= c("green", "red"), ylab='Number of Patients')
text(trex, 0, round(prostate_cleaned$Rescan, 1),cex=1,pos=3)

There is clearly something wrong with all I get is '1' and '2' jumbled toeghet together on both charts, where I want the actual numbers (i.e. frequencies of patients with 1)

so for the text labels you are trying to pass rounded number (although these are counts so you wouldnt expect fractions to arise...)

you should check what the result is of

round(prostate_cleaned$Rescan, 1)

which are the labels you are asking to be used

most likely I think you meant to use the same table info you gathered previously

text(trex, 0, table(prostate_cleaned$Rescan),cex=1,pos=3)

many thanks,

That worked..but I actually wanted the values on top of the chart rather than below..what do I substitute '3' with in the pos=3 section of the code?

Thanks again

  trex <- barplot(mytab, col= c("green", "red"),
                  ylab='Number of Patients',
  text(x = trex, 
       y = mytab, 
       labels = mytab,

I refactored to save seeing table(prostate_cleaned$Rescan) repeared 3 times
and I increaded the yaxis limit

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.