如何在多面 ggplot2 条形图中对条形进行排序

发布于 2024-10-26 08:02:11 字数 896 浏览 2 评论 0原文

如果我想将 ggplot2 条形图中的条形从大到小排序,那么我通常会更新条形类别的因子水平,就像这样

one_group <- data.frame(
  height   = runif(5),
  category = gl(5, 1)
)

o <- order(one_group$height, decreasing = TRUE)
one_group$category <- factor(one_group$category, levels = one_group$category[o])

p_one_group <- ggplot(one_group, aes(category, height)) +
  geom_bar(stat = "identity")
p_one_group

如果有几组条形图,我想要在不同的方面,每个如果小平面的条形从最大到最小(以及不同的 x 轴)排序,那么该技术就会失效。

给定一些示例数据

two_groups <- data.frame(
  height   = runif(10),
  category = gl(5, 2),
  group    = gl(2, 1, 10, labels = letters[1:2])
)

和绘图代码,

p_two_groups <- ggplot(two_groups, aes(category, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x")
p_two_groups

我需要做什么才能使条形图顺序正确?

如果有帮助,需要解决的一个等效问题是:完成分面后如何更新因子级别?

If I want to order the bars in a ggplot2 barchart from largest to smallest, then I'd usually update the factor levels of the bar category, like so

one_group <- data.frame(
  height   = runif(5),
  category = gl(5, 1)
)

o <- order(one_group$height, decreasing = TRUE)
one_group$category <- factor(one_group$category, levels = one_group$category[o])

p_one_group <- ggplot(one_group, aes(category, height)) +
  geom_bar(stat = "identity")
p_one_group

If have have several groups of barcharts that I'd like in different facets, with each facet having bars ordered from largest to smallest (and different x-axes) then the technique breaks down.

Given some sample data

two_groups <- data.frame(
  height   = runif(10),
  category = gl(5, 2),
  group    = gl(2, 1, 10, labels = letters[1:2])
)

and the plotting code

p_two_groups <- ggplot(two_groups, aes(category, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x")
p_two_groups

what do I need to do to get the bar ordering right?

If it helps, an equivalent problem to solve is: how do I update factor levels after I've done the faceting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天 2024-11-02 08:02:11

这里有一个技巧:

two_groups <- transform(two_groups, category2 = factor(paste(group, category)))
two_groups <- transform(two_groups, category2 = reorder(category2, rank(height)))

ggplot(two_groups, aes(category2, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x") +
  scale_x_discrete(labels=two_groups$category, breaks=two_groups$category2)
  1. 为所有条目(category2)创建UNIQUE因子变量,
  2. 根据
  3. 变量上的高度图重新排序变量:aes(x=category2)
  4. 使用原始值(category2)重新标记轴) 对于scale_x_discrete 中的变量(category2)。

here is a hack:

two_groups <- transform(two_groups, category2 = factor(paste(group, category)))
two_groups <- transform(two_groups, category2 = reorder(category2, rank(height)))

ggplot(two_groups, aes(category2, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x") +
  scale_x_discrete(labels=two_groups$category, breaks=two_groups$category2)
  1. make UNIQUE factor variable for all entries (category2)
  2. reorder the variable based on the height
  3. plot on the variable: aes(x=category2)
  4. re-label the axis using original value (category) for the variable (category2) in scale_x_discrete.
喜爱纠缠 2024-11-02 08:02:11

这是一个实现你想要的东西的技巧。我无法弄清楚如何获得刻度线下方的类别值。因此,如果有人可以帮助解决这个问题,那就太好了。让我知道这是否有效

# add a height rank variable to the data frame
two_groups = ddply(two_groups, .(group), transform, hrank = rank(height));

# plot the graph

p_two_groups <- ggplot(two_groups, aes(-hrank, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x") +
  opts(axis.text.x = theme_blank()) +
  geom_text(aes(y = 0, label = category, vjust = 1.5))

Here is a hack to achieve what you want. I was unable to figure out how to get the category values below the tick marks. So if someone can help fix that, it would be wonderful. Let me know if this works

# add a height rank variable to the data frame
two_groups = ddply(two_groups, .(group), transform, hrank = rank(height));

# plot the graph

p_two_groups <- ggplot(two_groups, aes(-hrank, height)) +
  geom_bar(stat = "identity") +
  facet_grid(. ~ group, scales = "free_x") +
  opts(axis.text.x = theme_blank()) +
  geom_text(aes(y = 0, label = category, vjust = 1.5))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文