0

I have four variables as columns in my data set:

  1. whether the person had free school meals when they were younger
  2. whether the person's parents attended university
  3. whether the person studied A-level drama at school
  4. whether their school offered A-level drama

Each value in the column is either "yes", "no" or "not applicable".

I want to put four sets of bar charts on one single plot (which I can then save as a .png), with each of the bar charts having a yes bar and a no bar. I have used the below to create a frequency table for each of the variables. Here I've used the example of whether the person received free school meals (FSM) when they were younger:

FSM_df <- champions %>% count(FSM, sort = TRUE) %>% mutate(pct = prop.table(n))
percentage = label_percent()(FSM_df$pct)
FSM_df$percentage = percentage

I can use the code below to create a single bar chart, but I'm not sure how to do this for multiple plots:

ggplot(FSM_df, aes(x = FSM, y = n, fill = "#fe8080")) + geom_bar(stat = "identity", show.legend = FALSE) + coord_flip() + labs(x = "FSM", y = "Number of Champions") + geom_text(aes(label = percentage), color = "#662483")
Jess
  • 11
  • 2

1 Answers1

0

Generating Random Data

lunch <- sample(0:1, 100, replace = TRUE, prob = c(0.7,0.3))
parents <- sample(0:1, 100, replace = TRUE, prob = c(0.5,0.5))
drama_major <- sample(0:1, 100, replace = TRUE, prob = c(0.9,0.1))
drama_offered <- sample(0:1, 100, replace = TRUE, prob = c(0.8,0.1))

Creating the Tibble

df <- tibble(lunch = lunch,
       parents = parents,
       drama_major = drama_major,
       drama_offered)

pivot_longer

df %>% 
  pivot_longer(cols = 1:4,
               names_to = "measure",
               values_to = "measure_is_true_1") %>% 
  mutate(is_true = if_else(measure_is_true_1 == 0, "no", "yes")) %>% 
  ggplot(aes(x = measure)) +
  geom_bar(aes(fill = is_true), position = "dodge", alpha = 0.7) +
  coord_flip() +
  theme_bw()

^ in this example, you should convert your data to long format and then set the grouping aesthetics using the fill parameters. The ggplot logic should be: plot my groups along the x axis and count the frequency for each time it's a 0 or 1 in the response column (whether or not they were on free lunch/drama, etc). This is how you can achieve it all on the same plot.

Simple Bar Chart Plot

  • This is great thank you! The last thing I want to do is change the variable names that appear on the Y axis. How can I do this? Using `labs(y = ...)` just adds a label to the Y axis, but I want to rename each of the column titles (variables). – Jess Feb 05 '23 at 18:01
  • Sorry for the late reply- that's a good question. From my experience, you need to specify the type of scale and then pass in a vector of the column names as the label argument. `labels_vector <- c("Lunch", "Parents", "Drama", "Drama Offered")` and then **Pass in `scale_x_discrete` function** `scale_x_discrete(labels = labels_vector)` – SirMightyMike Mar 12 '23 at 19:48