Home > Software design >  Reorder ggplot in decreasing and show only top 5 count
Reorder ggplot in decreasing and show only top 5 count

Time:10-02

ggplot(profiles)  
 aes(x = Location, fill = Gender)  
 geom_bar(position = "dodge")  
 scale_fill_hue(direction = 1)  
 labs(title = "Locations of Users")  
 theme_light()  
 theme(plot.title = element_text(face = "bold", 
 hjust = 0.5), axis.title.y = element_text(face = "bold"), axis.title.x = element_text(face = "bold"))

How do I edit this so it sort by decreasing order on my plot and show only top 5 counts?

CodePudding user response:

Simulating Your Data

I'm not totally sure what your data structure is (you should provide a minimally reproducible dataset next time), but I have simulated a dataset I think may be similar and you can try to tinker with this to see if it works for you. First, I set the seed to a random number and made up a fake dataset below that may be reproducible:

#### Set Seed for Replication ####
set.seed(123)

#### Create Data ####
profiles <- data.frame(Gender = round(rbinom(n=1000,
                                 size=1,
                                 prob = .5)),
                 Location = round(rbinom(n=1000,
                                       size=6,
                                       prob = .5))) %>% 
  mutate(Gender = as.factor(ifelse(Gender == 0,
                         "Male",
                         "Female")),
         Location = as.factor(ifelse(Location == 0,
                           "Farm",
                           ifelse(Location == 1,
                                  "Zoo",
                                  ifelse(Location == 3,
                                         "School",
                                         ifelse(Location == 4,
                                                "Library",
                                                ifelse(Location == 5,
                                                       "Prison",
                                                       "Factory")))))))

You can check the first 10 lines of data with this:

head(profiles,10) # check data

Which should look something similar to this:

   Gender Location
1    Male  Factory
2    Male   Prison
3    Male   School
4  Female   School
5    Male  Factory
6  Female  Library
7    Male  Library
8    Male   School
9    Male  Library
10   Male  Library

Checking Counts

Then from there I checked to see what the counts were in descending order:

#### Plot Arranged Bar ####
profiles %>% 
  group_by(Location) %>% 
  count() %>% 
  arrange(desc(n))  # Farm has lowest count, Zoo next

Shown below:

# A tibble: 6 × 2
# Groups:   Location [6]
  Location     n
  <fct>    <int>
1 School     301
2 Factory    253
3 Library    232
4 Prison     105
5 Zoo         92
6 Farm        17

From there I reordered the levels by count:

profiles$Location <- factor(profiles$Location,
                            levels = c("School",
                                       "Factory",
                                       "Library",
                                       "Prison",
                                       "Zoo",
                                       "Farm"))

Plotting Data

Finally I plotted by first filtering Farm out, then using the new reordering:

#### Plot Desc Order ####
profiles %>% 
  filter(!Location == "Farm") %>%
  ggplot(aes(x =Location, 
      fill = Gender))  
  geom_bar(position = "dodge")  
  scale_fill_hue(direction = 1)  
  labs(title = "Locations of Users")  
  theme_light()  
  theme(plot.title = element_text(face = "bold", 
                                  hjust = 0.5), 
        axis.title.y = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"))

Which should give you something like this:

enter image description here

CodePudding user response:

I believe the below code chunk is a bit tidier, based on Shawn's simulated dataset.

profiles %>%  
  group_by(Location, Gender) %>% 
  summarise(total = n()) %>% 
  ungroup() %>% 
  mutate(Location = fct_reorder(Location, total, .desc = TRUE)) %>% 
  arrange(Location) %>% 
  filter(!Location == last(Location)) %>%  
  ggplot(aes(x = Location, y = total,  fill = Gender))  
  geom_col(position = "dodge")  
  scale_fill_hue(direction = 1)  
  labs(title = "Locations of Users")  
  theme_light()  
  theme(plot.title = element_text(face = "bold", hjust = 0.5), 
        axis.title.y = element_text(face = "bold"),
        axis.title.x = element_text(face = "bold"))
  • Related