CodeGuy
CodeGuy

Reputation: 28905

Plotting data from a tapply output - R script

I am using tapply to get averages for certain values, and I get output that looks like:

 5        6        7        8        
 3066.892 1804.489 1754.675 1695.448

and when I plot it, I get a plot where the x axis has "index" that's 0 to 3 rather than the actual values 5 through 8. how can I plot this output of tapply so that the axis labels are correct?

Upvotes: 3

Views: 7543

Answers (1)

thelatemail
thelatemail

Reputation: 93908

You should be able to use the names() function to get the values, eg:

names(tapply(values,index,mean))
[1] "5" "6" "7" "8"

xnames <- names(tapply(values,index,mean))

That you can make a plot with no x-axis and add your new data labels

plot(tapply(values,index,mean),xaxt="n")
axis(1, at=1:length(xnames), labels=xnames)

I think that will work just right.

Upvotes: 7

Related Questions