我尝试绘制不同年龄段的男性和女性在支出方面的差异。
我要创建类别1 =年龄<25,2 =年龄25-45,3 =年龄> 45,4 =年龄45-55,5 =年龄> 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(“年龄45-55”,AveMonthSpend))+ geom_boxplot(aes(color = Gender))
ggplot(Adv.csv,aes(年龄> 55,AveMonthSpend))+ geom_boxplot(aes(color = Gender))
我想将所有这些图放在同一张图中以比较结果 来源:https://www.kaggle.com/philboaz/kernel8523b5e9dc/edit
答案 0 :(得分:0)
您可能想签出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()