Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a bar chart in ggplot with vertical labels in x axis

Tags:

r

ggplot2

Hi dear I was triying to make a bar chart in ggplot but I don't get the result. The data frame is the next:

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))

I built this data.frame because I want to work with ordered data

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

I was trying with ggplot but I don't get my bar chart with vertical labels related to variable Indicador. I would like in x axis variable Indicador and in y axis variable Número but with this code I get an ugly plot:

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

And all labels in x axis are in only one line. Thanks for your help and somebody could you help me how I can to put colour in the bars.

like image 367
Duck Avatar asked Mar 26 '13 04:03

Duck


1 Answers

For better control of parameters used function ggplot().

First, you should reorder your variable Indicador according to Número to get ordered bars. Minus sign before tabla$Número means reverse order (from highest to lowest).

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

Then you should provide x and y values and use stat="identity" inside the geom_bar() to plot actual values. With theme() and axis.text.x= you can change text direction and also adjust vertical and horizontal position of texts under x axis.

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

Suggestion: In publications, it looks better to use something like 45 degree:

theme(axis.text.x=element_text(angle=45,hjust=1,vjust=0.5))
like image 118
Didzis Elferts Avatar answered Oct 09 '22 11:10

Didzis Elferts