You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中绘制Google表单复选框网格的多分类变量并优化可视化

问题解答

1. 整合所有应用的图表展示

完全可以把所有应用的数据整合到一张图里,不用逐个编写重复代码。核心思路是先合并所有数据集,统一添加标识区分不同应用,再批量处理数据格式并绘图:

# 加载依赖包
library(tidyverse)

# 合并所有应用数据,添加应用名称标识
combined_data <- bind_rows(
  data1 %>% mutate(app_label = "应用1"),
  data2 %>% mutate(app_label = "应用2"),
  data3 %>% mutate(app_label = "应用3"),
  data4 %>% mutate(app_label = "应用4")
) %>%
  # 转换yes/no列为数值类型
  mutate(across(c(yes, no), as.numeric)) %>%
  # 宽格式转长格式,方便ggplot绘图
  pivot_longer(cols = c(yes, no), names_to = "usage_status", values_to = "count")

# 绘制整合后的分面堆叠柱状图
ggplot(combined_data, aes(x = apps, y = count, fill = usage_status)) +
  geom_bar(position = "stack", stat = "identity") +
  # 按应用分面,可通过nrow/ncol控制布局
  facet_wrap(~app_label, nrow = 3) +
  labs(title = "各应用使用场景分布", x = "使用场景", y = "人数", fill = "是否使用") +
  scale_fill_manual(values = c("purple", "pink")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) # 旋转x轴标签避免重叠

如果想对比不同应用在同一场景下的表现,也可以把x设为app_label,用facet_wrap(~apps)按场景分面,灵活调整即可。

2. 更优的可视化方式选择

优先推荐:百分比堆叠柱状图

如果核心需求是看各场景下"是/否"的占比而非绝对人数,把堆叠方式改成position = "fill",每个柱子高度统一为100%,比例对比更清晰:

ggplot(combined_data, aes(x = apps, y = count, fill = usage_status)) +
  geom_bar(position = "fill", stat = "identity") +
  facet_wrap(~app_label) +
  labs(title = "各应用使用场景占比分布", x = "使用场景", y = "占比", fill = "是否使用") +
  scale_fill_manual(values = c("purple", "pink")) +
  scale_y_continuous(labels = scales::percent) # 显示百分比刻度

备选:分组柱状图

如果需要直接对比"是/否"的绝对数值,用position = "dodge"制作分组柱状图,比堆叠更直观:

ggplot(combined_data, aes(x = apps, y = count, fill = usage_status)) +
  geom_bar(position = "dodge", stat = "identity") +
  facet_wrap(~app_label) +
  labs(title = "各应用使用场景人数对比", x = "使用场景", y = "人数", fill = "是否使用") +
  scale_fill_manual(values = c("purple", "pink")) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

马赛克图的适用性

马赛克图更适合展示多分类变量的交叉关联占比(比如同时分析应用、场景、使用状态三者的深层关系),但解读成本较高;如果你的需求只是清晰对比每款应用下各场景的"是/否"情况,柱状图(尤其是百分比堆叠款)更直观,也更适合新手快速实现和理解。

内容的提问来源于stack exchange,提问作者ilc17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 14:03:22