R ggplot2:无法根据因子变量的自定义顺序对x轴进行排序
我正在尝试创建一个带有x轴(因子变量)自定义顺序的条形图。在搜索了大约3个小时后,我想我应该直接寻求帮助R ggplot2:无法根据因子变量的自定义顺序对x轴进行排序,r,ggplot2,bar-chart,x-axis,R,Ggplot2,Bar Chart,X Axis,我正在尝试创建一个带有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) {
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)
库(dplyr)
图书馆(GG2)
图书馆(stringr)
库(extrafont)#extrafont::font_import()
图书馆(比例尺)
图书馆(gridExtra)
图书馆(ggrepel)
获取包装在aes
中的tag
周围移除paste0
解决了问题。