在图中改变图例中的顺序,但不是两者都改变 [英] Changing order in legend OR in plot, but not both
本文介绍了在图中改变图例中的顺序,但不是两者都改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pre $ lt; code> datat< - structure(list(Carga = structure(c(2L,1L, 2L,1L,2L,1L,2L,
1L,2L,1L,2L,1L),。标签= 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)标签= 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('Cargahorária')+ facet_grid(。〜Categoria)+ coord_flip()
图例颜色在inv中如果与绘图颜色相比,则为erse顺序(绘图在红色前为绿色,而图例在绿色前为红色)。我希望他们以相同的顺序出现。我尝试在 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('Cargahorária')+ facet_grid(。〜Categoria)+ coord_flip()+
guides(fill = guide_legend(reverse = T))
I have this data:
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))
and this plot:
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()
The legend colours are in inverse order if compared with plot colours (plot have green before red, and legend have red before green). I want they appers in the same order. I tried add the parameter order=-as.numeric(Vagas)
in aes()
, but didn't changed anything.
解决方案
This should help:
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))
这篇关于在图中改变图例中的顺序,但不是两者都改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文