带刻面和两个变量的R中柱状图排序

带刻面和两个变量的R中柱状图排序,r,ggplot2,R,Ggplot2,我的意图是使用ggplot2和facet_wrap绘制两张图表“长期”和“非常长期”失业率。 我希望顺序按indi==“LTU”(facet_wrap的顶部)升序,因此在顶部图表中,“LU”和“MT”不在右侧。但我没办法把酒吧订好。有什么建议吗 可复制示例: library(tidyverse) library(eurostat) #Long-term unemployment by sex - quarterly average une_ltu_q <- get_eurostat("

我的意图是使用ggplot2和facet_wrap绘制两张图表“长期”和“非常长期”失业率。 我希望顺序按indi==“LTU”(facet_wrap的顶部)升序,因此在顶部图表中,“LU”和“MT”不在右侧。但我没办法把酒吧订好。有什么建议吗

可复制示例:

library(tidyverse)
library(eurostat)

#Long-term unemployment by sex - quarterly average 
une_ltu_q <- get_eurostat("une_ltu_q", stringsAsFactors=FALSE)


df <- une_ltu_q %>% filter(age=="Y15-74",
                          geo %in% c("EU28", "BE","BG","CZ","DK","DE","EE","IE","EL","ES","FR","HR","IT","CY","LV","LT","LU","HU","MT","NL","AT","PL","PT","RO","SI","SK","FI","SE","UK"),
                          sex=="T",
                          s_adj=="SA",
                          time==max(une_ltu_q$time), 
                          unit=="PC_ACT")%>% 
        group_by(indic_em)%>% arrange()



ggplot(data=df, aes(x=reorder(geo, values), y=values))+
        geom_bar(stat = "identity", 
                 position = "dodge", 
                 show.legend = FALSE,
                 fill="steelblue")+
        geom_text(aes(label=values), vjust=-0.5, size=3.5)+
        facet_wrap(~indic_em, ncol=1, scales = "free")
库(tidyverse)
图书馆(欧盟统计局)
#按性别分列的长期失业率——季度平均数
联合国千年发展目标q%
分组依据(指标)%>%arrange()
ggplot(数据=df,aes(x=重新排序(地理位置,值),y=值))+
几何图形栏(stat=“identity”,
position=“道奇”,
show.legend=FALSE,
fill=“钢蓝”)+
geom_文本(aes(标签=值),vjust=-0.5,大小=3.5)+
镶嵌面包裹(~indic_em,ncol=1,scales=“free”)

reorder
有一个
FUN
action参数,可用于更改顺序。默认情况下,示例按两个值的
平均值排序。但也可以只取第一个值:

ggplot(data=df, aes(x=reorder(geo, values, "[", 1), y=values))+
  geom_bar(stat = "identity", 
           position = "dodge", 
           show.legend = FALSE,
           fill="steelblue")+
  geom_text(aes(label=values), vjust=-0.5, size=3.5)+
  facet_wrap(~indic_em, ncol=1, scales = "free")

总和

ggplot(data=df, aes(x=reorder(geo, values, sum, na.rm=T), y=values))+
  geom_bar(stat = "identity", 
           position = "dodge", 
           show.legend = FALSE,
           fill="steelblue")+
  geom_text(aes(label=values), vjust=-0.5, size=3.5)+
  facet_wrap(~indic_em, ncol=1, scales = "free")

使用下列代码:

我们可以使用这些函数在组内重新排序,以便每个方面都有升序条。下面是集成这些功能的修改后的ggplot代码

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

scale_x_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}


ggplot(data=df, aes(x=reorder_within(geo, values, indic_em), y=values))+
geom_col(stat = "identity", 
position = "dodge", 
show.legend = FALSE,
fill="steelblue")+
geom_text(aes(label=values), vjust=-0.5, size=3.5)+
scale_x_reordered()+
facet_wrap(~indic_em, ncol=1, scales = "free")

reorder_内感谢!请您解释一下
“[”
重新排序
函数中到底意味着什么?@JustasMundeikis不客气。这是
重新排序(geo,values,function(x)x[1])
的快捷方式。方括号是一个函数,请参见
?Extract