0
votes

I try to plots the differnces for Males and females of differentes ages in function of their expenses.

I want to create categories 1= Age<25, 2= Age25-45, 3= Age>45, 4= Age 45-55, 5=Age >55

ggplot(Adv.csv, aes(Age<25,AveMonthSpend)) + geom_boxplot(aes(color =Gender))

ggplot(Adv.csv, aes("Age 25-45",AveMonthSpend)) + geom_boxplot(aes(color =Gender))

ggplot(Adv.csv, aes(Age>45,AveMonthSpend)) + geom_boxplot(aes(color =Gender))

ggplot(Adv.csv, aes("Age 45-55",AveMonthSpend)) + geom_boxplot(aes(color =Gender))

ggplot(Adv.csv, aes(Age>55,AveMonthSpend)) + geom_boxplot(aes(color =Gender))

I want to put all these plots in the same graphic to compare the results Source: https://www.kaggle.com/philboaz/kernel8523b5e9dc/edit

1
Are you sure you want > 45 and then 45-55 ?AntoniosK

1 Answers

0
votes

You might want to check out cut():

library(tidyverse)

#generate Dataset
data <- tibble(Age = sample(12:99, 50,T), 
               AveMonthSpend = rnorm(50, 100,15), 
               Gender = sample(c('f','m'),50,T)) 

# group data and plot
data %>% 
  mutate(AgeGroup = cut(x = Age, 
                        breaks = c(-Inf, 25, 45, 55, Inf), 
                        labels = c('<25', '25-45', '45-55', '>55'), 
                        right = T)) %>%
  ggplot(aes(x = AgeGroup, y = AveMonthSpend, color = Gender)) +
  geom_boxplot()