0

I want to plot the mean of multiple variables in one graph. I have five variables in a data frame. These variables have been measures on a 5-point scale.


"V1_A","V1_B","V1_C", "V1_D","V1_E"

head(df$V1A)
[1] 4 5 4 5 5 3

I want to depict the mean values of each of these variables from higher to lower values.

I thought of first creating a list of these variables and then plot them using ggplot2. But I am unable to do it

plotData <- df[, c("V1_A","V1_B","V1_C", "V1_D","V1_E")]

ggplot(df, aes(x=plotData, y=mean, fill=plotData))

Anyone can please help me with the R code?

tab
  • 1
  • 3
  • 1
    Hi, and welcome to Stack Overflow! In order for us to help you, please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example, to produce a minimal data set, you can use `head()`, `subset()`, or the indices. Then use `dput()` to give us something that can be put in R immediately. Also, please make sure you know what to do [when someone answers your question](https://stackoverflow.com/help/someone-answers). Finally, here is a link to Stack Overflow's [help center](https://stackoverflow.com/help). Thank you! – iamericfletcher Aug 11 '20 at 11:29

1 Answers1

0

You can compute the means of all columns and store them in a named vector. Then, just plot this vector (sorted, if you want) with barplot.

Since you did not provide any data, I can show you the general idea with an example dataet:

data<-data.frame(col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,3,4,4,4,1,2,5,3,5),
                 col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,7,2,7,7,2,3,7,2,4)
)

means=colMeans(data[,c("col1","col2")])
barplot(means[order(means,decreasing=T)])

EDIT: Note, that the colMeans function will return NA if you have missing values in your columns. If there are only NAs in a numeric vector, you can't draw anyting. Include the argument na.rm=T if you have missing values.

That is:

means=colMeans(data[,c("col1","col2")],na.rm=T)
barplot(means[order(means,decreasing=T)])
Martin Wettstein
  • 2,532
  • 1
  • 6
  • 14