I have a dataset which looks like this one below. I am trying to make a barplot with the grouping variable gender, with all the variables side by side on the x axis (grouped by gender as filler with different colors), and mean values of variables on the y axis (which basically represents percentages)
tea coke beer water gender
14.55 26.50793651 22.53968254 40 1
24.92997199 24.50980392 26.05042017 24.50980393 2
23.03732304 30.63063063 25.41827542 20.91377091 1
225.51781276 24.6064623 24.85501243 50.80645161 1
24.53662842 26.03706973 25.24271845 24.18358341 2
In the end I want to get a barplot like this
any suggestions how to do that? I made some searches but I only find examples for factors on the x axis, not variables grouped by a factor. any help will be appreciated!
Best Answer
You can use aggregate to calculate the means:
Get rid of the Group.1 column
Then you have reformat the data to be in long format:
Finally, you can use ggplot2 to create your plot: