这是数据集: https://www.dropbox.com/s/mrlfnh6e2ww1xwd/home.csv?dl=0
这是我的代码:
hom <- read.csv(file.choose(),header=TRUE)
home.melt <- melt(hom, id.vars='home')
ggplot(home.melt,
aes(x = reorder(home, value), y = value,
fill=forcats::fct_rev(variable))) +
geom_bar(stat = "identity",width = 0.8) + coord_flip() +
theme_minimal(base_size=10) +
labs(title="Home time",
subtitle="By matches",
x="Home",
y="time (minutes)",
fill=" ")
这是输出:
如您所见,它不是按降序排列的。
最佳答案
关键是在重新排序的调用中指定函数:
reorder(home, value, FUN = sum)
默认是“均值”
ggplot(home.melt,
aes(x = reorder(home, value, FUN = sum), y = value,
fill=forcats::fct_rev(variable))) +
geom_bar(stat = "identity",width = 0.8) + coord_flip() +
theme_minimal(base_size=10) +
labs(title="Home time",
subtitle="By matches",
x="Home",
y="time (minutes)",
fill=" ")
关于r - 使用 ggplot 的条形图重新排序无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46103521/