r - 在 ggplot 中制作带有 x 轴垂直标签的条形图

标签 r ggplot2

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

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 轴上的所有标签都在一行中。
感谢您的帮助,有人可以帮助我如何在条形图中添加颜色。

最佳答案

为了更好地控制使用的参数ggplot() .

首先,您应该重新排序您的变量 Indicador根据 Número得到有序的酒吧。前面的减号 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))

关于r - 在 ggplot 中制作带有 x 轴垂直标签的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629192/

相关文章:

r - 应该在函数之前还是在函数内部加载数据集?

html - 如何从 <span> 和 </span> 之间的 html 中检索数据

r - 如何从大量变量的图中提取截距和斜率

r - 使用ggplot时,保持与数据文件中的顺序相同

r - 无法从 Shiny 下载 .png 文件

r - 如何在图表中添加大括号?

r - 如何在 R 中设置 plotly 图表以具有透明背景?

r - ggplot2 中 glm 和 stat_smooth 的逻辑回归预测值不同

r - 文本层在 ggplot 中工作,但用 ggplotly 删除

r - 使用 facet wrap 和 scales free 用 geom_sf 映射不同的状态