【发布时间】:2013-04-17 17:09:32
【问题描述】:
我编写了一个函数来使用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 错误:无法将类“uneval”强制转换为 data.frame
-
如果没有包含数据的可重现示例,很难判断