u/fuckpineapplepizza

Trying to add labels of count to my stacked bar chart

Trying to add labels of count to my stacked bar chart

Hi everyone,

thank you everyone who has taken the time to help me before, I am really, really appreciative. Since I don't know the language that well yet and I am very much learning by doing, as I finish the project I am working on presently, I struggle with finding the errors, when I apply the answers others were given online.

I have created a stacked bar chart and I would very much like to add counts to the columns.

surveyresponses_Freizeit_Master_for_stacked %>%
  pivot_longer(-Arbeit, names_to = "Group", values_to = "value") %>%
  summarise(count = sum(value), .by = c("Arbeit", "Group")) %>%
  ggplot(aes(Group, count, fill = Arbeit,)) +
  geom_col() +
  theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))

This would be my code, and it produced the graph as I need it... However, when I tried to add the count, based on this code that tackled a similar problem:

# Source - https://stackoverflow.com/a/63656093
# Posted by stefan, modified by community. See post 'Timeline' for change history
# Retrieved 2026-05-13, License - CC BY-SA 4.0

library(ggplot2)

ggplot(mtcars, aes(cyl, fill = factor(gear))) +
  geom_bar(position = "fill") +
  geom_text(aes(label = after_stat(count)),
    stat = "count", position = "fill"
  )

I receive this result:

Browse[1]> surveyresponses_Freizeit_Master_for_stacked %>%
+   pivot_longer(-Arbeit, names_to = "Group", values_to = "value") %>%
+   summarise(count = sum(value), .by = c("Arbeit", "Group")) %>%
+   ggplot(aes(Group, fill = Arbeit, label = after_stat(count)), stat = "count") +
+   geom_col() +
+   theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))
Error during wrapup: Problem while mapping stat to aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error:
! Aesthetics must be valid computed stats.
✖ The following aesthetics are invalid:
• `label = after_stat(count)`
ℹ Did you map your stat in the wrong layer?

I understand the error message, but I am not sure what I ned to change to get the desired result... Again, I appreciate any help!

u/fuckpineapplepizza — 17 hours ago

Creating a stacked bar chart with a complex data set - advice please

Update: Has been solved, thank you for all the responses

Hi everyone,

everyone has been so kind and helpful so I am asking one last question, that the internet, unfortunately, could not answer for me...

I would like to create a stacked bar chart with a complex dataset. My dataset looks a little like this:

Work Group 1a Group 1b Group 2a ...(up to 9)
yes 0 1 0 ...
no 1 0 0 ...
...

I have tried to use this explanation online, but I am unsure what to add for "points" in the code.

#create data frame
df <- data.frame(team=rep(c('A', 'B', 'C'), each
=3),
                 position=rep(c('Guard', 'Forward', 'Center'), times
=3),
                 points=c(14, 8, 8, 16, 3, 7, 17, 22, 26))

#view data frame
df

  team position points
1    A    Guard     14
2    A  Forward      8
3    A   Center      8
4    B    Guard     16
5    B  Forward      3
6    B   Center      7
7    C    Guard     17
8    C  Forward     22
9    C   Center     26



library
(ggplot2)

ggplot(df, aes
(fill=position, y=points, x=team)) + 
  geom_bar(position='stack', stat='identity')

Further explanation:

I am trying to map which students have time for leisure so the dataset looks as follows:
'Work' answers the question "Do you work?" with Yes or no
Group 1a would be: Yes I have time for leisure and my parents support me
Group 1b would be: Yes I have time for leisure and my parents don't support me --> if a person falls into this category I assigned a 1, if they don't a 0 --> this counts for all the groups (up to 9).

I would like to have all the groups on the x-Axis and the answers to "do you work" stacked for each group.

Would the best approach be, to group the yes or no answers and count the values for each group and then based off of that do the stacked bar chart?

Unfortunately, since it has taken me a while to relearn a lot about R and there were a lot of data to present and organise, I am by now in a bit of a time crunch, so I only have today to finish all my graphs and I don't have as much time as I would like to try out different approaches. I'd appreciate any help you can give me.

reddit.com
u/fuckpineapplepizza — 2 days ago

Calculating percentages

Hi everyone,

thank you for your help last time with finding the problem in my code for plotly. I am struggling with calculating the percentages and receiving a tidy usable table using the mutate() function. Unfortunately, all the tutorials online do not seem to work for me and I don't understand what I am doing wrong.

```

surveyresponses_Freizeit_Master_count=surveyresponses_Freizeit_Master%>% count(.$`Bleibt genug Zeit für Freizeit?`)

```

After this I receive a table where all the answers per group have been calculated and what I would need is an additional column in which I have the percentages adding up to 100% and I am not sure how to get there... Could anyone please help? I would really like to learn how to do it and to truly understand it, because while I could do it by hand, I do have two more datasets I need to do this for. I appreciate any help.

https://preview.redd.it/ks6vlv7xyn0h1.png?width=874&format=png&auto=webp&s=0a81cc3ef285846578dbd28c60a9f586736983d5

reddit.com
u/fuckpineapplepizza — 2 days ago

Hi everyone,

I have tried finding out what the issue is for quite some time now, but since I am not the most proficient regarding technology, I am struggling finding something applicable.

My goal was to do a simple pie chart with plotly, but for some reason the code that I used previously and copy-pasted, always comes back with the 'unexpected symbol' error. I have tried finding a punctuation error or a misspelling, but nothing. I downloaded the new version of R Studio today and I think I might be missing some packages, but I also don't know which ones they might be and my research did not yield anything. I have installed tidyverse, plotly, dplyr, ggplot2 and readxl.

I used the code from this website and adjusted it for my dataset

https://www.geeksforgeeks.org/r-language/how-to-create-pie-chart-using-plotly-in-r/

'plotly::plot_ly(data=surveyresponses_Freizeit_Bachelor_count,values=~n,labels=~factor(Bleibt genug Zeit für Freizeit?),'

'marker=list(colors=c("green","orange","blue")),'
'type="pie") %>% layout(title="Bleibt genug Zeit für Freizeit im Bachelor?")'

I look forward to any input and thank you all in advance for your help. Maybe it's something really stupid, that I just didn't see...

u/fuckpineapplepizza — 6 days ago