简体   繁体   中英

Plotting mean of multiple variables in R

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?

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)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM