我编写了一个使用 ggplot
函数获取比例堆叠条形图的函数。目前我在此 ID
中使用列名。
PropBarPlot<-function(df, mytitle=""){
melteddf<-melt(df, id="ID", na.rm=T)
ggplot(melteddf, aes(ID, value, fill=variable)) +
geom_bar(position="fill") +
theme(axis.text.x = element_text(angle=90, vjust=1)) +
labs(title=mytitle)
}
我希望让它更通用。因此,我想使用列索引而不是列名。我尝试了以下代码:
PropBarPlot<-function(df, mytitle=""){
melteddf<-melt(df, id=names(df)[1], na.rm=T)
ggplot(melteddf, aes(names(df)[1], value, fill=variable)) +
geom_bar(position="fill") +
theme(axis.text.x = element_text(angle=90, vjust=1)) +
labs(title=mytitle)
}
但是没有用。有人能建议我如何做吗?谢谢。
aes_string(names(df)[1])
。 - baptiste