stats_noob
stats_noob

Reputation: 5897

R: Changing the Order of Bars in a Graph

I am working with the R programming language.

I found this tutorial here (https://rpubs.com/chidungkt/931948) that I am trying to replicate - the data for this tutorial is in the following format:

> df_for_graph1
# A tibble: 16 x 3
   Univ_Name    type         n
   <fct>        <fct>    <dbl>
 1 Cornell      Accepted  5288
 2 Cornell      Rejected 46040
 3 Dartmouth    Accepted  1925
 4 Dartmouth    Rejected 20108
 5 Pennsylvania Accepted  3731
 6 Pennsylvania Rejected 40751
 7 Brown        Accepted  2566
 8 Brown        Rejected 32872
 9 Yale         Accepted  2229
10 Yale         Rejected 33077
11 Columbia     Accepted  2214
12 Columbia     Rejected 37989
13 Princeton    Accepted  1941
14 Princeton    Rejected 33429
15 Harvard      Accepted  1962
16 Harvard      Rejected 40787

I tried to create my own data for this problem:

Univ_Name = c("Uni1", "Uni1", "Uni2", "Uni2", "Uni3", "Uni3", "Uni4", "Uni4", "Uni5", "Uni5", "Uni6", "Uni6")
type = c("Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected")
n = c(0.80, 0.2, 0.5, 0.5, 0.3, 0.7, 0.1, 0.9, 0.6, 0.4, 0.2,0.8)
df_for_graph1 = data.frame(Univ_Name, type, n)

I then tried to continue the rest of the tutorial:

library(rvest)
library(magrittr)
library(tidyverse)


# Colors for our plot: 

color_cases_text <- "#258BC3"

color_accepted <- "#377eb8"
color_rejected <- "#CE3240" 

bgr_color <- "#EFF2F4"

library(showtext) # -> Package for using extra fonts. 

font_subtitle <- "Roboto Condensed"  # -> Set Outfit font for our plot. 

font_add_google(name = font_subtitle, family = font_subtitle) # -> Load font for using. 

font_text <- "Oswald"

font_add_google(name = font_text, family = font_text)

font_main <- "Ubuntu"  

font_add_google(name = font_main, family = font_main) 

# Automatically render text: 
showtext_auto()

#------------------------
#       Bar Chart
#------------------------

labels_on_x <- str_c(seq(0, 100, 10), "%")

df_for_graph1 %>% 
  filter(type == "Accepted") %>% 
  mutate(text = n, n = 0.04) -> df_text

library(ggtext)

p_title <- "Ivy League Acceptance Rates 2022"

p_caption <- "Source: IVY COACH | Graphic Designer: Nguyen Chi Dung"

p_subtitle <- "Harvard is not just the most selective of the Ivies, but it typically ranks as the most selective university <br> in the United States. Harvard <b style = 'color:#3e6487'> accepted 1962 students </b> or <b style = 'color:#CE3240'> 4.6 percent of the 42749 people </b> who applied<br>in the academic year 2022. Overall, Ivy League Colleges had an average acceptance rate of only 7.1%"
 
df_for_graph1 %>% 
  ggplot(aes(y = Univ_Name, x = n, fill = type)) + 
  geom_col(position = "fill", width = 0.75) + 
  theme_minimal() + 
  theme(legend.position = "top") + 
  theme(axis.title = element_blank()) + 
  theme(panel.grid.minor = element_blank()) + 
  scale_x_continuous(expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = labels_on_x) + 
  labs(title = p_title, subtitle = p_subtitle, caption = p_caption) +   
  theme(plot.subtitle = element_markdown(size = 11, color = "grey20", family = font_subtitle)) +  
  theme(plot.title = element_text(family = font_main, size = 18, face = "bold", color = "grey10")) +  
  theme(plot.caption = element_text(family = font_subtitle, size = 10.5, color = "grey50", vjust = -1)) + 
  theme(plot.margin = unit(c(0.5, 0.8, 0.5, 0.5), "cm")) + 
  theme(axis.text = element_text(family = font_subtitle, size = 12, color = "grey25")) + 
  scale_fill_manual(values = c(Rejected = color_rejected, Accepted = color_accepted)) + 
  theme(panel.grid.major = element_line(color = "grey70")) + 
  theme(legend.title = element_blank()) +  
  theme(legend.text = element_text(size = 11, color = "grey20", family = font_subtitle)) + 
  theme(legend.key.height = unit(0.4, "cm")) + 
  theme(legend.key.width = unit(0.4, "cm")) +  
  guides(fill = guide_legend(reverse = TRUE)) + 
  geom_text(data = df_text, aes(label = text), 
            hjust = 0.97, color = "white", 
            family = font_text, size = 3.5) + 
  theme(plot.background = element_rect(fill = bgr_color, color = NA)) 

enter image description here

The two issues I am trying to fix with this graph:

I figure that it is probably easier to re-arrange the data frame (i.e. df_for_graph1) compared to changing the R code. I manually trying to do this right now:

Univ_Name = c("Uni6", "Uni6", "Uni5", "Uni5", "Uni4", "Uni4", "Uni3", "Uni3", "Uni2", "Uni2", "Uni1", "Uni1")

type = c("Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted", "Rejected", "Accepted",)

df_for_graph1 = data.frame(Univ_Name, type, n)

But is there a quicker way to change this?

Thanks!

Upvotes: 0

Views: 51

Answers (1)

MarBlo
MarBlo

Reputation: 4524

Your first issue: you can reorder the axis completely to your desire with fct_relevel in which you can define the order.

The second issue: you have filtered the little helperDF for Accepted. Changing this to Rejected will give you the right text in the red bars.

I have a few design features of yours left out in my plot

df_for_graph1 %>% 
  filter(type == "Rejected") %>% 
  mutate(text = n, n = 0.04) -> df_text

df_for_graph1 %>% 
  mutate(Univ_Name = fct_relevel(
    Univ_Name, 'Uni2', 'Uni3', 'Uni5', 'Uni4',
      'Uni6', 'Uni1')) |> 
  ggplot(aes(y = Univ_Name, x = n, fill = type)) + 
  geom_col(position = "fill", width = 0.75) + 
  theme_minimal() +
  theme(legend.position = "top") + 
  theme(axis.title = element_blank()) + 
  theme(panel.grid.minor = element_blank()) + 
  scale_x_continuous(expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = labels_on_x) + 
  labs(title = p_title, subtitle = p_subtitle, caption = p_caption) +   
  theme(plot.margin = unit(c(0.5, 0.8, 0.5, 0.5), "cm")) + 
  scale_fill_manual(values = c(Rejected = color_rejected, Accepted = color_accepted)) + 
  theme(panel.grid.major = element_line(color = "grey70")) + 
  theme(legend.title = element_blank()) +  
  theme(legend.key.height = unit(0.4, "cm")) + 
  theme(legend.key.width = unit(0.4, "cm")) +  
  guides(fill = guide_legend(reverse = TRUE)) + 
  geom_text(data = df_text, aes(label = text), 
            hjust = 0.97, color = "white", 
            size = 3.5) + 
  theme(plot.background = element_rect(fill = bgr_color, color = NA)) 

Upvotes: 1

Related Questions