在传说或情节中改变顺序,但不能两者都改变



我有这些数据:

datat <- structure(list(Carga = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L), .Label = c("Outra", "88"), class = "factor"), 
    Categoria = structure(c(1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 3L, 
    3L, 2L, 2L), .Label = c("A", "G", "B"), class = "factor"), 
    Vagas = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("Ocupadas", "Autorizadas"), class = "factor"), 
    Cat.A.88 = c(26, 1, 30, 1, 18, 0, 57, 0, 39, 0, 0, 0)), .Names = c("Carga", 
"Categoria", "Vagas", "Cat.A.88"), class = "data.frame", row.names = c(NA, 
-12L))

和这个情节:

ggplot(datat, aes(x=Carga, y=Cat.A.88, fill=Vagas)) + geom_bar(stat='identity', position='dodge') + ylab('Vagas') + xlab('Carga horária') + facet_grid(. ~ Categoria) + coord_flip() 

图例颜色与图例颜色相反(图例绿色在红色之前,图例红色在绿色之前)。我要它们按同样的顺序排列。我尝试在aes()中添加参数order=-as.numeric(Vagas),但没有改变任何东西。

这应该有帮助:

ggplot(datat, aes(x=Carga, y=Cat.A.88, fill=Vagas)) + 
  geom_bar(stat='identity', position='dodge') + ylab('Vagas') + 
  xlab('Carga horária') + facet_grid(. ~ Categoria) + coord_flip() + 
  guides(fill = guide_legend(reverse=T))

最新更新