堆积和分组条形图与rg中的ggplot

问题描述 投票:0回答:1

大家好我想创建一个像图像的情节

我希望家庭成为家庭数字的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)

非常感谢你的帮助。

r ggplot2 plot
1个回答
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))

这是我使用基数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'))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.