条形图的大小和百分比不匹配

亚什·卡诺加

我想根据客户的性别、教育程度和默认付款状态绘制客户的详细信息。但是other类别显示的尺寸比其他条形图大。

# 数据链接“ https://archive.ics.uci.edu/ml/machine-learning-databases/00350/

plot_data5 <- customer.data %>% 
  group_by(EDUCATION,SEX) %>% 
  mutate(group_size = n()) %>%
  group_by(EDUCATION,SEX, DEFAULT_PAYMENT) %>%
  summarise(perc = paste(round(n()*100/max(group_size), digits = 2), 
  "%", sep = ""))


ggplot(plot_data5, aes(x = plot_data5$EDUCATION, y = plot_data5$perc, fill = DEFAULT_PAYMENT))+
  geom_bar(stat = "identity") + 
  geom_text(aes(label = plot_data5$perc),vjust=-.3) +
  facet_wrap(DEFAULT_PAYMENT~SEX,scales = "free") +
  theme(plot.subtitle = element_text(vjust = 1), 
        plot.caption = element_text(vjust = 1)) + 
  labs(y = "% of Customer ") + 
  labs(x = "Default_Payment")

实际结果应该只是这些,但具有条形的真实大小和连续的 y 轴比例。

凯斯

有没有必要重新指定要使用的数据帧aes你的-call ggplot这会妨碍标签的正确分配。此外,由于您希望拥有连续的 y 轴,因此您需要将其perc作为连续变量。

plot_data <- customer.data.small %>% 
  group_by(EDUCATION, SEX) %>% 
  mutate(group_size = n()) %>%
  group_by(EDUCATION, SEX, DEFAULT_PAYMENT) %>%
  summarise(perc = n()/max(group_size)) # Keep perc continuous

ggplot(plot_data, aes(x = EDUCATION, y = perc, fill = DEFAULT_PAYMENT)) +
  geom_bar(stat = "identity") +
  # Specify the labels with % and rounded in aes directly: 
  geom_text(aes(label = paste0(round(100*perc, 2), "%")), vjust = -.3) +
  facet_wrap(DEFAULT_PAYMENT ~ SEX, scales = "free_y") +
  # Use scales::percent to have percentages on the y-axis. 
  # Expand makes sure you can still read the labels
  scale_y_continuous(labels = scales::percent, expand = c(0.075, 0)) +
  theme(plot.subtitle = element_text(vjust = 1),
        plot.caption = element_text(vjust = 1)) +
  labs(y = "% of Customer ") +
  labs(x = "Default_Payment")

在此处输入图片说明我发现数据的表示非常具有误导性!您将 x 轴标记为“Default_Payment”,尽管它显示EDUCATION从图中不清楚为什么每个分组的百分比加起来不是 100%,这让读者感到困惑。以下是如何改进情节的建议:

plot_data2 <- customer.data.small %>% 
  mutate_at(c("DEFAULT_PAYMENT", "EDUCATION", "SEX"), factor) %>% 
  group_by(EDUCATION, SEX) %>% 
  mutate(group_size = n()) %>%
  group_by(EDUCATION, SEX, DEFAULT_PAYMENT) %>%
  summarise(perc = n()/max(group_size))

ggplot(plot_data2, aes(x = EDUCATION, y = perc, fill = DEFAULT_PAYMENT)) +
  geom_bar(stat = "identity", 
           position = position_dodge2(width = 0.9, preserve = "single")) +
  geom_text(aes(label = paste0(round(100 * perc, 2), "%")),
            vjust = -.3,
            position = position_dodge(0.9)) +
  facet_wrap( ~ SEX, labeller = label_both) +
  scale_y_continuous(labels = scales::percent) +
  theme(plot.subtitle = element_text(vjust = 1),
        plot.caption = element_text(vjust = 1)) +
  labs(y = "% of Customer ") +
  labs(x = "Education")

在此处输入图片说明

数据
我使用您以可重现格式提供的一小部分数据,每个人都可以复制并粘贴到他们自己的 R 会话中,而无需下载数据集。

customer.data.small <- 
  structure(list(ID = 1:100, 
                 EDUCATION = c(2, 2, 2, 2, 2, 1, 1, 2, 3, 3, 3, 1, 2, 2, 1, 3, 1, 1, 1, 1, 3, 2, 2, 1, 1, 3, 1, 3, 3, 1, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 1, 5, 2, 1, 3, 3, 2, 1, 1, 1, 3, 2, 1, 2, 3, 2, 1, 2, 2, 1, 2, 1, 3, 5, 1, 2, 2, 1, 1, 2, 3, 1, 2, 2, 3, 1, 3, 2, 3, 2, 1, 2, 1, 3, 1, 1, 1, 2, 2, 2, 1, 1, 3, 2), 
                 SEX = c(2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 2, 1), 
                 DEFAULT_PAYMENT = c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1)), 
            row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))

这是我创建该数据的方式:

customer.data <- readxl::read_xls("default of credit card clients.xls", skip = 1)

customer.data.small <- customer.data %>% 
  select(ID, EDUCATION, SEX, DEFAULT_PAYMENT = `default payment next month`) %>% 
  slice(1:100) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何显示百分比和条形图

堆叠的ggplot百分比条形图在闪亮

百分比堆积条形图熊猫

ggplot条形图百分比组

带百分比的堆积条形图

试图从百分比绘制堆积条形图

堆积条形图百分比

叠加的堆积百分比条形图

总与部分的条形图,百分比 (R)

如何计算和绘制堆叠条形图的百分比标签以及条形图的总数?

具有渐变值(而非百分比)和值线的Excel条形图

按单位和百分比堆积的条形图

Python 中的分组、百分比和条形图

ggplot2中条形图的计数和百分比如何?[R

使用绝对值和百分比值 + 标签绘制分组条形图

Excel Pivot带有百分比和条形图计数

带有groupby和条形图的python百分比标签

如何使用分组条形图创建百分比条形图?

ggplot2 中的条形图显示每个条形和 c 中总的百分比

R堆叠百分比条形图,带有二元因子和标签的百分比

从条形图 Zipf 分布中获取条形百分比

创建条形图以显示条形顶部的百分比变化

Vega Lite 中带有绝对数量和百分比的标签条形图

Python绘图条形图和百分比折线图在同一图形上

数据透视表中的条形图,具有总计和每组总计的百分比

条形图 - 堆叠的 ggplot 百分比条形图起始值不是 0%

ggplot2:如何绘制条形图,条形图表示百分比,并根据百分比值进行着色?

如何创建频率堆叠的条形图,但是条形图上的百分比标签和y轴上的频率在R中?

Python条形图y轴显示百分比