Struggling to Create Stacked Bar

I am fairly new to working in RStudio and have been struggling to create a stacked bar chart. I wanted the the bar chart to stack "shots" and "goals". I'd appreciate any advice.
(I am also trying to create my first reprex. This may not be in the correct format, and I'll take advice on that too!) Thank you.

df <- data.frame(stringsAsFactors = FALSE,
        = c("Australia", "Brazil", "Canada", "England", "France", "Germany")
       shots = c(14.2, 12.8, 15.2, 12.7, 18.2, 15.4) 
       goals = c(2.25, 1.75, 1, 1.86, 2, 2)
       total_combined = c(16.5, 14.5, 16.2, 14.6, 20.2, 17.4)
ggplot(data = shots_goals,
 aes(x = reorder(, shots), y = total_combined, fill = "goals")) + 
 geom_bar(stat = "identity", width = 0.5) + 
  labs(y="Average Shots Per Game") +
  geom_col(fill = "lightblue", , width = 0.5)+
  theme(axis.title.y = element_blank()) +
  scale_y_continuous( expand = c(0,0)) +
  coord_flip() +

Welcome to the community! I hope this does not come across as having an assignment graded. The habits from 38 years of teaching are hard to break. It is great that you are learning how to create a reprex. It makes a big difference for people trying to find solutions.

Unless the "shots" variable is for shots on goal that did not score a goal, it does not make sense to add shots and goals together. If I take 5 exams and get an A on three of them, what does the total of eight represent? I created a new variable for misses = shots - goals. When you stack goals and misses, the total height will equal the number of shots.

For your reprex, you use df as the name of the data in one place and then shots_goals in another.
Also, you need commas to separate each variable and another ) at the end.

I am not sure why you use both geom_bar() and geom_col()

For a stacked bar chart, fill = should be a column with the different groups. I used tidyr::pivot_longer() to create the "stats" column with "misses" and "goals". Notice that "shots" is still there to use for reorder().

library(tidyverse)  # will load all the required tidyverse packages

shots_goals <- data.frame(stringsAsFactors = FALSE,
        = c("Australia", "Brazil", "Canada", "England", "France", "Germany"),
                 shots = c(14.2, 12.8, 15.2, 12.7, 18.2, 15.4), 
                 goals = c(2.25, 1.75, 1, 1.86, 2, 2))

shots_goals_long <- shots_goals |> 
  mutate(misses = shots - goals) |> 
  pivot_longer(cols = c(misses, goals), names_to = "stats")

shots_goals_long |> 
  ggplot(aes(x = reorder(, shots), y = value, fill = stats)) + 
  geom_bar(position = "stack", stat = "identity") +
  scale_fill_manual(values = c("blue","lightblue")) +
  xlab("Country") +
  ylab("Shots on goal") +
  coord_flip() +

Created on 2023-06-29 with reprex v2.0.2


Thank you for taking the time to walk through this with me. I was able to put the stacked bar chart together.
The "shots" variable was for shots not resulting in goal, so I wanted the stacked bar chart to show "shots"+"goals". I did not need the "total_combined" variable.

I also learned more about creating a proper reprex.


Glad to be of help!

total_combined would still be useful if you want to order the countries by shots taken, whether they scored a goal or not:

shots_goals_long <- shots_goals |> 
  pivot_longer(cols = c(shots, goals), names_to = "stats")
shots_goals_long |> 
  ggplot(aes(x = reorder(, total_combined), y = value, fill = stats)) + 
1 Like

Looks great! Thanks so much!

To create a stacked bar chart that includes the term "Mall of Toys," you can follow these steps:

  1. Determine the data categories you want to represent on the x-axis. For example, let's consider four categories: Category A, Category B, Category C, and Category D.
  2. Decide on the values for each category that you want to represent in the stacked bar chart. Let's assume the values are as follows:
  • Category A: 50
  • Category B: 30
  • Category C: 20
  • Category D: 40
  1. Create the stacked bar chart using these values, with the y-axis representing the numerical values and the x-axis representing the categories.
  2. Label the chart with the title "Mall of Toys Stacked Bar Chart" to include the term "Mall of Toys" in the chart.
  3. Add a legend to the chart to explain the meaning of each colored section of the stacked bars. For example, you can use different colors to represent different attributes or subcategories within each category.

By following these steps, you should be able to create a stacked bar chart that includes the term "Mall of Toys" and represents four categories with their respective values.

Welcome to the community,

If the four categories are on the x-axis and there is one value for each category on the y-axis, this will be a simple bar chart, not a stacked bar chart.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.