Mar*_*rta 7 r ggplot2 geom-bar
我想在闪避中订购酒吧geom_bar.你知道怎么处理吗?
我的代码:
ttt <- data.frame(typ=rep(c("main", "boks", "cuk"), 2),
klaster=rep(c("1", "2"), 3),
ile=c(5, 4, 6, 1, 8, 7))
ggplot()+
geom_bar(data=ttt, aes(x=klaster, y=ile, fill=typ),
stat="identity", color="black", position="dodge")
Run Code Online (Sandbox Code Playgroud)
示例图可以更好地理解问题:
是)我有的:

我想拥有什么:

一种选择是创建一个新变量来表示每个组中条形图的顺序,并将此变量添加为group绘图中的参数.
有很多方法来完成变量的任务,这里是使用dplyr函数的一种方法.新变量基于ile每个klaster组内降序排列.如果你在任何一个小组中都有联系,你会想知道在这种情况下你想做什么(这些小节应该用什么顺序给予关系?).您可能希望将ties.method参数设置为rank远离默认值,可能设置为"first"或"random".
library(dplyr)
ttt = ttt %>%
group_by(klaster) %>%
mutate(position = rank(-ile))
ttt
Source: local data frame [6 x 5]
Groups: klaster [2]
typ klaster ile rank position
(fctr) (fctr) (dbl) (dbl) (dbl)
1 main 1 5 3 3
2 boks 2 4 2 2
3 cuk 1 6 2 2
4 main 2 1 3 3
5 boks 1 8 1 1
6 cuk 2 7 1 1
Run Code Online (Sandbox Code Playgroud)
现在只需添加group = position到您的绘图代码中.
ggplot() +
geom_bar(data=ttt, aes(x=klaster, y=ile, fill=typ, group = position),
stat="identity", color="black", position="dodge")
Run Code Online (Sandbox Code Playgroud)