Reputation: 35
A dput() of data I wish to use this on is included below.
I am trying to plot a bar chart (stacked or not) which visualises the count of people with and without a binary characteristic (in this case, LGE presence). I then want to show two grouped pairs of bars next to each other, namely the first and last MRI measurement. I then want those measurements split by the treatment/control grouping variable subject_group.
So far, I used ggplot(data_lge_long, aes(variable)) + geom_bar(aes(fill = value))
and got part of the way, but I can't figure out how to split the stacked bars by treatment group.
Data:
dput(data_lge_long)
structure(list(subject_group = structure(c(1L, 2L, 2L, 1L, 1L,
2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 2L), .Label = c("eplerenone", "no treatment"), class = "factor"),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("first_mri_lge_yn",
"last_mri_lge_yn"), class = "factor"), value = c("no LGE",
"no LGE", "no LGE", "LGE present", "no LGE", "no LGE", "no LGE",
"no LGE", "no LGE", "LGE present", "no LGE", "no LGE", "no LGE",
"LGE present", "no LGE", "no LGE", "no LGE", "no LGE", "LGE present",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"LGE present", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"no LGE", "LGE present", "LGE present", "LGE present", "no LGE",
"no LGE", "LGE present", "no LGE", "no LGE", "LGE present",
"no LGE", "no LGE", "LGE present", "no LGE", "LGE present",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "LGE present",
"LGE present", "no LGE", "LGE present", "LGE present", "LGE present",
"LGE present", "no LGE", "LGE present", "no LGE", "no LGE",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "LGE present",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "LGE present",
"no LGE", "no LGE", "no LGE", "LGE present", "no LGE", "no LGE",
"no LGE", "no LGE", "LGE present", "no LGE", "LGE present",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"no LGE", "no LGE", "no LGE", "no LGE", "no LGE", "no LGE",
"no LGE", "no LGE", "LGE present", "no LGE", "no LGE", "no LGE",
"LGE present", "no LGE", "no LGE", "no LGE", "LGE present",
"LGE present", "LGE present", "LGE present", "no LGE", "no LGE",
"LGE present", "no LGE", "LGE present", "LGE present", "no LGE",
"no LGE", "LGE present", "LGE present", "LGE present", "no LGE",
"LGE present", "no LGE", "no LGE", "no LGE", "LGE present",
"LGE present", "no LGE", "LGE present", "LGE present", "LGE present",
"no LGE", "LGE present", "LGE present", "no LGE", "LGE present",
"no LGE", "LGE present")), row.names = c(NA, -152L), class = "data.frame")
Upvotes: 2
Views: 1074
Reputation: 12461
Ah. The problem with dput
and the graph was due to a formatting problem in your post. Here are a few options that may give you close to what you want.
ggplot(d, aes(variable)) +
geom_bar(aes(fill = interaction(value, subject_group)))
and
ggplot(d, aes(variable)) +
geom_bar(aes(fill = value), position="dodge") +
facet_wrap(~subject_group)
and
ggplot(d, aes(variable)) +
geom_bar(aes(fill = interaction(value, subject_group)), position="dodge")
Upvotes: 1