简体   繁体   中英

Is there a way to further split a grouped ggplot bar chart by another grouping variable?

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.

Result so far: 阴谋

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

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

在此处输入图像描述

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