1
votes

I work with a large number of data(n=2057) and my data frame looks like:

        id_num    Gender   Protein_Milk   Protein_Cheese
 1       2345       1           4.5           3.4
 2       45983      2           5.6           5.2
 .         .        .            .             .
 .         .        .            .             .
 .         .        .            .             .
2057    13454       1           2.6            8.5

I want to create a barplot has both columns Protein_Milk and Protein_Cheese side by side on the x axis grouped by gender. Y axis shows the mean value of Protein(g). The problem is, that I can not create barplot has both columns in it. So I have 2 different barplots for each column (Protein_Milk/Protein_Cheese).

My R-command:

  Data_Frame$Gemder<-factor(Data_Frame$Gender, levels = c(1,2), labels = c("Men", "Women"))
  Barplot<-ggplot(Data_Frame, aes(Gender, Protein_Milk))
  Barplot +
  stat_summary(fun.y = mean, geom = "bar")+
  stat_summary(fun.data = mean_cl_normal, geom = "errorbar")

Anybody has any suggestions? Thanks in advance

Edit: Since my data is large, I can't use the solution here:

Creating grouped bar-plot of multi-column data in R

I need to find a way how to create barplot with two column, without writing all entries in c() or read.table(text=" ") since it would take so long for 2057 entries per column.

1
Just to clarify, what's your question? Would love to help but not sure what the problem is.Ben G
Thanks for caring. The problem is, that I can not create barplot has mean of both Protein_Milk and Protein_Cheese columns in it. So I have 2 different barplots for each column (one barplot for Protein_Milk/ one for Protein_Cheese).o.d.rinani

1 Answers

0
votes

Still not entirely sure the type of output you want, but here's an example. The main problem is that you're data is in long format and needs to be in tall format. For more, check out: http://r4ds.had.co.nz/tidy-data.html.

Here's my solution which uses the facet wrap to place a graph for each gender side by side. I'm making some dummy data for simplicity.

library(tidyverse) 

data <- tibble(id = c(1:4), 
               gender = c(1, 2, 1, 2), 
               protein_cheese = c(4, 5, 6, 7), 
               protein_milk = c(6, 7, 8, 9)
        )

data %>%
  gather(key = type, 
         value = protein,
         protein_cheese:protein_milk) %>%
         ggplot(aes(x = type, y = protein)) +
         geom_col() +
         facet_wrap( ~ gender)