首页 > 解决方案 > 没有使用 Rmisc 和 ggplot 的图例

问题描述

我正在尝试为分类变量与分类变量创建 3 个图。我没有看到任何关于不同类别变量 C1、C4、C5 的图例。条形图也是灰色的。该数据集包含 9 个分类变量和 6 个数值变量的信息。C1、C4、C5分别有2、4、3个类别。已雇用 (1,0) 是显示候选人是否会被雇用的变量。

install.packages("Rmisc")

library(Rmisc)
p1<-ggplot(hireouti, aes(x = Hired, fill = C1)) + geom_bar(position = 'stack', show.legend=TRUE) 
p2<-ggplot(hireouti, aes(x = Hired, fill = C4)) + geom_bar(position = 'stack', show.legend=TRUE)
p3<-ggplot(hireouti, aes(x = Hired, fill = C5)) + geom_bar(position = 'stack', show.legend=TRUE)

multiplot(p1,p2,p3, cols = 3)

下面的第一个可视化是我得到的输出。我想获得像下面第二个这样的可视化。

没有图例的输出

期望的输出

我已经能够用图例重现可视化。

我已经执行了下面的代码,C1、C4、C5 是数字。我试过转换成因子。

p1<-ggplot(hireouti, aes(x = Hired, fill = factor(C1))) + geom_bar(position = 'stack', show.legend = TRUE) 
p2<-ggplot(hireouti, aes(x = Hired, fill = factor(C4))) + geom_bar(position = 'stack', show.legend = TRUE)
p3<-ggplot(hireouti, aes(x = Hired, fill = factor(C5))) + geom_bar(position = 'stack', show.legend = TRUE)
multiplot(p1,p2,p3, cols = 3) 

标签: rggplot2visualization

解决方案


推荐阅读