0

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Juanito Tomas
  • 141
  • 1
  • 11

1 Answers1

1

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

enter image description here

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

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • So order was to see if I could order by descending order, something I was playing with. My larger dataset has 19 Halls, how could I show the Hall names without them all covering eachother. – Juanito Tomas Mar 29 '19 at 14:42
  • With dodging, you can't label individual bars. The idea of dodging/stacking is that you have multiple bars *at a single point on the x-axis*. And a single point has a single label. [Here's a good example of ordering bars separately within each facet](https://stackoverflow.com/q/34001024/903061). – Gregor Thomas Mar 29 '19 at 14:51