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

ggplot2+ggmosaic绘制的马赛克图过小、图例过大的调整问询

马赛克图布局优化问题与解决方案

问题说明

使用ggplot2和ggmosaic绘制马赛克图时,出现绘图区域尺寸过小、图例占比过大的问题,调整图例key的高度和宽度后未达到预期优化效果。

原始代码

mosaic_plot_color <- ggplot(data = longdata) +
  geom_mosaic(aes(x = product(Category, variable), fill=Category ,weight = `value`), offset=0.010) +
  scale_fill_brewer(palette = 2)+
  #scale_color_brewer(palette=3)+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = .5), 
        legend.key.height = unit(.1, 'cm'), 
        legend.key.width = unit(.4, 'cm'))+
  labs(x ="Frequency of social contact" , y = "Type of street (microm)", weight = "Number of observations", title = "Social contact with locals living in different house typologies")

可复现数据

structure(list(Category = c("1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses", "1-2 family houses in homogeneously built street section", 
"1-2 family houses in non-homogeneous street section", "3-5 family houses", 
"6-9 family houses", "Blocks with 10-19 households", "High-rise buildings with 20 and more households", 
"Predominantly commercially used houses"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), levels = c("Never", "Rarely", 
"Every month", "Every Week", "More than once a week", "Daily"
), class = "factor"), value = c(0, 24, 11, 35, 11, 14, 0, 4, 
26, 34, 28, 20, 20, 8, 6, 24, 22, 39, 14, 13, 3, 14, 40, 59, 
54, 48, 14, 6, 1, 26, 46, 36, 37, 17, 4, 4, 42, 52, 63, 40, 13, 
0)), row.names = c(NA, -42L), class = "data.frame")

尝试过的调整

曾尝试通过以下代码调整图例尺寸,但未达预期:

theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = .5), 
        legend.key.height = unit(.1, 'cm'), 
        legend.key.width = unit(.4, 'cm'))+

期望效果

希望实现绘图区域与图例比例协调的布局,同时保留当前使用的scale_fill_brewer(palette=2)配色方案。

最终解决方案代码

ggplot(data=longdata)+
  geom_mosaic(aes(x=product(variable,Category), fill=Category,weight = `value`))+
  theme(axis.text.x = element_text(size=4, angle = 45, hjust = 1))+
  theme(axis.text.y = element_text(size=5, angle = 45, hjust = 1))+
  scale_fill_brewer(palette=2)

最终效果

调整后,绘图区域尺寸得到合理分配,图例占比适中;轴文本缩小后适配布局,配色保留原方案,整体布局协调。


内容的提问来源于stack exchange,提问作者Maria Victoria Behler

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 06:24:58