业余R用户在这里。我在网上看起来很难,看看这个问题是否已被回答/询问,但我还没有找到一个好的答案。我无法发布图片,因为我没有10个“声望”在数据框中按因子排序堆积条形图
我想要一个堆积条形图,它根据摄取路线的贡献百分比(按降序)对x变量进行排序。
Percent<-c(0.4,0.75,0.8, 0.3,0.1,0.6,0.25,0.5)
Inh<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Inhalation"), Percent=Percent)
Ing<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Ingestion"), Percent=1-Percent)
df<-data.frame(rbind(Inh,Ing))
ggplot(df,aes(x=ID,y=Percent,fill=Route))+ geom_bar(stat="identity")+
facet_wrap(~Age, scales = "free_x") +
ylab("Percent Contribution") +
labs(title = "Route Contribution to Exposure by Age Groups")
但我希望它看起来像这样,我手动嘲笑了:
Percent<-c(0.1,0.6,0.25, 0.3,0.4,0.75,0.8,0.5)
Inh<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Inhalation"), Percent=Percent)
Ing<-data.frame(ID=c(rep(1,4),rep(2,4)),Age=factor(rep(1:4,2), label=c("0-1 year", "1-2 years", "2-3 years","3-6 years")), Route=factor(rep(1), label="Ingestion"), Percent=1-Percent)
df<-data.frame(rbind(Inh,Ing))
ggplot(df,aes(x=ID,y=Percent,fill=Route))+ geom_bar(stat="identity")+
facet_wrap(~Age, scales = "free_x") +
ylab("Percent Contribution") +
labs(title = "Route Contribution to Exposure by Age Groups")
预先感谢您!
更新:感谢罗兰,我有一个情节!虽然问题仍然清晰。对于那些有兴趣在这里的代码和最终产品:
ggplot(df,aes(x=id2,y=Percent,fill=Route, width=1,order = -as.numeric(Route)))+
geom_bar(stat="identity")+
facet_wrap(~Age, scales = "free_x") +
xlab(" ")+
ylab("Percent Contribution") +
theme(axis.text.x = element_blank(), axis.ticks.x= element_blank()) +
labs(title = "DEHP Route Contribution to Exposure by Age Groups")
对于图片,你可以把它作为链接,并有更高的声誉的人可以添加它。 – agstudy
在线放置图像或图像:即您的输入提供给您的图像和模拟您希望产生的图像。 –