R语言ggplot绘图报错:Error in alpha * 255 : non-numeric argument to binary operator
问题解决:Error in alpha * 255 : non-numeric argument to binary operator
报错根源
你的代码中geom_ribbon的alpha参数传入了字符串类型的"0.5",而新版本ggplot2(适配新版R)要求透明度参数必须是数值类型,字符串无法参与内部的alpha * 255数值运算,因此触发报错。旧版本R/ggplot2存在类型容错,所以之前能正常运行。
修复步骤
将geom_ribbon中的alpha="0.5"改为alpha=0.5,去掉引号即可。
修复后的关键代码片段
geom_ribbon(data=subset(Z_score_summary,occ==1), aes(x = Titles.ordered,ymin=Zmin,ymax=Zmax,fill="Historical Z-Scores"), color=0, alpha=0.5,show.legend=TRUE)
完整修复后代码
ggplot(subset(Z_score_summary,occ==1),aes(x = Titles.ordered,y = Z_score,ymin=Zmin,ymax=Zmax,group=date) ) + geom_ribbon(data=subset(Z_score_summary,occ==1), aes(x = Titles.ordered,ymin=Zmin,ymax=Zmax,fill="Historical Z-Scores"), color=0, alpha=0.5,show.legend=TRUE) + scale_fill_manual(values=c("grey")) + geom_hline(yintercept=3.1, linetype="dashed",size=1.2) + geom_hline(yintercept=2.0, linetype="solid",size=0.5) + geom_hline(yintercept=1.0, linetype="solid",size=0.5) + geom_hline(yintercept=0.0, linetype="solid",size=0.5) + geom_line(data=Z_score_summary,aes(x = Titles.ordered, y = Z_score, group = date, color = date,linetype=date), size=1.5) + annotate(geom="text", label='Z-Score = 3.1 (FTY > 99.9%)', x=13.2, y=3.3, vjust=0, size = 4.5)+ annotate(geom="text", label='FTY = 97.7%', x=13.2, y=2.1, vjust=0, size = 3.5)+ annotate(geom="text", label='FTY = 84.1%', x=13.2, y=1.1, vjust=0, size = 3.5)+ annotate(geom="text", label='FTY = 50.0%', x=13.2, y=0.1, vjust=0, size = 3.5)+ scale_color_manual(values=c("#00FF00", "#0033CC","#FF3300","#FF3300"),guide = guide_legend(override.aes=aes(fill=NA)))+ scale_linetype_manual(values = c(1,1,1,0)) + ylab("Z-Scores (none)") + labs(title=paste("Process Capability ")) + scale_y_continuous(minor_breaks = seq(0, 15, by = 1), breaks = seq(0, 15, by = 5), #limits = c(0,25), expand = c(0, 0))+ coord_cartesian(ylim = c(0,15)) + theme(axis.title.x = element_blank(), axis.text.x = element_text(size=10, angle = 45, hjust = 1, face="bold", color = 'black'), axis.title.y = element_text(face="bold", size=14, color = 'black'), axis.text.y = element_text(size=10, face="bold", color = 'black'), axis.ticks = element_line(colour = 'black', size = 1, linetype = 'solid'), axis.ticks.length = unit(0.3, "cm"), axis.line.x = element_line(size=1.2), axis.line.y = element_line(size=1.2), legend.title=element_blank(), legend.text = element_text(size = 12, face="bold", color = 'black'), legend.justification=c(0,1), legend.position=c(0,1), legend.background = element_rect(colour = "black"), legend.key.size = unit(0.85, "cm"), legend.key = element_rect(fill = "white"), panel.grid.major = element_line(colour = "grey80"), panel.grid.minor = element_line(colour = "grey80"), panel.background = element_rect(fill = "white"), plot.title = element_text(size = rel(2), face="bold")) ggsave(width = 16, height = 9, dpi = 200,"Plot_summary.png", plot = last_plot())
额外检查
如果修复后仍有问题,建议确认所有依赖包(ggplot2、dplyr等)都更新到适配当前R版本的最新版,避免版本兼容问题。
内容的提问来源于stack exchange,提问作者nemox007
相关产品推荐
相关产品推荐

