0

So I made the graph "Most popular age group" and I found out that the age '26-35' is the most popular and now I am trying to look into what 'gender' they are comprised of and make another bar plot out of it.


filename <- read.csv("./XYZ.csv")
myData = filename

install.packages("ggplot")
library(ggplot2)


table(myData$Age)
barplot(table(myData$Age),xlab="Age group", ylab = 'Number of Purchases', main ='Most popular age group', col = 'pink',)

graph of Most Popular Age group

MLavoie
  • 9,671
  • 41
  • 36
  • 56
dummy123
  • 31
  • 6
  • Please make your question reproducible by adding the data, e.g. with ´dput(myData)´. See also [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – help-info.de Jul 13 '19 at 10:31
  • I think you need a stacked barplot... You need do count not only by age but also gender... try maybe to include gender in your call to table() – xhr489 Jul 13 '19 at 11:29

1 Answers1

0

Something like this should work

library(dplyr)
library(ggplot2)
df %>% 
  count(Age, Gender) %>% 
  ggplot(aes(x=Age, y = n, fill = Gender)) +
  geom_bar(stat = "identity", position = "stack", color="black") 

Here I am using ggplot2 and dplyr, if you want to use barplot() maybe look here: https://www.statmethods.net/graphs/bar.html

xhr489
  • 1,957
  • 13
  • 39