0

my data looks like this:

groupvar    var1   var2   var3    var4
group1      1       1       0       0
group1      0       1       1       1
group3      1       0       1       0
group4      1       1       0       1
group4      0       0       0       1
group2      0       1       1       0

I would like to have an output that looks like this:

Some made up data

I'm not too sure how to go about this, does it require reshaping the data to long somehow? Any help is appreciated.

EDIT: I have spent a good deal of time searching, it's very possible I'm bad at searching but if you could direct me in the right place rather than downvoting that would be soooooo much better.

Edit2: I don't quite know how to use dput but here's my output from it:

structure(list(groupvar = c("group1", "group1", "group2", "group2", 
"group2", "group3", "group3", "group3", "group1", "group1"),
var1 = c(0, 0, 1, 0, 0, 0, 1, 1, 1, 1), var2 = c(0, 0, 1, 
1, 0, 1, 1, 1, 0, 1), var3 = c(1, 1, 0, 1, 1, 0, 1, 0, 0, 
0), var4 = c(1, 0, 1, 0, 1, 1, 1, 0, 1, 1)), .Names = c("groupvar", 
"var1", "var2", "var3", "var4"), row.names = c(NA, -10L), class = 
"data.frame")
4
  • You probably got down voted because you provided data in to format that takes more than one second to load. Better use dput Commented May 16, 2017 at 22:50
  • You can use reshape2::melt or tidyr::gather to reshape your dataset from wide to long, which is then easy to plot in ggplot2 Commented May 16, 2017 at 22:51
  • look at ggplot2.tidyverse.org/reference/position_dodge.html for plotting after melting. Also look at this question stackoverflow.com/questions/17182938/… Commented May 16, 2017 at 22:51
  • Ah, I offered a visual representation of the data because I cannot share the data. I'll try make the data I posted and use dput now, thank you! Commented May 16, 2017 at 22:54

1 Answer 1

1

Is this what you're looking for?

library(tidyverse)
d <- read_csv("groupvar,    var1,   var2,   var3,   var4
                group1,      1,       1,       0,       0
                group1,      0,       1,       1,       1
                group3,      1,       0,       1,       0
                group4,      1,       1,       0,       1
                group4,      0,       0,       0,       1
                group2,      0,       1,       1,       0")

td <- d %>% 
    gather(var, val, -1) %>% 
    group_by(groupvar, var) %>% 
    summarize(tot = sum(val))


ggplot(td, aes(groupvar, tot, fill = var)) +
    geom_bar(stat = "identity", position = "dodge")

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

That's exactly it! I had a feeling it was a combination of reshaping and summarizing but I could not wrap my head around how exactly to do it. Thank you very much!
Great! Glad it was helpful! (you may want to mark it as correct)
Can I ask why the third argument to gather is -1, and do I need to rename my variables to the same prefix if they do not share a prefix like var?
The third argument to gather specifies the columns to gather. So in this case, all but the first one. You should be fine if they don't have the same prefix, but I'm not sure... I may be misunderstanding your question. But basically you'll want to try to get your data in a tidy format. See vita.had.co.nz/papers/tidy-data.html
Yep that makes sense. Seriously thank you so much for help and for the paper.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.