I addition I need the bars in an descendant order in the graph excepting the "Otros" bar which I need in the last place (Bottom)
Cred_by_Prod<-structure(list(cultivo = c("GANADERIA CARNE", "SERVICIOS DE APOYO",
"ARROZ", "CAFÉ", "CAÑA DE AZUCAR", "AVICULTURA", "PALMA", "GANADERIA LECHE",
"FRUTALES", "OTROS CULTIVOS (Ciclo corto Perennes)", "ACTIVIDAD MIXTA AGROPECUARIA RURAL",
"PORCICULTURA", "CACAO", "PAPA", "BANANO", "PLATANO", "FLORES",
"HORTALIZAS", "AGUACATE", "ALGODÓN", "CAÑA PANALERA", "MAIZ",
"FORESTALES", "ACUICULTURA", "CITRICOS", "ACTIVIDADES RURALES",
"TABACO", "OTROS PECUARIOS APICULTURA", "YUCA", "CAUCHO", "ACTIVIDADES NO CLASIFICADAS"
), gr_sum = c(28.3254536384453, 22.4779943084809, 17.3083119473968,
16.5173053124414, 13.512530394183, 11.2419739653207, 9.8645607723738,
9.15579319695601, 6.21764754430497, 6.0597093441194, 3.40838697587753,
2.91394799943291, 2.530961953157, 2.250675318187, 2.248310268646,
1.915656141584, 1.80467388350462, 1.782433571294, 1.729078557108,
1.708796392104, 1.585391516969, 1.546049716909, 1.501875611076,
1.474594244825, 0.911062924627, 0.805293685917, 0.663811759298,
0.386514092089, 0.363622531613, 0.28208629398177, 0.042880323094
), prop = c(0.164169949441345, 0.130278979333187, 0.100316299734825,
0.0957317475886894, 0.0783165367783116, 0.0651567428033229, 0.0571734689206754,
0.0530655616473361, 0.0360365237578125, 0.035121138370864, 0.0197544838874844,
0.0168887920330538, 0.014669064128378, 0.0130445661316485, 0.0130308586702066,
0.0111028467866794, 0.0104596107795762, 0.0103307093689305, 0.0100214719567723,
0.00990391966455597, 0.00918868408985608, 0.00896066509996723,
0.00870463881301742, 0.00854652023263082, 0.00528037983726739,
0.00466735768436169, 0.00384735031443982, 0.00224017591268139,
0.00210749996778928, 0.00163492854208797, 0.000248527722246814
), prop_acum = c(0.164169949441345, 0.294448928774532, 0.394765228509357,
0.490496976098046, 0.568813512876357, 0.63397025567968, 0.691143724600356,
0.744209286247692, 0.780245810005504, 0.815366948376368, 0.835121432263853,
0.852010224296907, 0.866679288425285, 0.879723854556933, 0.89275471322714,
0.903857560013819, 0.914317170793395, 0.924647880162326, 0.934669352119098,
0.944573271783654, 0.95376195587351, 0.962722620973477, 0.971427259786495,
0.979973780019126, 0.985254159856393, 0.989921517540755, 0.993768867855195,
0.996009043767876, 0.998116543735665, 0.999751472277753, 1),
Cat_top = c("GANADERIA CARNE", "SERVICIOS DE APOYO", "ARROZ",
"CAFÉ", "CAÑA DE AZUCAR", "AVICULTURA", "PALMA", "GANADERIA LECHE",
"FRUTALES", "OTROS CULTIVOS (Ciclo corto Perennes)", "Otros",
"Otros", "Otros", "Otros", "Otros", "Otros", "Otros", "Otros",
"Otros", "Otros", "Otros", "Otros", "Otros", "Otros", "Otros",
"Otros", "Otros", "Otros", "Otros", "Otros", "Otros")), row.names = c(NA,
-31L), class = "data.frame")
Cred_by_Prod%>%
mutate(Cat_top = fct_reorder(Cat_top,gr_sum)) %>%
ggplot(aes(x=Cat_top,y=gr_sum)) #graficando cred por producto 2000-2021
geom_bar(stat="identity", alpha=.6, width=.4)
theme_bw()
coord_flip()
theme(axis.text.x = element_text(angle = 90))
labs(title="Colocaciones Finagro por Producto 2000-2021",
x="Cultivo",y="Billones $COP")
geom_label(label=round(Cred_by_Prod$gr_sum,1),nudge_x=0.1, nudge_y=-1,
check_overlap=T,label.padding=unit(0.1, "lines"),
label.size=0.0001,
color="black")
geom_label(label=paste(round(Cred_by_Prod$prop*100,1),"%"),nudge_x=0.1, nudge_y=2,
check_overlap=TRUE,
color="black",size=3)
I really appreciate your help. I am new in R
CodePudding user response:
Here is an option using group_by
and summarize
within the pipe leading to ggplot:
Cred_by_Prod%>%
mutate(Cat_top = fct_reorder(Cat_top,gr_sum)) %>%
group_by(Cat_top) %>%
summarize(gr_sum = sum(gr_sum))
# A tibble: 11 x 2
Cat_top gr_sum
<fct> <dbl>
1 Otros 31.9
2 OTROS CULTIVOS (Ciclo corto Perennes) 6.06
3 FRUTALES 6.22
4 GANADERIA LECHE 9.16
5 PALMA 9.86
6 AVICULTURA 11.2
7 CAÑA DE AZUCAR 13.5
8 CAFÉ 16.5
9 ARROZ 17.3
10 SERVICIOS DE APOYO 22.5
11 GANADERIA CARNE 28.3
Then you need to specify the label in aes
in geom_label
Cred_by_Prod%>%
mutate(Cat_top = fct_reorder(Cat_top,gr_sum)) %>%
group_by(Cat_top) %>%
summarize(gr_sum = sum(gr_sum)) %>%
ggplot(aes(x=Cat_top,y=gr_sum)) #graficando cred por producto 2000-2021
geom_bar(stat="identity", alpha=.6, width=.4)
theme_bw()
coord_flip()
theme(axis.text.x = element_text(angle = 90))
labs(title="Colocaciones Finagro por Producto 2000-2021",
x="Cultivo",y="Billones $COP")
geom_label(aes(label=round(gr_sum,1)),nudge_x=0.1, nudge_y=-1,
check_overlap=T,label.padding=unit(0.1, "lines"),
label.size=0.0001,
color="black")