0

我正在尝试使用 x 轴(因子变量)的自定义顺序创建条形图。搜索了大约3个小时后,我想我应该直接寻求帮助。

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)

正如代码所示,我正在使用可重现的示例数据 ( df) 创建一个多面条形图。x 轴应按照factor(df$tag, levels(...))函数中指定的方式排序。但是,最终结果始终是按字母顺序排列的 x 轴。ordered=T中的选项factor也不做任何事情。我也试过forcats::fct_relevel没有成功。此外,我还尝试将因子变量转换为tag排序character之后或之前,但这也没有改变任何东西。所有这些失败的尝试都是基于以前的答案,例如这个。此处提供的代码在https://rdrr.io/snippets/上运行,以确保这不是我的系统的错误。下图是从该运行中复制的。

在此处输入图像描述

这当然是一个非常常见的问题,但我已尽力遵循其他答案或搜索教程但没有成功。因此,您的帮助将非常显着。

4

1 回答 1

1

paste0解决tagaes这个问题。 在此处输入图像描述

于 2021-05-25T10:55:32.837 回答