0

I'd like to plot the decrease the number Area_atacada in a barplot without success.

# Library
library(ggplot2)
library(dplyr)

ds<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/order_pred.csv")
str(ds)

# 'data.frame': 147 obs. of  4 variables:
#  $ ID_UNIQUE   : chr  "ALTOALEGREII_001A" "ALTOALEGREII_001A" "ANTONIOHELIOREISSEVERO_007A" "AROEIRAA_068B" ...
#  $ DATA_S2     : chr  "2021-10-22" "2021-10-27" "2021-10-22" "2021-10-24" ...
#  $ Area_atacada: num  0.25 0.32 0.1 0.84 0.86 0.01 0.02 0.49 0.14 0.05 ...
#  $ ID          : chr  "ALTOALEGREII_001A / 2021-10-22" "ALTOALEGREII_001A / 2021-10-27" "ANTONIOHELIOREISSEVERO_007A / 2021-10-22" "AROEIRAA_068B / 2021-10-24" ...

# Organizing the data by decresing `Area_atacada` values and select the first 5
        pred_attack_all_F <- ds %>%
           arrange(desc(Area_atacada)) %>%
           head(., 5)
        pred_attack_all_F

#            ID_UNIQUE    DATA_S2 Area_atacada                              ID
# 1      FERRARIA_005B 2021-10-24         5.29      FERRARIA_005B / 2021-10-24
# 2 CERRODOBATOVI_001C 2021-10-22         4.77 CERRODOBATOVI_001C / 2021-10-22
# 3   SAOCIPRIANO_011A 2021-10-24         2.83   SAOCIPRIANO_011A / 2021-10-24
# 4 FAZENDATARUMA_007C 2021-10-22         2.58 FAZENDATARUMA_007C / 2021-10-22
# 5        PECCIN_059A 2021-10-24         1.69        PECCIN_059A / 2021-10-24


# Create the barplot
        ggplot() +
           geom_bar(data=pred_attack_all_F,aes(x=ID,y=Area_atacada),stat="identity", fill="#f68060", alpha=.6, width=.4) +
           coord_flip() +
           xlab("") +
           theme_bw()+ theme(plot.title = element_text(size = 18, face = "bold"))

my-plot

Despite the pred_attack_all_F being well organized, the plot does not show the x=ID started in FERRARIA_005B / 2021-10-24 and I don't know whats happened. Please any help for me to fix this?

Leprechault
  • 1,531
  • 12
  • 28
  • 2
    The order in your dataset does not matter for the ordering of the bars. By default ggplot will order alphabetically. To avoid that you could use `x = reorder(ID, Area_atacada)` to order ID by the value of Area_atacada. – stefan Nov 15 '21 at 19:39
  • 1
    Possible duplicate: https://stackoverflow.com/questions/3253641/order-discrete-x-scale-by-frequency-value – stefan Nov 15 '21 at 19:52

1 Answers1

0

By default ggplot will order alphabetically and to avoid that you could use x = reorder(ID, Area_atacada):

ggplot() +
   geom_bar(data=pred_attack_all_F,aes(x=reorder(ID, Area_atacada),y=Area_atacada),stat="identity", fill="#f68060", alpha=.6, width=.4) +
   coord_flip() +
   xlab("") +
   theme_bw()+ theme(plot.title = element_text(size = 18, face = "bold"))

my-plot2

Leprechault
  • 1,531
  • 12
  • 28