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.