This question already has answers here:
ggplot bar plot with facet-dependent order of categories

(4个答案)


4年前关闭。




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

我试过了
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)

它给了我
r - ggplot2:重新排列各个方面中从最高到最低的条形-LMLPHP

它并没有在每个方面将条形从最高到最低排序。

我想出了另一种获取我想要的东西的方法。我必须一次绘制每个变量,然后使用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))

结果就是我想要的
r - ggplot2:重新排列各个方面中从最高到最低的条形-LMLPHP

我想知道是否有一种直接的方法可以帮助我在所有方面中按从高到低的顺序排列条形,而不必分别绘制每个变量然后组合它们。任何建议将不胜感激。

数据
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)

最佳答案

下面的方法对带有facet_wrap()的x轴使用了专门准备的变量,但使用labels参数对scale_x_discrete()进行了显示,以显示正确的x轴标签:

准备数据

我更熟练data.table,所以这里使用它。随意使用您喜欢的软件包进行数据处理。

编辑:删除了第二个虚拟变量,只需要ord

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

创建情节
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")

r - ggplot2:重新排列各个方面中从最高到最低的条形-LMLPHP

数据
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)

关于r - ggplot2:重新排列各个方面中从最高到最低的条形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43176546/

10-12 18:47