这是我的data.frame:
names order rate chance_level
Neutral 1 77% 0.076923077
Blaming 2 66% 0.076923077
Insincere 3 61% 0.076923077
Polite 4 59% 0.076923077
Commanding 5 58% 0.076923077
Prasing 6 57% 0.076923077
Friendly 7 48% 0.076923077
Sincere 8 46% 0.076923077
Joking 9 39% 0.076923077
Hostile 10 39% 0.076923077
Rude 11 36% 0.076923077
Serious 12 33% 0.076923077
Suggestion 13 16% 0.076923077
这是我的代码:
ggplot(data = data, aes(x = order)) +
geom_bar(aes(y = rate), stat = "identity", fill = "grey") +
scale_x_continuous(breaks = c(1:13),
labels = c("Reluctant","Joking","Suggestion","Blaming", "Neutral","Seriously","Command","Prasing", "Frankly","Friendly","Polite","Hostile", "Rude"))
因此,如您所见,我必须以正确的顺序手动输入x轴的刻度标签,因此,如何使用“名称”列而不是这种白痴方式以正确的顺序添加刻度标签?
技巧是将您的列names
变成一个因子,其级别顺序由确定order
。
您可以使用该函数reorder()
执行以下操作:
dat$names <- reorder(dat$names, dat$order)
然后绘制:
ggplot(data = dat, aes(x = names, y = rate)) +
geom_bar(stat = "identity", fill="grey")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句