Juanito Tomas
Juanito Tomas

Reputation: 151

How to create a barchart with a main grouping variable and a sub-grouping variable using ggplot2?

I would like to make a barchart of Z, grouped by a grouping variable known as Y and then further grouped to X. I'm sure there's an ideal way to plot this however this is what I've got.

HW1bii <- HW1b %>% 
  group_by(Y,X)

ggplot(data = HW1bii, mapping = aes(x = Z)) +
  geom_bar(color = "Black", fill = "steelblue")

Not exactly what I'm looking for but I hope someone can help me find something better.

structure(list(X = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L), Y = c(1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L), Z = c(113L, 109L, 105L, 80L, 122L, 88L, 104L, 98L, 93L, 
105L, 99L, 105L, 99L, 105L, 94L, 104L, 110L, 109L, 106L, 93L, 
125L)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame"
))

Upvotes: 1

Views: 84

Answers (2)

www
www

Reputation: 39154

There are many ways to do this. Here is plot the X on the x-axis, group by Y using facet_grid.

library(ggplot2)

ggplot(data = HW1b, aes(x = factor(X), y = Z)) +
  geom_bar(stat = "identity", color = "Black", fill = "steelblue", width = 0.5) +
  scale_x_discrete(name = "X") +
  facet_grid(Y ~ .)

enter image description here

You can also flit the axis with coord_flip.

ggplot(data = HW1b, aes(x = factor(X), y = Z)) +
  geom_bar(stat = "identity", color = "Black", fill = "steelblue", width = 0.5) +
  scale_x_discrete(name = "X") +
  coord_flip() +
  facet_grid(Y ~ .)

enter image description here

Or use facet_grid(Y ~ X, scales = "free_x)

ggplot(data = HW1b, aes(x = factor(X), y = Z)) +
  geom_bar(stat = "identity", color = "Black", fill = "steelblue", width = 0.5) +
  scale_x_discrete(name = "") +
  facet_grid(Y ~ X, scales = "free_x")

enter image description here

Or map Y to fill.

ggplot(data = HW1b, aes(x = factor(X), y = Z, fill = factor(Y))) +
  geom_bar(stat = "identity", color = "Black", width = 0.5) +
  scale_x_discrete(name = "X") +
  scale_fill_viridis_d(name = "Y") +
  coord_flip()

enter image description here

Or use position = "dodge" to create group bar chart.

ggplot(data = HW1b, aes(x = factor(X), y = Z, fill = factor(Y))) +
  geom_bar(stat = "identity", position = "dodge", color = "Black", width = 0.5) +
  scale_x_discrete(name = "X") +
  scale_fill_viridis_d(name = "Y")

enter image description here

And this is not a bar plot, but you may also consider using geom_tile to map Z as the fill.

ggplot(data = HW1b, aes(x = X, y = Y, fill = Z)) +
  geom_tile(color = "Black") +
  scale_fill_viridis_c()

enter image description here

DATA

HW1b <- structure(list(X = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
                     4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L), Y = c(1L, 2L, 3L, 
                                                                        1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
                                                                        2L, 3L), Z = c(113L, 109L, 105L, 80L, 122L, 88L, 104L, 98L, 93L, 
                                                                                       105L, 99L, 105L, 99L, 105L, 94L, 104L, 110L, 109L, 106L, 93L, 
                                                                                       125L)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame"
                                                                                       ))

Upvotes: 2

Abdallah Atef
Abdallah Atef

Reputation: 671

You can use facet_wrap() to do this after creating a grouping variable:

ggplot(HW1b %>% mutate(group = paste(X, Y, sep = "-")), aes(x = Z)) +
geom_bar(color = "Black", fill = "steelblue") +
facet_wrap(~group)

output of the above code Note that ggplot() ignores groups created by group_by(), so you don't need the first step of your code

Upvotes: 0

Related Questions