我想重新排序ggplot
变量的类别。
我在变量类别之前专门添加了letters
来表示顺序。
因为我总结了其他变量,水平的顺序搞砸了。
value mean
1 a Sleep 539.72637
2 c Work 60.62189
3 e Travel/Commute 84.50249
4 f Cooking 54.24129
...............................
但它应该是
a Sleep
b Personal care
c Work
d Studies/library
...
以下是我的情节的汇总数据。
data = structure(list(value = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L,
21L, 22L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L), .Label = c("a Sleep",
"c Work", "e Travel/Commute", "f Cooking", "g Housework", "h Odd jobs",
"i Eating", "j Child care", "k Care for others", "m Leisure",
"u TV/Radio", "v Others", "t Visiting/Socialising", "p Telephone/Online Communication",
"b Personal care", "d Studies/library", "s Religious", "l Shopping",
"q Computing/Internet", "n Highbrow", "o Sport", "r Civic"), class = "factor"),
mean = c(539.726368159204, 60.6218905472637, 84.5024875621891,
54.2412935323383, 133.980099502488, 37.4378109452736, 92.773631840796,
39.6393034825871, 49.8880597014925, 55.3109452736318, 126.119402985075,
19.1417910447761, 61.6169154228856, 7.86069651741294, 3.70646766169154,
2.46268656716418, 6.00746268656716, 41.6417910447761, 5.08706467661692,
12.3880597014925, 4.27860696517413, 1.56716417910448, 531.689440993789,
118.086956521739, 92.5590062111801, 25.167701863354, 71.2422360248447,
67.7391304347826, 91.5776397515528, 27.0310559006211, 29.1801242236025,
62.583850931677, 161.391304347826, 20.1614906832298, 61.0434782608696,
4.6583850931677, 0.31055900621118, 3.21739130434783, 5.3416149068323,
32.1739130434783, 16.1614906832298, 10.5962732919255, 7.57763975155279,
2.29813664596273)), .Names = c("value", "mean"), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -44L))
你能帮我重新介绍一下这些类别的顺序吗?
我想我必须在某个地方使用desc
。
ggplot(data=data, aes(x= value, y=mean, fill = value )) +
geom_bar(stat="identity") + theme_minimal()
有些人proposed reorder
但这不是我想做的事情
ggplot(data=data, aes(x= reorder(value, mean), y=mean, fill = value )) +
geom_bar(stat="identity") + theme_minimal()
答案 0 :(得分:0)
使用'值'变量的排序级别重新调整变量(为了比较,我创建了一个新变量):
data$value2 <- factor(data$value, levels = sort(levels(data$value)))
ggplot(data=data, aes(x= value2, y=mean, fill = value2 )) +
geom_bar(stat="identity") + theme_minimal()