在ggplot中制作条形图,在x轴上带有垂直标签 [英] Making a bar chart in ggplot with vertical labels in x axis

查看:48
本文介绍了在ggplot中制作条形图,在x轴上带有垂直标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,亲爱的我正在尝试在ggplot中制作条形图,但我没有得到结果.数据帧是下一个:

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

我构建此data.frame是因为我想使用有序数据

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

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

我尝试使用ggplot,但是我的条形图没有与变量Indicador相关的垂直标签.我想在x轴变量Indicador和y轴变量Número中但是有了这段代码,我得到了一个丑陋的情节:

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

并且x轴上的所有标签仅在一行中.感谢您的帮助,有人可以帮助我如何在栏上添加颜色.

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.

推荐答案

使用函数 ggplot()可以更好地控制参数.

For better control of parameters used function ggplot().

首先,您应该根据Número对变量 Indicador 重新排序,以获得有序的小节. tabla $Número之前的减号表示相反的顺序(从最高到最低).

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)

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

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

建议:在出版物中,最好使用45度之类的东西:

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

这篇关于在ggplot中制作条形图,在x轴上带有垂直标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆