大家好,我正在尝试创建类似图片的情节
我希望住户是x轴,住户编号,而ELI,VLI,LI和MI是y轴值。如示例中所示,应将ELI,VLI,LI和MI值相加。
household<-c("Extremely low income", "Very low income", "Low income",
"Middle income")
householdnumbers<-c("3,000","3,015","5,175","12,255")
ELI<-c("1,885","1,885","1,885","1,885")
VLI<-c("0","3,500","3,500","3,500")
LI<-c("0","0","14,385","14,385")
MI<-c("0","0","0","5,085")
housing<-data.frame(household,householdnumbers,ELI,VLI,LI,MI)
非常感谢您的帮助。
答案 0 :(得分:0)
尝试一下:
m<-melt(housing, id.vars = "household", measure.vars = c("ELI", "VLI","LI","MI"))
m$household <- factor(m$household, levels = c("Extremely low income", "Very low income", "Low income", "Middle income"))
ggplot(data = m, aes(x = household, y = value, fill = variable)) + geom_bar(stat = 'identity', position = 'stack')+ theme(axis.text.x = element_text(angle = 90))
这是我使用base r编辑的版本:
householdnumbers<-c(3000,3015,5175,12255)
ELI<-c(1885,1885,1885,1885)
VLI<-c(0,3500,3500,3500)
LI<-c(0,0,14385,14385)
MI<-c(0,0,0,5085)
housing<-data.frame(household,householdnumbers,ELI,VLI,LI,MI)
barplot(t(housing[,!names(housing) %in% c("householdnumbers","household")]),space = 2,width = 0.5,names.arg=household,legend.text =c("ELI","VLI","LI","MI"),col=c("red","green","pink","blue"),args.legend = list(x = 'topleft'))
barplot(as.vector(housing[,names(housing) %in% c("householdnumbers")]), space = c(3,2,2,2), width = 0.5,add=T,args.legend = list(x = 'left'))