使用R的likert包绘图时,plot()无法通过cex.axis修改轴文本大小
解决likert包绘图时文本大小和主标题参数不生效的问题
问题原因
likert包的plot()方法(plot.likert)并不兼容基础R绘图的cex.axis、cex.lab和main参数——它底层基于ggplot2绘图系统,需要用ggplot2的语法来调整文本样式和添加标题,基础R的绘图参数无法直接生效。
解决方案
你可以先生成likert绘图对象,再用ggplot2的函数补充标题、调整文本大小:
library(likert) library(RColorBrewer) library(ggplot2) # 生成likert对象 p <- likert(summary = data ) # 生成基础绘图 plot_obj <- plot(p, plot.percents=FALSE, plot.percent.low=FALSE, plot.percent.high=FALSE, include.center=TRUE, plot.percent.neutral = FALSE, col = brewer.pal(n = 7, name = "RdBu")) # 添加主标题并调整文本大小 plot_obj + ggtitle("你的自定义标题") + theme( # 调整轴标签大小(对应cex.lab) axis.title = element_text(size = 10), # 调整轴刻度文本大小(对应cex.axis) axis.text.y = element_text(size = 8), # 国家名称所在的y轴 axis.text.x = element_text(size = 8), # 刻度值所在的x轴 # 调整标题大小并居中 plot.title = element_text(size = 12, hjust = 0.5) )
补充说明
如果你的likert版本较旧(基于lattice绘图系统),可以用lattice的参数直接设置:
plot(p, plot.percents=FALSE, plot.percent.low=FALSE, plot.percent.high=FALSE, include.center=TRUE, plot.percent.neutral = FALSE, col = brewer.pal(n = 7, name = "RdBu"), # 设置主标题 main = list(label = "你的自定义标题", cex = 1.2), # 调整轴刻度文本大小 scales = list(y = list(cex = 0.5), x = list(cex = 0.5)) )
内容的提问来源于stack exchange,提问作者FLOX DAW
相关产品推荐
相关产品推荐

