2013-03-26 114 views
9

嗨,亲爱的我正在试图在ggplot中制作条形图,但我没有得到结果。数据帧是下一个:在ggplot中制作条形图,在x轴上有垂直标签

z=data.frame(x1=read.table(textConnection(" 
Indicador 
Total 
Max. 
          Min. 
          Mean 
          Promedio.Aparatos 
          Promedio.Automotriz 
          Promedio.Belleza 
          Promedio.C.Internet 
          Promedio.Comp 
          Promedio.Deportes 
          Promedio.Educación 
          Promedio.Entretenimiento 
          Promedio.Gasolina 
          Promedio.C.Comerciales 
          Promedio.ATMs 
          Promedio.Hogar 
          Promedio.Libros.y.Música 
          Promedio.Moda 
          Promedio.Pagos.e.Impuestos 
          Promedio.Salud 
          Promedio.Servicios.Varios 
          Promedio.Supermercados 
          Promedio.Telefonia 
          Promedio.Viajes 
          Porcentaje.Aparatos 
          Porcentaje.Automotriz 
          PorcentajeBelleza 
          PorcentajeCompras.en.Internet 
          PorcentajeComputación 
          PorcentajeDeportes 
          PorcentajeEducación 
          PorcentajeEntretenimiento 
          PorcentajeGasolina 
          PorcentajeCentros.Comerciales 
          PorcentajeATMs 
          PorcentajeHogar 
          PorcentajeLibros.y.Música 
          PorcentajeModa 
          PorcentajePagos.e.Impuestos 
          PorcentajeSalud 
          PorcentajeServicios.Varios 
          PorcentajeSupermercados 
          PorcentajeTelefonia 
          PorcentajeViajes 
          "),header=T), 
x2=read.table(textConnection(" 
Número 
36001 
35916 
          12320 
          35889 
          4487 
          2751 
          673 
          1023 
          1062 
          4602 
          824 
          4438 
          4021 
          2577 
          31845 
          5443 
          641 
          6982 
          32868 
          4696 
          1594 
          9746 
          6239 
          13170 
          3973 
          2526 
          540 
          834 
          964 
          4291 
          755 
          3627 
          3254 
          2186 
          30356 
          4855 
          488 
          6612 
          33079 
          4105 
          1314 
          9284 
          5777 
          9666 
          "),header=TRUE)) 

我建这个data.frame因为我想和有序的数据

tabla=z[order(z$Número,decreasing=TRUE),] 

我与ggplot努力工作,但我没有得到我的条形图与变量Indicador相关的垂直标签。我想在x轴可变Indicador和Y轴可变NÚMERO 但与此代码我得到一个丑陋的阴谋:

qplot(Indicador, data = tabla, geom = "bar") 

而在x轴的所有标签都只有一行。 感谢您的帮助,并且有人能帮助我如何将颜色添加到酒吧中。

回答

33

为了更好地控制所使用的参数功能ggplot()

首先,您应该根据Número重新订购变量Indicador以获得有序的条形图。 tabla$Número之前的减号表示反向顺序(从最高到最低)。

tabla$Indicador<-reorder(tabla$Indicador,-tabla$Número) 

那么你应该提供x和y的值,并使用stat="identity"geom_bar()内绘制的实际值。使用theme()axis.text.x=您可以更改文本方向,还可以调整文本在x轴下的垂直和水平位置。

ggplot(tabla,aes(Indicador,Número))+ 
    geom_bar(stat="identity")+ 
    theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) 

enter image description here

建议:在出版物中,它看起来更好地使用类似45度:

theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))