Is this what you're looking for regarding the first part?
(I advise you change colors to prevent epileptic seizures.)
Data %>%
mutate_all(cut, c(0, 2, 4, 6, 7), right = F, ) %>%
gather(key = "variable", value= "value") %>%
ggplot(aes(x = variable, fill = value)) +
geom_bar(position = position_fill(reverse = TRUE)) +
coord_flip() +
scale_fill_manual(values=c("yellow", "orange", "red", "green"))
For the second part, a reproducible example would be useful but you can probably add a "group" variable (between gather
and ggplot
) and use facet_grid
or facet_wrap
.
--- Edited below after information about groups ---
Column selection is missing in DataG[Data_IlA$G1_ID == 2]
and variable names are not the same as the one in DataG
so DataG_1 cannot be created.
Does one of the suggestions below make the figure you want?
DataG %>%
gather(key = "variable", value = "value", -Group_ID) %>%
mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
ggplot(aes(x = variable, fill = value)) +
geom_bar(position = position_fill(reverse = TRUE)) +
scale_y_continuous(labels = scales::percent) +
coord_flip() +
scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
theme(panel.background = element_blank()) +
xlab("") + ylab("") +
facet_grid(Group_ID ~ .)
DataG %>%
gather(key = "variable", value = "value", -Group_ID) %>%
mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
ggplot(aes(x = Group_ID, fill = value)) +
geom_bar(position = position_fill(reverse = TRUE)) +
scale_x_discrete(limits = c("Group 1","Group 2")) +
scale_y_continuous(labels = scales::percent) +
coord_flip() +
scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
theme(panel.background = element_blank()) +
xlab("") + ylab("") +
facet_grid(variable ~ .)
--- Edited below after comment on groups ---
If you need to change categories for any variable, the easiest way may be to do so before calling ggplot
:
DataG %>%
mutate(Group_ID = case_when(
Group_ID == 1 ~ "1st group's name",
Group_ID == 2 ~ "2nd group's name"
)) %>%
gather(key = "variable", value = "value", -Group_ID) %>%
mutate(value = cut(value, c(0, 1.99, 3.99, 5.99, 7))) %>%
ggplot(aes(x = variable, fill = value)) +
geom_bar(position = position_fill(reverse = TRUE)) +
scale_y_continuous(labels = scales::percent) +
coord_flip() +
scale_fill_manual(values=c("#19557E","#6E3B60", "#EA916A", "#EFC76C")) +
theme(panel.background = element_blank()) +
xlab("") + ylab("") +
facet_grid(Group_ID ~ .)