ggplot柱状图多图例字号过大,如何统一设置为7号?
问题
我绘制的柱状图因左侧、右侧及底部的文本字号过大导致显示效果不佳。尝试用legend.title = element_text(size=7)仅成功修改了右侧“Answer”图例的标题字号,希望将图中所有文本统一设置为该字号。此前错误将这段代码放入scale_y_continuous参数中,触发报错:
Error in scale_y_continuous(name = "Percentage", breaks = seq(from = 0, : unused argument (legend.title = element_text(size = 7))
请问正确的代码放置位置或是否需要其他代码?
原代码
mydata %>% mutate( variable = recode( variable, 'item3' = ' Very long item name here')) %>% drop_na() %>% ggplot(mapping = aes(x = variable)) + geom_bar(aes(fill = value), position = 'fill', width = 0.30)+ scale_y_continuous(name = 'Percentage', breaks = seq(from = 0, to = 1, by = 0.2), minor_breaks = seq(from = 0, to = 1, by = 0.1), labels = seq(from = 0, to = 100, by = 20), expand = c(0,0)) + xlab( element_blank() ) + scale_fill_manual( values = c(fioh_colors$fioh_blue_40, fioh_colors$fioh_blue_60, fioh_colors$fioh_blue_80, fioh_colors$fioh_blue_100, fioh_colors$fioh_turq_20 ), labels = c( 'yes', 'no', 'maybe' ), drop = FALSE ) + guides( fill = guide_legend(title = 'Answer') ) + theme(panel.background = element_blank(), legend.title = element_text(size=7), plot.margin = margin(t = 30, r = 30, b = 20, l = 0), axis.ticks.y = element_blank(), panel.grid.major = element_line(colour = 'grey'), axis.ticks.x = element_line(colour = 'grey'), panel.grid.minor = element_line(colour = 'lightgrey'), axis.text.y = element_text( size = 11, face = 'bold', hjust = 0 )) + coord_flip()
数据
structure(list(variable = c("item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3", "item3"), value = structure(c(1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L), .Label = c("1", "2", "3"), class = c("ordered", "factor"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -147L))
解决方案
所有文本样式的调整都需要在theme()函数内完成,你只修改了图例标题的字号,还需要补充以下几个元素的设置:
axis.text: 轴刻度文本(对应左侧、底部的刻度文字)axis.title: 轴标题(对应底部的"Percentage")legend.text: 图例选项文本(对应右侧"Answer"下的yes/no/maybe)
修改后的完整代码如下:
mydata %>% mutate( variable = recode( variable, 'item3' = 'Very long item name here')) %>% # 去掉多余换行避免文本异常 drop_na() %>% ggplot(mapping = aes(x = variable)) + geom_bar(aes(fill = value), position = 'fill', width = 0.30)+ scale_y_continuous(name = 'Percentage', breaks = seq(from = 0, to = 1, by = 0.2), minor_breaks = seq(from = 0, to = 1, by = 0.1), labels = seq(from = 0, to = 100, by = 20), expand = c(0,0)) + xlab(element_blank()) + scale_fill_manual( values = c(fioh_colors$fioh_blue_40, fioh_colors$fioh_blue_60, fioh_colors$fioh_blue_80, fioh_colors$fioh_blue_100, fioh_colors$fioh_turq_20 ), labels = c( 'yes', 'no', 'maybe' ), drop = FALSE ) + guides( fill = guide_legend(title = 'Answer') ) + theme( panel.background = element_blank(), # 统一设置所有文本字号为7 legend.title = element_text(size=7), legend.text = element_text(size=7), axis.text = element_text(size=7), axis.title = element_text(size=7), plot.margin = margin(t = 30, r = 30, b = 20, l = 0), axis.ticks.y = element_blank(), panel.grid.major = element_line(colour = 'grey'), axis.ticks.x = element_line(colour = 'grey'), panel.grid.minor = element_line(colour = 'lightgrey'), # 保留y轴文本加粗样式,仅修改字号 axis.text.y = element_text( size = 7, face = 'bold', hjust = 0 ) ) + coord_flip()
关键说明
- 之前报错是因为
scale_y_continuous仅负责坐标轴的刻度、标签等逻辑设置,不接受主题样式参数,所有主题相关的设置必须放在theme()中。 - 若不需要y轴文本的加粗样式,可以直接删除
axis.text.y的单独设置,让它继承axis.text的统一样式。 - 修正了
variable重编码时的多余换行,避免左侧文本出现不必要的空白。
内容的提问来源于stack exchange,提问作者Espejito
相关产品推荐
相关产品推荐

