在条形图中绘制前n个和其他 [英] Plotting top n and other in a bar chart

查看:49
本文介绍了在条形图中绘制前n个和其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Kaggle的 WW2盟友爆炸数据集合作,有五个级别的类别变量.我正在尝试绘制两个轰炸机(美国和英国),并将其他轰炸机(南非,新西兰,澳大利亚)分组到条形图上.

I am working with a WW2 ally bombings dataset from Kaggle, which has a categorical variable with five levels. I am trying to plot the two highest bombers (USA and Great Britain) and group the others (South Africa, New Zealand, Australia) onto a bar chart.

如何将最小的组聚合为其他"组?

How can I aggregate the smallest groups into a group "Other"?

当前方法

我有一些解决方法,可以使用xlim绘制出前两个.我想知道R中是否有一种简单的方法可以将残差分组并将其绘制为其他"?

I have a bit of a workaround going to get the top two plotted using xlim. I am wondering if there a simple way in R to group the residuals and plot them as 'other'?

ggplot(data = operations) +
geom_bar(mapping = aes(x = Country, fill = Country)) + xlim('USA', 'GREAT 
         BRITAIN')+
ggtitle("Allied Bombings") +
    xlab("Country") + ylab("Bombs Dropped") +
    theme(plot.title = element_text(hjust = 0.5))+
    theme(panel.background = element_rect(fill = 'transparent', colour = NA))

推荐答案

您可以使用软件包forcats中的fct_lump函数,该函数将最小/最常见的因子水平汇总到其他"中.

You can use the fct_lump function from the package forcats, which aggregates together least/most common factor levels into "other".

这是一个可复制的示例.在此示例中,仅保留了两个最大的组:

Here is a reproducible example. Only the two largest groups are retained in this example:

df <- data.frame(group =rep(LETTERS[1:9], times = c(40, 10, 5, 27, 1, 1, 1, 1, 1)))

library(forcats)
df$groupLump <- fct_lump(df$group, 2)

library(ggplot2)
ggplot(df) +
  geom_bar(aes(x = groupLump, fill = groupLump))

这篇关于在条形图中绘制前n个和其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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