rbar-chart

R barplot: Using a subset of colnames in the names.arg


I am trying to create a bar chart containing only the top 5 correlations found between my outcome of interest and a large number of predictor variables.

I'm trying it out with just 20 to get the logic correct.

I have a 1 by 20 numeric vector containing correlation coefficients produced by the cor() function. There are positive and negative values. The vector has column names which correspond to the names of the predictor variables. This is the structure I'm starting with:

set.seed(10)
mydata <- runif(20,-.5,.5)
mydata <- t(mydata)
colnames(mydata) <- letters[1:20]

The 5 strongest correlations are "t", "q", "m", "e" and "f" where "t" is positive and the other 4 are negative. I am generating the bar plot of the 5 strongest correlations by ordering the squared values.

barplot(mydata[order(-mydata^2)][1:5], horiz=TRUE,names.arg=HELP, xlim= c(-1,1), las=1)

The values for the 5 bars look correct, but I haven't been able to get the labels to show correctly. I've tried a variety of things for names.arg=HELP with no luck. I've tried

names.args = colnames(mydata)
names.args = colnames(mydata[order(-mydata^2)]
names.args = colnames(mydata[order(-mydata^2)[1:5]])
names.args = colnames(order(-mydata^2)[1:5])

I just want a bar chart with the correct 5 labels.

I need to repeat this process on 13,000+ predictor variables and 30+ outcomes once it is figured out so the simpler the better.


Solution

  • This works:

    barplot(mydata[order(-mydata^2)][1:5], horiz=TRUE,
            names.arg=colnames(mydata)[order(-mydata^2)][1:5], xlim= c(-1,1), las=1)