【发布时间】:2021-08-16 07:22:52
【问题描述】:
我有一个名为“employee_attrition”的数据框。我感兴趣的变量有两个,第一个称为“MonthlyIncome”(具有连续的工资数据),第二个是“PerformanceRating”,它采用离散值(1、2、3 或 4)。我的目的是为 MonthlyIncome 创建一个直方图,并在同一图中显示 PerformanceRating。我有这个:
ggplot(data = employee_attrition, aes(x=MonthlyIncome, fill=PerformanceRating))+
geom_histogram(aes(y=..count..))+
xlab("Salario mensual (MonthlyIncome)")+
ylab("Frecuencia")+
ggtitle("Histograma: MonthlyIncome y Attrition")+
theme_minimal()
问题是该图没有显示与直方图的每个条相关联的“PerformanceRating”。
我的数据框是这样的:
MonthlyIncome PerformanceRating
1 5993 1
2 5130 1
3 2090 4
4 2909 3
5 3468 4
6 3068 3
我想要一个直方图,显示 MonthlyIncome 的频率以及每个条形图,其中包含 4 种颜色的 PerformanceRating。
类似的东西,但有 4 种颜色(PerformanceRating 值)
【问题讨论】:
-
如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。目前还不清楚你期望这个情节是什么样子。您是否正在寻找堆积条形图类型的外观?也许你想要
aes(x=MonthlyIncome, fill=factor(PerformanceRating))? -
MrFlick 几乎可以肯定是正确的,如果你想要离散的填充颜色,你需要像
factor这样的离散数据类型。 -
也就是说,堆积直方图可能很难阅读 - 我建议也使用
facet_wrap(~PerformanceRating)。 -
我想要这样的东西:每个条包含 4 种颜色,代表 PerformanceRating 值的频率,整个条的大代表 MonthlyIncome 的频率。