我有这个简单的代码,试图绘制图形。我的意图是按照我的方式绘制x轴,即与order_num相同:从1:10到10+。但是,ggplot改变了我的订单。我怎样才能保留原始顺序,我将它们放在数据框中。
data_order=data.frame(order_num=as.factor(c(rep(1:10),"10+")),
ratio=c(0.18223,0.1561,0.14177,0.1163,0.09646,
0.07518,0.05699,0.04,0.0345,0.02668,0.006725))
ggplot(data_order,aes(x=order_num,y=ratio))+geom_bar(stat = 'identity')
答案 0 :(得分:1)
阅读数据:(注意删除as.factor
,我们将在下一步中执行此操作。这不是强制性的!)
data_order=data.frame(order_num=c(rep(1:10),"10+"),
ratio=c(0.18223,0.1561,0.14177,0.1163,0.09646,
0.07518,0.05699,0.04,0.0345,0.02668,0.006725))
您需要使用数据框而不是ggplot。
data_order$order_num <- factor(data_order$order_num, levels = data_order$order_num)
更改级别后,它将按预期进行。
ggplot(data_order,aes(x=order_num,y=ratio))+geom_bar(stat = 'identity')