我正在尝试在ggplot2中绘制条形图。需要按照在数据帧中的方式对其进行排序。我知道要保持顺序,我需要将变量转换为一个因子,但是,这似乎不适用于我的数据。
这是我要绘制的数据帧的dput版本
> dput(tabla_an)
structure(list(Dominio = c("T_IND", "T_IND", "1040", "1100",
"1511", "1511", "1512", "1520", "2023", "2310", "2800", "2800",
"1010", "1010", "1100", "1900", "1900", "2100", "2220", "2220",
"2390", "2410"), Año = c(2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018,
2018, 2018, 2018, 2018, 2018), Mes = c(4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4), Clase = structure(c(37L,
37L, 34L, 6L, 12L, 12L, 7L, 8L, 16L, 40L, 17L, 17L, 9L, 9L, 6L,
36L, 36L, 33L, 32L, 32L, 18L, 14L), .Label = c("Aceites y grasas",
"Actividades de impresión", "Alimentos para animales", "Aparatos y equipo eléctrico",
"Azúcar y panela", "Bebidas", "Bolsos y similares en cuero",
"Calzado", "Carne y pescado", "Carrocerías para vehículos", "Chocolate y confitería",
"Curtido de cueros", "Hilaturas, tejidos y textiles", "I. básicas de hierro y acero",
"I. básicas metales preciosos", "Jabones, deterg. y perfumes",
"Maquinaria y equipo", "Minerales no metálicos", "Muebles y colchones",
"Otras industrias", "Otros equipos de transporte", "Otros prod. alimenticios",
"Otros productos químicos", "Papel y cartón", "Partes y accesorios vehículos",
"Prendas de vestir", "Productos de caucho", "Productos de madera",
"Productos de metal", "Productos de molinería", "Productos de panadería",
"Productos de plástico", "Productos farmacéuticos", "Productos lácteos",
"Química básica", "Refinación de petróleo", "Total Industria",
"Trilla de café", "Vehículos y sus motores", "Vidrio y sus productos"
), class = "factor"), variable = c("Producción Real", "Ventas Reales",
"Producción Real", "Ventas Reales", "Producción Real", "Ventas Reales",
"Producción Real", "Ventas Reales", "Ventas Reales", "Producción Real",
"Producción Real", "Ventas Reales", "Producción Real", "Ventas Reales",
"Producción Real", "Producción Real", "Ventas Reales", "Producción Real",
"Producción Real", "Ventas Reales", "Ventas Reales", "Ventas Reales"
), value = c(10.4514055804005, 8.27782059333249, -0.250198939619928,
-8.74242004296935, -9.9154316234509, -3.03175596023642, -0.144133952371017,
-0.361452269938989, -2.30416781528587, -0.894989019568837, -8.81710222452629,
-2.29533431715434, 16.9952305679959, 12.936870879628, 13.716747116265,
8.83772048845031, 9.83023540140342, 15.7709569477986, 15.3753710735845,
13.9731453790821, 10.6163559446309, 30.6402565702231), value_ac = c(2.79381474010882,
3.95278968520578, 1.55473909567014, 1.18768502560165, -25.1515941361191,
-20.893186511086, 2.68693288594819, -4.56035152341705, -0.0790394397418459,
-0.930541066639634, -9.36021147098659, -5.65888144752885, 9.31874606292218,
10.564099789443, 6.56279602310872, 6.00195191932449, 8.94524739544196,
0.86998919220429, 0.253882663245042, 0.631504043521791, -3.72207995467949,
12.0227782275297), ponder = c(1, 1, 0.0357132548720255, 0.107919834853296,
0.000454261034583859, 0.000410071121754424, 0.00188010608774883,
0.00498907855522448, 0.0386842021393349, 0.0106486865746522,
0.0118508879267037, 0.0104501572507955, 0.0339285896411869, 0.0352009971606078,
0.105194338889698, 0.211079774012972, 0.233624799512387, 0.0441910788340747,
0.0414846849225406, 0.0387966466343206, 0.0601313043015465, 0.0236147326568741
), ponder_ac = c(1, 1, 0.0341009210225283, 0.106927877696523,
0.000402636316055208, 0.000458363298724155, 0.00150815607624677,
0.00400673600994054, 0.0356510426986507, 0.00897863033286804,
0.00892728947398616, 0.00827182789772384, 0.0362837018499556,
0.0386095103730992, 0.102862348625654, 0.235816967481456, 0.239840611205947,
0.0372454802400577, 0.0421676913250118, 0.0409587164741937, 0.0657768006854785,
0.0307060443780067), contrib = c(10.4514055804005, 8.27782059333249,
-0.00893541849935701, -0.9434805272554, -0.00450419422761432,
-0.00124323556749981, -0.000270987121304049, -0.00180331376868982,
-0.0891348935294683, -0.00953045755714384, -0.104490490301151,
-0.0239866045574102, 0.576624203798891, 0.455390755100934, 1.44292414461267,
1.86546404349191, 2.29658677481244, 0.696935601768966, 0.637842424554798,
0.542111183642237, 0.63837532988013, 0.723561467443848), contrib_ac = c(2.79381474010882,
3.95278968520578, 0.0530180351120847, 0.126996639159525, -0.0101269452058828,
-0.00957666989008037, 0.00405231415841003, -0.0182721246668623,
-0.00281783844111398, -0.00835498424691, -0.0835613173392231,
-0.046809293427581, 0.338118603762516, 0.407874720402954, 0.675064612488068,
1.41536210058461, 2.1454336027112, 0.0324031652673086, 0.0107056457764888,
0.0258655950709159, -0.244826511314368, 0.369171961801459)), row.names = c(NA,
-22L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = c("Dominio",
"variable"), indices = list(12L, 13L, 2L, 14L, 3L, 4L, 5L, 6L,
7L, 15L, 16L, 8L, 17L, 18L, 19L, 9L, 20L, 21L, 10L, 11L,
0L, 1L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
Dominio = c("1010", "1010", "1040", "1100", "1100", "1511",
"1511", "1512", "1520", "1900", "1900", "2023", "2100", "2220",
"2220", "2310", "2390", "2410", "2800", "2800", "T_IND",
"T_IND"), variable = c("Producción Real", "Ventas Reales",
"Producción Real", "Producción Real", "Ventas Reales", "Producción Real",
"Ventas Reales", "Producción Real", "Ventas Reales", "Producción Real",
"Ventas Reales", "Ventas Reales", "Producción Real", "Producción Real",
"Ventas Reales", "Producción Real", "Ventas Reales", "Ventas Reales",
"Producción Real", "Ventas Reales", "Producción Real", "Ventas Reales"
)), row.names = c(NA, -22L), class = "data.frame", vars = c("Dominio",
"variable")))
您可以检查一下,Clase是一个因素
> class(tabla_an$Clase)
[1] "factor"
但是图形并不以数据帧排序的方式出现
tabla_an %>% filter(variable == "Producción Real") %>%
ggplot(aes(y = value, x = Clase)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
谢谢!