Shakir
Shakir

Reputation: 345

ggplot2: unable to sort x axis based on custom ordering of factor variable

I am trying to create a bar plot with custom ordering of x axis (factor variable). After searching around for about 3 hours, I think I should ask for help directly.

library(dplyr)
library(ggplot2)
library(stringr)
library(extrafont) #extrafont::font_import()
library(scales)
library(gridExtra)
library(ggrepel)
get_wraper <- function(width) {
  function(x) {
    lapply(strwrap(x, width = width, simplify = FALSE), paste, collapse="\n")
  }
}
graph_object <- function(d, font="Verdana", facet = FALSE, title = "", repel = FALSE, repel_force = 0.3)
{
  d1 <- d
  #relevel factor variable for ggplot2
  p <- ggplot(data = d1, aes(x = paste0(tag), y=percentage, fill=variant2, order = -as.numeric(variant2))) +
    geom_bar(stat="identity", color = "black", width = 0.5)
  if(repel == TRUE)
  {
    p <- p + geom_text_repel(data=d1, aes(label = paste0(variant, "\n(", frequency, ")"), y = percentage), size=2.8, position = position_stack(vjust = .5), colour = "black", force = repel_force, max.iter = 10, family=font)
  }else
  {
    p <- p + geom_text(data=d1, aes(label = paste0(variant, "\n(", frequency, ")"), y = percentage), size=2.8, position = position_stack(vjust = .5), colour = "black", family=font) 
  }
  p <- p +  scale_x_discrete(labels = get_wraper(5)) +
    scale_fill_grey(start = 0.5, end = 0.8)+
    scale_y_continuous(labels = percent_format())+
    theme_bw()+
    theme(legend.position = "none") +
    theme(text=element_text(family=font, colour = "black"), panel.grid.minor.y = element_blank(),panel.grid.minor.x = element_blank() ,panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
    theme(axis.title.x = element_blank(), axis.title.y = element_blank())+
    theme(axis.text.x = element_text(colour="black"), axis.text.y = element_text(colour="black"))
  if(facet == TRUE)
  {
    p <- p + facet_wrap(~ study, ncol = 1)
  }
  p
}
tag = c("una", "una", "una", "una", "na", "na", "na", "na", "pikin", "pikin", "pikin", "pikin", "dey", "dey", "dey", "dey", "wey", "wey", "wey", "wey")
variant = c("other", "una", "other", "una", "other", "na", "other", "na", "other", "pikin(s)", "other", "pikin(s)", "other", "dey", "other", "dey", "other", "wey", "other", "wey")
study= c("Present Study", "Present Study", "Deuber and Hinrichs (2007)", "Deuber and Hinrichs (2007)", "Present Study", "Present Study", "Deuber and Hinrichs (2007)", "Deuber and Hinrichs (2007)", "Present Study", "Present Study", "Deuber and Hinrichs (2007)", "Deuber and Hinrichs (2007)", "Present Study", "Present Study", "Deuber and Hinrichs (2007)", "Deuber and Hinrichs (2007)", "Present Study", "Present Study", "Deuber and Hinrichs (2007)", "Deuber and Hinrichs (2007)")
variant2= c("other", "main", "other", "main", "other", "main", "other", "main", "other", "main", "other", "main", "other", "main", "other", "main", "other", "main", "other", "main")
frequency= c(55, 1094, 4, 746, 188, 8161, 2, 1598, 6, 711, 7, 101, 1753, 11878, 50, 2495, 311, 7410, 23, 1546)
percentage= c(0.047867711, 0.952132289, 0.005333333, 0.994666667, 0.022517667, 0.977482333, 0.00125, 0.99875, 0.008368201, 0.991631799, 0.064814815, 0.935185185, 0.128603918, 0.871396082, 0.019646365, 0.980353635, 0.040279757, 0.959720243, 0.014659018, 0.985340982)
df = cbind.data.frame(tag, variant, study, variant2, frequency, percentage)
df$tag <- as.character(df$tag)
df$tag <- factor(x=df$tag, levels=c("una", "na", "pikin", "dey", "wey"), ordered=T)
df <- df[order(df$tag),]
df
p1 <- graph_object(df, facet = TRUE)
plot(p1)

As the code shows I am creating a faceted bar plot with the reproduceable example data (df). The x-axis should be ordered in the way specified in factor(df$tag, levels(...)) function. However, the end result is always alphabetically ordered x axis. The option ordered=T in factor does not do anything as well. I have also tried with forcats::fct_relevel without success. Moreover, I also tried to convert the factor variable tag to character after or before sorting, but that does not change anything as well. All of these failed attempts have been based on previous answers, for example this one. The code provided here was run on https://rdrr.io/snippets/ to make sure it was not the fault of my system. The below image is copied from that run.

enter image description here

This is certainly a highly frequently asked question, but I have tried my best to follow other answers or search for tutorials without success. So your help will be very much appreciable.

Upvotes: 0

Views: 115

Answers (1)

Shakir
Shakir

Reputation: 345

removing paste0 around tag in aes solved the problem. enter image description here

Upvotes: 1

Related Questions