I am attempting to create a column chart which has a variable "Hall" represented for their totals across months. I had some help with the code but I ran it it is putting all of the halls on top of eachother as a stacked column chart as opposed to dodged. Any ideas?
Here's what I've tried.
library(tidyverse)
fall2 <- structure(list(Hall = c("1959E", "1959E", "1959E", "1959E", "1959E",
"2109 F", "2109 F", "2109 F", "2109 F", "2109 F"), Month = c("August",
"December", "November", "October", "September", "August", "December",
"November", "October", "September"), total = c(2, 4, 5, 11, 8,
1, 3, 8, 7, 4)), row.names = c(NA, -10L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "Hall", drop = TRUE, indices = list(
0:4, 5:9), group_sizes = c(5L, 5L), biggest_group_size = 5L, labels =
structure(list(Hall = c("1959E", "2109 F")), row.names = c(NA, -2L),
class = "data.frame", vars = "Hall", drop = TRUE))
fall2$Month <- fall2$Month %>%
fct_relevel("August", "September", "October", "November", "December")
fall2 <- fall2 %>%
arrange(Month, -total) %>%
mutate(order = row_number())
#something like this?
ggplot(fall2, aes(order, total)) +
geom_col(aes(fill = total), position = "dodge") +
guides(fill=FALSE) +
ggtitle("Fall Events by Hall") +
facet_wrap(~Month, nrow = 1, scales = "free_x") +
scale_x_continuous(breaks = fall2$order, labels = fall2$Hall,expand = c(0,0))
I would like it to look like 2 halls for each month, not stacked. It has to be a small error I feel.
ggplot
needs a categorical variable to dodge by. Here I've added the group = Hall
to aes
to tell it to dodge by hall value:
ggplot(fall2, aes(order, total)) +
geom_col(aes(fill = total, group = Hall), position = "dodge") +
guides(fill=FALSE) +
ggtitle("Fall Events by Hall") +
facet_wrap(~Month, nrow = 1, scales = "free_x") +
scale_x_continuous(breaks = fall2$order, labels = fall2$Hall,expand = c(0,0))
As you can see, we still get overlapping labels on the x-axis. Your code says breaks = fall2$order, labels = fall2$Hall
, making the x-axis labels at unique order
values and labeling them with the respective Hall
values. Looking at your data:
fall2
# A tibble: 10 x 4
# Groups: Hall [2]
Hall Month total order
<chr> <fct> <dbl> <int>
1 1959E August 2 1
2 2109 F August 1 1
3 1959E September 8 2
4 2109 F September 4 2
...
We can see that each order
value has multiple Hall
values---so ggplot
is doing exactly what you are asking it to do. At order = 1
we get the corresponding Hall
labels: "1959E"
and 2109 F
(rows 1 and 2).
I'm not really sure why you're using order
at all... seems pointless. If we instead put Hall
on the x-axis (and make some other related changes, no need for group
anymore, nor specifying labels, nor position "dodge"
. But we need a discrete
not a continuous
x scale), things get simpler and look better.
ggplot(fall2, aes(Hall, total)) +
geom_col(aes(fill = total), width = 1) +
guides(fill=FALSE) +
ggtitle("Fall Events by Hall") +
facet_wrap(~Month, nrow = 1, scales = "free_x") +
scale_x_discrete(expand = c(0,0))