ggplot2:在每个方面从最高到最低排列条形图 [英] ggplot2: reorder bars from highest to lowest in each facet

查看:1076
本文介绍了ggplot2:在每个方面从最高到最低排列条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的 df 中,我想在每个方面从最高到最低 对条进行重新排序



我试过了

  df < -  df%>%tidyr :: collect(var,value,2:4)
ggplot(df,aes(x = reorder(id,-value),y = value,fill = id))+
geom_bar stat =identity)+ facet_wrap(〜var,ncol = 3)

$ b



它并没有在每个方面从高到低排列。



我找到了另一种获得我想要的方式。我必须一次绘制每个变量,然后使用 grid.arrange() $ b $

 #我从@ eipi10的回答得到了这个函数
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937
#Function提取传奇
#https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend< -function(a.gplot){
tmp< - ggplot_gtable(ggplot_build(a.gplot))
leg< - 其中(sapply(tmp $ grobs,function(x)x $ name)==guide-box)
传说< - tmp $ grobs [[leg]]
return(legend)
}

p1 < - ggplot(df [df $ var ==A ',],aes(x = reorder(id,-value),y = value,fill = id))+
geom_bar(stat =identity)+ facet_wrap(〜var,ncol = 3)

fin_legend <-g_legend(p1)
p1 <-p1 + guides(fill = F)

p2 < - ggplot(df [df $ var = =B,],aes(x = reorder(id,-value),y = value,fill = id))+
geom_bar(stat =identity)+ fa cet_wrap(〜var,ncol = 3)+ guides(fill = FALSE)

p3 < - ggplot(df [df $ var ==C,],aes(x = reorder , - =值),fill = id))+
geom_bar(stat =identity)+ facet_wrap(〜var,ncol = 3)+ guides(fill = FALSE)


grid.arrange(p1,p2,p3,fin_legend,ncol = 4,widths = c(1.5,1.5,1.5,0.5))

结果是我想要的



我想知道是否有一种简单的方法可以帮助我在所有方面从高到低排列条,而不必将每个变量单独绘制并合并它们。任何建议将不胜感激。

DATA
$ b

  df < -  read.table(text = c(
id ABC
site1 10 15 20
site2 20 10 30
site3 30 20 25 $ b $ site4 40 35 40
site5 50 30 35),header = T)


解决方案

下面的方法为 facet_wrap()使用一个特殊准备的x轴变量,但使用标签参数至 scale_x_discrete()以显示正确的x轴标签:

准备数据



我更流利 data.table ,所以这里使用它。您可以随意使用您想要进行数据操作的软件包。



编辑:删除第二个虚拟变量,只有 ord 是必需的

  library(data.table)
#从宽转长
熔化< - 熔化(setDT(df),id.vars =id)
#创建虚拟var,它反映按字母顺序排序时的顺序
molten [,ord:= sprintf( 02i,弗兰克(熔化,变量,值,tie.method =第一))]

熔化
#id变量值ord
#1:site1 A 10 05
#2:site2 A 20 04
#3:site3 A 30 03
#4:site4 A 40 02
#5:site5 A 50 01
# 6:site1 B 15 09
#7:site2 B 10 10
#8:site3 B 20 08
#9:site4 B 35 06
#10:site5 B 30 07
#11:site1 C 20 15
#12:site2 C 30 13
#13:site3 C 25 14
#14:site4 C 40 11
#15:site5 C 35 12



创建绘图



pre $ library $ g $
$``ord`绘制在x轴上而不是`id`
ggplot(熔化, aes(x = ord,y = value,fill = id))+
#geom_col()取代geom_bar(stat =identity)
geom_col()+
#独立x每个方面的轴比例,
#缺省因子等级(这里不是这种情况)
facet_wrap(〜variable,scales =free_x,drop = TRUE)+
#使用命名字符用于替换x轴标签的矢量
scale_x_discrete(labels = molten [,setNames(as.character(id),ord)])+
#替换x轴标题
xlab(id )



Data



  df < -  read.table(text =
id ABC
site1 10 15 20
site2 20 10 30
site3 30 20 25
site4 40 35 40
site5 50 30 35,header = T)


In the df below, I want to reorder bars from highest to lowest in each facet

I tried

df <- df %>% tidyr::gather("var", "value", 2:4)
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity")+facet_wrap(~var, ncol =3)

It gave me

It didn't order the bars from highest to lowest in each facet.

I figured out another way to get what I want. I had to plot each variable at a time, then combine all plots using grid.arrange()

#I got this function from @eipi10's answer
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937  
#Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot) {
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)
}

p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity") + facet_wrap(~var, ncol =3)

fin_legend <- g_legend(p1)
p1 <- p1 + guides(fill= F)

p2 <- ggplot(df[df$var== "B", ], aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE) 

p3 <- ggplot(df[df$var== "C", ], aes (x = reorder(id, -value), y = value, fill = id))+
  geom_bar(stat="identity") + facet_wrap(~var, ncol =3)+guides(fill=FALSE) 


grid.arrange(p1, p2, p3, fin_legend, ncol =4, widths = c(1.5, 1.5, 1.5, 0.5))

The result is what I want

I wonder if there is a straightforward way that can help me order the bars from highest to lowest in all facets without having to plot each variable separtely and then combine them. Any suggestions will be much appreciated.

DATA

df <-  read.table(text = c("
id  A   B   C
site1   10  15  20
site2   20  10  30
site3   30  20  25
site4   40  35  40
site5   50  30  35"), header = T)

解决方案

The approach below uses a specially prepared variable for the x-axis with facet_wrap() but uses the labels parameter to scale_x_discrete() to display the correct x-axis labels:

Prepare data

I'm more fluent in data.table, so this is used here. Feel free to use what ever package you prefer for data manipulation.

Edit: Removed second dummy variable, only ord is required

library(data.table)   
# reshape from wide to long
molten <- melt(setDT(df), id.vars = "id")
# create dummy var which reflects order when sorted alphabetically
molten[, ord := sprintf("%02i", frank(molten, variable, -value, ties.method = "first"))]

molten
#       id variable value ord
# 1: site1        A    10  05
# 2: site2        A    20  04
# 3: site3        A    30  03
# 4: site4        A    40  02
# 5: site5        A    50  01
# 6: site1        B    15  09
# 7: site2        B    10  10
# 8: site3        B    20  08
# 9: site4        B    35  06
#10: site5        B    30  07
#11: site1        C    20  15
#12: site2        C    30  13
#13: site3        C    25  14
#14: site4        C    40  11
#15: site5        C    35  12

Create plot

library(ggplot2)
# `ord` is plotted on x-axis instead of `id`
ggplot(molten, aes(x = ord, y = value, fill = id)) +
  # geom_col() is replacement for geom_bar(stat = "identity")
  geom_col() +
  # independent x-axis scale in each facet, 
  # drop absent factor levels (not the case here)
  facet_wrap(~ variable, scales = "free_x", drop = TRUE) +
  # use named character vector to replace x-axis labels
  scale_x_discrete(labels = molten[, setNames(as.character(id), ord)]) + 
  # replace x-axis title
  xlab("id")

Data

df <- read.table(text = "
id  A   B   C
site1   10  15  20
site2   20  10  30
site3   30  20  25
site4   40  35  40
site5   50  30  35", header = T)

这篇关于ggplot2:在每个方面从最高到最低排列条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆