【问题标题】:ggplot2: unable to sort x axis based on custom ordering of factor variableggplot2:无法根据因子变量的自定义排序对 x 轴进行排序
【发布时间】:2021-08-13 14:30:07
【问题描述】:

我正在尝试使用 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 轴。 factor 中的选项 ordered=T 也没有任何作用。我也尝试过forcats::fct_relevel,但没有成功。此外,我还尝试在排序之后或之前将因子变量tag 转换为character,但这也没有改变任何东西。所有这些失败的尝试都是基于以前的答案,例如this one。这里提供的代码是在https://rdrr.io/snippets/ 上运行的,以确保它不是我的系统的错误。下图是从该运行中复制的。

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

【问题讨论】:

    标签: r ggplot2 bar-chart x-axis


    【解决方案1】:

    aes 中删除tag 周围的paste0 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多